-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add benchmarks for alba and panko_serializer
- Loading branch information
Showing
10 changed files
with
279 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'benchmark_helper' | ||
require 'support/models/sql' | ||
|
||
class PlayerPanko < Panko::Serializer | ||
attributes :id, :first_name, :last_name, :full_name | ||
|
||
def id | ||
object.id if object.persisted? | ||
end | ||
|
||
def full_name | ||
object.full_name | ||
end | ||
end | ||
|
||
# NOTE: This example is quite contrived. Finding good test cases is as hard as | ||
# finding good names. | ||
class ScoresPanko < Panko::Serializer | ||
attributes :high_score, :score | ||
end | ||
|
||
class GamePanko < Panko::Serializer | ||
attributes :id, :name | ||
|
||
has_one :scores, serializer: ScoresPanko | ||
|
||
has_one :best_player, serializer: PlayerPanko | ||
has_many :players, serializer: PlayerPanko | ||
|
||
def id | ||
object.id if object.persisted? | ||
end | ||
end | ||
|
||
Game.prepend Module.new { | ||
def scores | ||
self | ||
end | ||
} | ||
|
||
RSpec.describe 'GameSerializer', :benchmark do | ||
context 'albums' do | ||
it 'serializing a model' do | ||
game = Game.example | ||
|
||
Benchmark.ips do |x| | ||
x.config(time: 5, warmup: 2) | ||
x.report('oj_serializers as_hash') do | ||
Oj.dump GameSerializer.one_as_hash(game) | ||
end | ||
x.report('oj_serializers') do | ||
Oj.dump GameSerializer.one_as_json(game) | ||
end | ||
x.report('panko') do | ||
GamePanko.new.serialize_to_json(game) | ||
end | ||
x.compare! | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'alba' | ||
|
||
Alba.backend = :oj_rails | ||
Alba.inflector = :active_support | ||
|
||
class BaseAlba | ||
include Alba::Resource | ||
transform_keys :lower_camel | ||
end | ||
|
||
class SongAlba < BaseAlba | ||
attributes :id, if: proc { |song| !song.new_record? } | ||
|
||
attributes( | ||
:track, | ||
:name, | ||
:composers, | ||
) | ||
|
||
def composers(song) | ||
song.composer&.split(', ') | ||
end | ||
end | ||
|
||
class AlbumAlba < BaseAlba | ||
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 :special, if: proc { params[:special] } | ||
def special(album) | ||
params[:special] | ||
end | ||
|
||
many :songs, resource: SongAlba | ||
many :other_songs, resource: SongAlba, if: proc { |album| album.other_songs.present? } | ||
end | ||
|
||
class ModelAlba < BaseAlba | ||
attributes( | ||
:id, | ||
:name, | ||
) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.