benchmark/single_resource.rb in alba-1.5.0 vs benchmark/single_resource.rb in alba-1.6.0

- old
+ new

@@ -14,10 +14,11 @@ gem "alba", path: '../' gem "benchmark-ips" gem "benchmark-memory" gem "blueprinter" gem "jbuilder" + gem 'turbostreamer' gem "jserializer" gem "jsonapi-serializer" # successor of fast_jsonapi gem "multi_json" gem "panko_serializer" gem "pg" @@ -251,11 +252,11 @@ attributes :id, :body, :commenter_names has_many :comments, serializer: PankoCommentSerializer def commenter_names - object.comments.pluck(:name) + object.commenters.pluck(:name) end end # --- Primalize serializers --- # @@ -322,10 +323,33 @@ def commenter_names object.commenters.pluck(:name) end end +require 'turbostreamer' +TurboStreamer.set_default_encoder(:json, :oj) + +class TurbostreamerSerializer + def initialize(post) + @post = post + end + + def to_json + TurboStreamer.encode do |json| + json.object! do + json.extract! @post, :id, :body, :commenter_names + + json.comments @post.comments do |comment| + json.object! do + json.extract! comment, :id, :body + end + end + end + end + end +end + # --- Test data creation --- post = Post.create!(body: 'post') user1 = User.create!(name: 'John') user2 = User.create!(name: 'Jane') @@ -345,10 +369,11 @@ many :comments do attributes :id, :body end end end + ams = Proc.new { AMSPostSerializer.new(post, {}).to_json } blueprinter = Proc.new { PostBlueprint.render(post) } jbuilder = Proc.new { post.to_builder.target! } jserializer = Proc.new { JserializerPostSerializer.new(post).to_json } jsonapi = proc { JsonApiStandardPostSerializer.new(post).to_json } @@ -356,10 +381,11 @@ panko = proc { PankoPostSerializer.new.serialize_to_json(post) } 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 } simple_ams = Proc.new { SimpleAMS::Renderer.new(post, serializer: SimpleAMSPostSerializer).to_json } +turbostreamer = Proc.new { TurbostreamerSerializer.new(post).to_json } # --- Execute the serializers to check their output --- puts "Serializer outputs ----------------------------------" { @@ -374,11 +400,14 @@ panko: panko, primalize: primalize, rails: rails, representable: representable, simple_ams: simple_ams, -}.each { |name, serializer| puts "#{name.to_s.ljust(24, ' ')} #{serializer.call}" } + turbostreamer: turbostreamer +}.each do |name, serializer| + puts "#{name.to_s.ljust(24, ' ')} #{serializer.call}" +end # --- Run the benchmarks --- require 'benchmark/ips' Benchmark.ips do |x| @@ -393,10 +422,11 @@ x.report(:panko, &panko) x.report(:primalize, &primalize) x.report(:rails, &rails) x.report(:representable, &representable) x.report(:simple_ams, &simple_ams) + x.report(:turbostreamer, &turbostreamer) x.compare! end @@ -413,8 +443,9 @@ x.report(:panko, &panko) x.report(:primalize, &primalize) x.report(:rails, &rails) x.report(:representable, &representable) x.report(:simple_ams, &simple_ams) + x.report(:turbostreamer, &turbostreamer) x.compare! end