eprothro
2/29/2016 - 7:02 PM

delegator_v_AMS.rb

# Serializing 1 string attribute, 1 overriden uuid.to_s attribute,
# and 1 empty array, 30 times.
# bmbm:
#                  user     system      total        real
# serializer   0.010000   0.000000   0.010000 (  0.002572)
# delegator    0.000000   0.000000   0.000000 (  0.000424)

# ips:
#  delegator:     3600.6 i/s
# serializer:      427.6 i/s - 8.42x slower
#
# 3ms here is not insignificant when projecting
# to much larger json payloads. Considering that
# AMS isn't much easier to use or more flexible than
# simple decorators and, more importantly, this is not
# something that changes much (an API's format), I don't
# see a lot of value in the adapter abstraction that we lose.

messages = Array.new(30) do
  Message.new(id: Cassandra::Uuid.new(SecureRandom.uuid), foo: 'some text')
end and ''

Benchmark.ips do |r|
  r.config(:time => 1, :warmup => 0)

  r.report('delegator') do
    messages.each do |message|
      DelegatorMessageSerializer.new(message).serializable_hash.as_json
    end
  end
  r.report('serializer') do
    messages.each do |message|
      ActiveModel::SerializableResource.new(message, serializer: SerializerMessageSerializer).serializable_hash.as_json
    end
  end

  r.compare!
end and ''