benchmark/local.rb in alba-1.1.0 vs benchmark/local.rb in alba-1.2.0

- old
+ new

@@ -15,10 +15,11 @@ gem "benchmark-ips" gem "blueprinter" gem "jbuilder" gem "jsonapi-serializer" # successor of fast_jsonapi gem "multi_json" + gem "primalize" gem "oj" gem "representable" gem "sqlite3" end @@ -211,10 +212,31 @@ attribute :comments do |post| post.comments.map { |comment| JsonApiSameFormatCommentSerializer.new(comment) } end end +# --- Primalize serializers --- +# +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 + # --- Representable serializers --- require "representable" class CommentRepresenter < Representable::Decorator @@ -263,10 +285,11 @@ ams = Proc.new { AMSPostSerializer.new(post, {}).to_json } blueprinter = Proc.new { PostBlueprint.render(post) } jbuilder = Proc.new { post.to_builder.target! } jsonapi = proc { JsonApiStandardPostSerializer.new(post).to_json } jsonapi_same_format = proc { JsonApiSameFormatPostSerializer.new(post).to_json } +primalize = proc { PrimalizePostResource.new(post).to_json } rails = Proc.new { ActiveSupport::JSON.encode(post.serializable_hash(include: :comments)) } representable = Proc.new { PostRepresenter.new(post).to_json } # --- Execute the serializers to check their output --- @@ -277,10 +300,11 @@ ams: ams, blueprinter: blueprinter, jbuilder: jbuilder, # different order jsonapi: jsonapi, # nested JSON:API format jsonapi_same_format: jsonapi_same_format, + primalize: primalize, rails: rails, representable: representable }.each { |name, serializer| puts "#{name.to_s.ljust(24, ' ')} #{serializer.call}" } # --- Run the benchmarks --- @@ -293,10 +317,11 @@ x.report(:ams) { time.times(&ams) } x.report(:blueprinter) { time.times(&blueprinter) } x.report(:jbuilder) { time.times(&jbuilder) } x.report(:jsonapi) { time.times(&jsonapi) } x.report(:jsonapi_same_format) { time.times(&jsonapi_same_format) } + x.report(:primalize) { time.times(&primalize) } x.report(:rails) { time.times(&rails) } x.report(:representable) { time.times(&representable) } end require 'benchmark/ips' @@ -306,9 +331,10 @@ x.report(:ams, &ams) x.report(:blueprinter, &blueprinter) x.report(:jbuilder, &jbuilder) x.report(:jsonapi, &jsonapi) x.report(:jsonapi_same_format, &jsonapi_same_format) + x.report(:primalize, &primalize) x.report(:rails, &rails) x.report(:representable, &representable) x.compare! end