Skip to content

Commit

Permalink
Add primalize benchmark + ips
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaskins committed Apr 15, 2021
1 parent f2772c2 commit ad78703
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion benchmark/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
gem "active_model_serializers"
gem "blueprinter"
gem "representable"
gem "primalize"
gem "alba", path: '../'
gem "oj"
gem "multi_json"
gem "benchmark-ips"
end

require "active_record"
Expand Down Expand Up @@ -87,6 +89,25 @@ class AlbaPostResource
end
end

class PrimalizeCommentResource < Primalize::Single
attributes id: integer, body: string
end

class PrimalizePostResource < Primalize::Single
alias post object

attributes(
id: integer,
body: string,
comments: array(primalize(PrimalizeCommentResource)),
commenter_names: array(string),
)

def commenter_names
post.commenters.pluck(:name)
end
end

require "jbuilder"
class Post
def to_builder
Expand Down Expand Up @@ -183,7 +204,10 @@ def commenter_names
end
end
end
[alba, jbuilder, ams, rails, blueprinter, representable, alba_inline].each {|x| puts x.call }
primalize = proc do
PrimalizePostResource.new(post).to_json
end
[alba, jbuilder, ams, rails, blueprinter, representable, alba_inline, primalize].each {|x| puts x.call }

require 'benchmark'
time = 1000
Expand All @@ -195,4 +219,19 @@ def commenter_names
x.report(:blueprinter) { time.times(&blueprinter) }
x.report(:representable) { time.times(&representable) }
x.report(:alba_inline) { time.times(&alba_inline) }
x.report(:primalize) { time.times(&primalize) }
end

require 'benchmark/ips'
Benchmark.ips do |x|
x.report(:alba, &alba)
x.report(:jbuilder, &jbuilder)
x.report(:ams, &ams)
x.report(:rails, &rails)
x.report(:blueprinter, &blueprinter)
x.report(:representable, &representable)
x.report(:alba_inline, &alba_inline)
x.report(:primalize, &primalize)

x.compare!
end

0 comments on commit ad78703

Please sign in to comment.