Skip to content

Commit

Permalink
chore: add benchmarks for alba and panko_serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Mar 25, 2023
1 parent 7e7cf5e commit 49ba50b
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -12,11 +12,13 @@ end

group :development, :test do
gem 'active_model_serializers', '~> 0.8'
gem 'alba'
gem 'benchmark-ips'
gem 'benchmark-memory'
gem 'blueprinter', '~> 0.8'
gem 'memory_profiler'
gem 'mongoid'
gem 'panko_serializer'
gem 'pry-byebug', '~> 3.9'
gem 'rails' unless ENV['NO_RAILS']
gem 'rake', '~> 13.0'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Expand Up @@ -67,6 +67,7 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
alba (2.2.0)
ast (2.4.2)
benchmark-ips (2.8.3)
benchmark-memory (0.1.2)
Expand Down Expand Up @@ -128,6 +129,9 @@ GEM
nokogiri (1.14.2-x86_64-linux)
racc (~> 1.4)
oj (3.14.2)
panko_serializer (0.7.9)
activesupport
oj (> 3.11.0, < 4.0.0)
parallel (1.22.1)
parser (3.2.1.1)
ast (~> 2.4.1)
Expand Down Expand Up @@ -235,12 +239,14 @@ PLATFORMS

DEPENDENCIES
active_model_serializers (~> 0.8)
alba
benchmark-ips
benchmark-memory
blueprinter (~> 0.8)
memory_profiler
mongoid
oj_serializers!
panko_serializer
pry-byebug (~> 3.9)
rails
rake (~> 13.0)
Expand Down
18 changes: 18 additions & 0 deletions benchmarks/album_serializer_benchmark.rb
Expand Up @@ -10,6 +10,12 @@
expect(AlbumSerializer.one(album, special: true).to_json).to eq AlbumBlueprint.render(album, special: true)
Benchmark.ips do |x|
x.config(time: 5, warmup: 2)
x.report('panko') do
AlbumPanko.new.serialize_to_json(album)
end
x.report('alba') do
AlbumAlba.new(album).serialize
end
x.report('oj_serializers') do
Oj.dump AlbumSerializer.one_as_json(album)
end
Expand All @@ -30,6 +36,12 @@
albums = 100.times.map { Album.abraxas }
Benchmark.ips do |x|
x.config(time: 5, warmup: 2)
x.report('panko') do
Panko::ArraySerializer.new(albums, each_serializer: AlbumPanko).to_json
end
x.report('alba') do
AlbumAlba.new(albums).serialize
end
x.report('oj_serializers') do
Oj.dump AlbumSerializer.many_as_json(albums)
end
Expand All @@ -50,6 +62,12 @@
albums = 1000.times.map { Album.abraxas }
Benchmark.ips do |x|
x.config(time: 5, warmup: 2)
x.report('panko') do
Panko::ArraySerializer.new(albums, each_serializer: AlbumPanko).to_json
end
x.report('alba') do
AlbumAlba.new(albums).serialize
end
x.report('oj_serializers') do
Oj.dump AlbumSerializer.many_as_json(albums)
end
Expand Down
58 changes: 58 additions & 0 deletions spec/support/serializers/alba.rb
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'alba'

Alba.backend = :oj
Alba.inflector = :active_support

class SongAlba
include Alba::Resource
transform_keys :lower_camel

attributes :id, if: proc { |song| !song.new_record? }

attributes(
:track,
:name,
)

attributes
def composers(song)
song.composer&.split(', ')
end
end

class AlbumAlba
include Alba::Resource
transform_keys :lower_camel

attributes :id, if: proc { |album| !album.new_record? }

attributes(
:name,
:genres,
)

attributes :release, if: proc { |album| album.released? }
def release(album)
album.release_date.strftime('%B %d, %Y')
end

attributes
def special
options[:special]
end

one :songs, resource: SongAlba
many :other_songs, resource: SongAlba, if: proc { |album| album.other_songs.present? }
end

class ModelAlba
include Alba::Resource
transform_keys :lower_camel

attributes(
:id,
:name,
)
end
81 changes: 81 additions & 0 deletions spec/support/serializers/panko.rb
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require 'panko_serializer'

Mongoid::Criteria.prepend Module.new {
def track
end
}

Album.prepend Module.new {
def other_songs
[]
end
}

class SongPanko < Panko::Serializer
attributes
def id
object.id unless object.new_record?
end

attributes(
:track,
:name,
)

attributes
def composers
object.composer&.split(', ')
end

# NOTE: Panko does not have a way to conditionally not include an attribute.
def self.filters_for(context, scope)
{
except: [:id]
}
end
end

class AlbumPanko < Panko::Serializer
attributes
def id
object.id unless object.new_record?
end

attributes(
:name,
:genres,
)

attributes
def release
object.release_date.strftime('%B %d, %Y') if object.released?
end

attributes
def special
context[:special]
end

has_one :songs, serializer: SongPanko
has_many :other_songs, serializer: SongPanko

def other_songs
other_songs if other_songs.present?
end

# NOTE: Panko does not have a way to conditionally not include an attribute.
def self.filters_for(context, scope)
{
except: [:id, :special]
}
end
end

class ModelPanko < Panko::Serializer
attributes(
:id,
:name,
)
end

0 comments on commit 49ba50b

Please sign in to comment.