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

- old
+ new

@@ -11,24 +11,30 @@ gem "active_model_serializers" gem "activerecord", "6.1.3" gem "alba", path: '../' gem "benchmark-ips" + gem "benchmark-memory" gem "blueprinter" gem "jbuilder" + gem "jserializer" gem "jsonapi-serializer" # successor of fast_jsonapi gem "multi_json" + gem "panko_serializer" + gem "pg" gem "primalize" gem "oj" gem "representable" gem "simple_ams" gem "sqlite3" end # --- Test data model setup --- +require "pg" require "active_record" +require "active_record/connection_adapters/postgresql_adapter" require "logger" require "oj" require "sqlite3" Oj.optimize_rails @@ -152,10 +158,27 @@ comment.call(self, :id, :body) end end end +# --- Jserializer serializers --- + +require 'jserializer' + +class JserializerCommentSerializer < Jserializer::Base + attributes :id, :body +end + +class JserializerPostSerializer < Jserializer::Base + attributes :id, :body, :commenter_names + has_many :comments, serializer: JserializerCommentSerializer + def commenter_names + object.commenters.pluck(:name) + end +end + + # --- JSONAPI:Serializer serializers / (successor of fast_jsonapi) --- class JsonApiStandardCommentSerializer include JSONAPI::Serializer @@ -213,10 +236,29 @@ attribute :comments do |post| post.comments.map { |comment| JsonApiSameFormatCommentSerializer.new(comment) } end end +# --- Panko serializers --- +# +require "panko_serializer" + +class PankoCommentSerializer < Panko::Serializer + attributes :id, :body +end + + +class PankoPostSerializer < Panko::Serializer + attributes :id, :body, :commenter_names + + has_many :comments, serializer: PankoCommentSerializer + + def commenter_names + object.comments.pluck(:name) + end +end + # --- Primalize serializers --- # class PrimalizeCommentResource < Primalize::Single attributes id: integer, body: string end @@ -306,12 +348,14 @@ 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 } jsonapi_same_format = proc { JsonApiSameFormatPostSerializer.new(post).to_json } +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 } @@ -322,12 +366,14 @@ alba: alba, alba_inline: alba_inline, ams: ams, blueprinter: blueprinter, 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, }.each { |name, serializer| puts "#{name.to_s.ljust(24, ' ')} #{serializer.call}" } @@ -339,12 +385,14 @@ x.report(:alba, &alba) x.report(:alba_inline, &alba_inline) x.report(:ams, &ams) x.report(:blueprinter, &blueprinter) 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) @@ -357,11 +405,13 @@ x.report(:alba, &alba) x.report(:alba_inline, &alba_inline) x.report(:ams, &ams) x.report(:blueprinter, &blueprinter) 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)