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

- old
+ new

@@ -13,11 +13,13 @@ gem "activerecord", "6.1.3" gem "alba", path: '../' gem "benchmark-ips" gem "benchmark-memory" gem "blueprinter" + gem "fast_serializer_ruby" gem "jbuilder" + gem 'turbostreamer' gem "jserializer" gem "jsonapi-serializer" # successor of fast_jsonapi gem "multi_json" gem "panko_serializer" gem "pg" @@ -136,10 +138,31 @@ def commenter_names commenters.pluck(:name) end end +# --- Fast Serializer Ruby + +require "fast_serializer" + +class FastSerializerCommentResource + include ::FastSerializer::Schema::Mixin + attributes :id, :body +end + +class FastSerializerPostResource + include ::FastSerializer::Schema::Mixin + + attributes :id, :body + + attribute :commenter_names do + object.commenters.pluck(:name) + end + + has_many :comments, serializer: FastSerializerCommentResource +end + # --- JBuilder serializers --- require "jbuilder" class Post @@ -253,11 +276,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 --- # @@ -330,10 +353,35 @@ def commenter_names object.commenters.pluck(:name) end end +require 'turbostreamer' +TurboStreamer.set_default_encoder(:json, :oj) + +class TurbostreamerSerializer + def initialize(posts) + @posts = posts + end + + def to_json + TurboStreamer.encode do |json| + json.array! @posts do |post| + 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 +end + # --- Test data creation --- 100.times do |i| post = Post.create!(body: "post#{i}") user1 = User.create!(name: "John#{i}") @@ -342,11 +390,11 @@ post.comments.create!(commenter: user1, body: "Comment1_#{i}_#{n}") post.comments.create!(commenter: user2, body: "Comment2_#{i}_#{n}") end end -posts = Post.all.to_a +posts = Post.all.includes(:comments, :commenters) # --- Store the serializers in procs --- alba = Proc.new { AlbaPostResource.new(posts).serialize } alba_inline = Proc.new do @@ -358,12 +406,13 @@ many :comments do attributes :id, :body end end end -ams = Proc.new { ActiveModelSerializers::SerializableResource.new(posts, {}).as_json } +ams = Proc.new { ActiveModelSerializers::SerializableResource.new(posts, {each_serializer: AMSPostSerializer}).to_json } blueprinter = Proc.new { PostBlueprint.render(posts) } +fast_serializer = Proc.new { FastSerializerPostResource.new(posts).to_json } jbuilder = Proc.new do Jbuilder.new do |json| json.array!(posts) do |post| json.post post.to_builder end @@ -377,47 +426,52 @@ rails = Proc.new do ActiveSupport::JSON.encode(posts.map{ |post| post.serializable_hash(include: :comments) }) end representable = Proc.new { PostsRepresenter.new(posts).to_json } simple_ams = Proc.new { SimpleAMS::Renderer::Collection.new(posts, serializer: SimpleAMSPostSerializer).to_json } +turbostreamer = Proc.new { TurbostreamerSerializer.new(posts).to_json } # --- Execute the serializers to check their output --- - +GC.disable puts "Serializer outputs ----------------------------------" { alba: alba, alba_inline: alba_inline, ams: ams, blueprinter: blueprinter, + fast_serializer: fast_serializer, jbuilder: jbuilder, # different order jserializer: jserializer, jsonapi: jsonapi, # nested JSON:API format jsonapi_same_format: jsonapi_same_format, panko: panko, primalize: primalize, rails: rails, representable: representable, simple_ams: simple_ams, + turbostreamer: turbostreamer }.each { |name, serializer| puts "#{name.to_s.ljust(24, ' ')} #{serializer.call}" } # --- Run the benchmarks --- require 'benchmark/ips' Benchmark.ips do |x| x.report(:alba, &alba) x.report(:alba_inline, &alba_inline) x.report(:ams, &ams) x.report(:blueprinter, &blueprinter) + x.report(:fast_serializer, &fast_serializer) x.report(:jbuilder, &jbuilder) x.report(:jserializer, &jserializer) x.report(:jsonapi, &jsonapi) x.report(:jsonapi_same_format, &jsonapi_same_format) 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 @@ -425,17 +479,19 @@ Benchmark.memory do |x| x.report(:alba, &alba) x.report(:alba_inline, &alba_inline) x.report(:ams, &ams) x.report(:blueprinter, &blueprinter) + x.report(:fast_serializer, &fast_serializer) x.report(:jbuilder, &jbuilder) x.report(:jserializer, &jserializer) x.report(:jsonapi, &jsonapi) x.report(:jsonapi_same_format, &jsonapi_same_format) 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