1
0
mirror of https://github.com/meineerde/rackstash.git synced 2026-01-31 17:27:13 +00:00

Use a class instance for Message's default formatter

Calling #call in a custom class instead of a lambda performs much
better:

    require 'benchmark/ips'

    class RawFormatter
      def call(_severity, _timestamp, _progname, msg) msg end
    end

    Benchmark.ips do |x|
      message = 'my message'

      proc = ->(_severity, _timestamp, _progname, msg) { msg }
      instance = RawFormatter.new

      x.report('proc') do
        proc.call(nil, nil, nil, message)
      end

      x.report('instance') do
        instance.call(nil, nil, nil, message)
      end

      x.compare!
    end

    # Warming up --------------------------------------
    #                 proc   159.882k i/100ms
    #             instance   182.648k i/100ms
    # Calculating -------------------------------------
    #                 proc      4.612M (± 5.2%) i/s -     23.023M in   5.005950s
    #             instance      7.716M (± 6.3%) i/s -     38.539M in   5.015306s
    #
    # Comparison:
    #             instance:  7716097.7 i/s
    #                 proc:  4611920.5 i/s - 1.67x  slower
This commit is contained in:
Holger Just 2017-01-25 23:29:34 +01:00
parent 2b72a1e5cf
commit 2261315dcc
3 changed files with 16 additions and 1 deletions

View File

@ -25,4 +25,10 @@ module Rackstash
"#{msg2str(msg)}\n"
end
end
class RawFormatter
def call(_severity, _timestamp, _progname, msg)
msg
end
end
end

View File

@ -6,7 +6,7 @@
module Rackstash
# This class and all its data are immutable after initialization
class Message
RAW_FORMATTER = ->(_severity, _timestamp, _progname, msg) { msg }
RAW_FORMATTER = RawFormatter.new
SEVERITY_LABEL = [
'DEBUG'.freeze,

View File

@ -46,3 +46,12 @@ describe Rackstash::Formatter do
expect(formatter.call('ERROR', Time.now, 'progname', object)).to eql "#{inspected}\n"
end
end
describe Rackstash::RawFormatter do
let(:formatter) { Rackstash::RawFormatter.new }
it 'returns the message' do
msg = 'my message'
expect(formatter.call('ERROR', Time.now, 'progname', msg)).to equal msg
end
end