benchmark/collection.rb in alba-3.4.0 vs benchmark/collection.rb in alba-3.5.0

- old
+ new

@@ -5,10 +5,12 @@ # --- Alba serializers --- require "alba" +Alba.inflector = :active_support + class AlbaCommentResource include ::Alba::Resource attributes :id, :body end @@ -19,10 +21,20 @@ post.commenters.pluck(:name) end many :comments, resource: AlbaCommentResource end +class AlbaCommentWithTransformationResource < AlbaCommentResource + transform_keys :lower_camel +end + +class AlbaPostWithTransformationResource < AlbaPostResource + many :comments, resource: AlbaCommentWithTransformationResource + + transform_keys :lower_camel +end + # --- ActiveModelSerializer serializers --- require "active_model_serializers" ActiveModelSerializers.logger = Logger.new(nil) @@ -131,11 +143,11 @@ items class: Post do property :id property :body property :commenter_names - collection :comments + collection :comments, decorator: CommentRepresenter end def commenter_names commenters.pluck(:name) end @@ -203,10 +215,11 @@ posts = Post.all.includes(:comments, :commenters) # --- Store the serializers in procs --- alba = Proc.new { AlbaPostResource.new(posts).serialize } +alba_with_transformation = Proc.new { AlbaPostWithTransformationResource.new(posts).serialize } alba_inline = Proc.new do Alba.serialize(posts) do attributes :id, :body attribute :commenter_names do |post| post.commenters.pluck(:name) @@ -220,11 +233,11 @@ blueprinter = Proc.new { PostBlueprint.render(posts) } fast_serializer = Proc.new { FastSerializerPostResource.new(posts).to_json } jserializer = Proc.new { JserializerPostSerializer.new(posts, is_collection: true).to_json } panko = proc { Panko::ArraySerializer.new(posts, each_serializer: PankoPostSerializer).to_json } rails = Proc.new do - ActiveSupport::JSON.encode(posts.map{ |post| post.serializable_hash(include: :comments) }) + posts.to_json(include: {comments: {only: [:id, :body]}}, methods: [:commenter_names]) 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 } @@ -252,10 +265,11 @@ # --- Run the benchmarks --- benchmark_body = lambda do |x| x.report(:alba, &alba) + x.report(:alba_with_transformation, &alba_with_transformation) x.report(:alba_inline, &alba_inline) x.report(:ams, &ams) x.report(:blueprinter, &blueprinter) x.report(:fast_serializer, &fast_serializer) x.report(:jserializer, &jserializer) @@ -271,5 +285,13 @@ require 'benchmark/ips' Benchmark.ips(&benchmark_body) require 'benchmark/memory' Benchmark.memory(&benchmark_body) + +# --- Show gem versions --- + +puts "Gem versions:" +gems = %w[alba active_model_serializers blueprinter fast_serializer jserializer panko_serializer representable simple_ams turbostreamer] +Bundler.load.specs.each do |spec| + puts "#{spec.name}: #{spec.version}" if gems.include?(spec.name) +end