Skip to content

Commit

Permalink
Add bench/perf.rb from 0.8 for reference
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Dec 16, 2015
1 parent 1301b52 commit 1c9a76a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions bench/perf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'rubygems'
require 'bundler/setup'
require 'rails/railtie'
require 'active_model_serializers'
require 'active_support/json'
require 'benchmark'

class User < Struct.new(:id, :name, :age, :about)
include ActiveModel::Serialization

def fast_hash
h = {
id: read_attribute_for_serialization(:id),
name: read_attribute_for_serialization(:name),
about: read_attribute_for_serialization(:about)
}
h[:age] = read_attribute_for_serialization(:age) if age > 18
h
end
end

class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :age, :about

def include_age?
object.age > 18
end
end

u = User.new(1, 'sam', 10, 'about')
s = UserSerializer.new(u)

n = 100000

Benchmark.bmbm do|x|
x.report('init') { n.times { UserSerializer.new(u) } }
x.report('fast_hash') { n.times { u.fast_hash } }
x.report('attributes') { n.times { UserSerializer.new(u).attributes } }
# x.report("serializable_hash") { n.times { u.serializable_hash } }
end

0 comments on commit 1c9a76a

Please sign in to comment.