benchmark/collection.rb in alba-1.4.0 vs benchmark/collection.rb in alba-1.5.0
- old
+ new
@@ -14,22 +14,27 @@
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
@@ -155,10 +160,26 @@
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
@@ -216,10 +237,30 @@
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
@@ -326,12 +367,14 @@
json.array!(posts) do |post|
json.post post.to_builder
end
end.target!
end
+jserializer = Proc.new { JserializerPostSerializer.new(posts, is_collection: true).to_json }
jsonapi = proc { JsonApiStandardPostSerializer.new(posts).to_json }
jsonapi_same_format = proc { JsonApiSameFormatPostSerializer.new(posts).to_json }
+panko = proc { Panko::ArraySerializer.new(posts, each_serializer: PankoPostSerializer).to_json }
primalize = proc { PrimalizePostsResource.new(posts: posts).to_json }
rails = Proc.new do
ActiveSupport::JSON.encode(posts.map{ |post| post.serializable_hash(include: :comments) })
end
representable = Proc.new { PostsRepresenter.new(posts).to_json }
@@ -344,12 +387,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}" }
@@ -361,12 +406,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)
@@ -379,11 +426,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)