Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skryukov/770a9793dc13ddb9c39256bdd0dfe431 to your computer and use it in GitHub Desktop.
Save skryukov/770a9793dc13ddb9c39256bdd0dfe431 to your computer and use it in GitHub Desktop.
blueprinter benchmark comparisons
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'oj'
gem 'benchmark-ips', require: 'benchmark/ips'
gem 'kalibera'
gem 'benchmark-memory', require: 'benchmark/memory'
gem 'activesupport'
# https://github.com/procore/blueprinter
if ENV['OPTIMIZED'] == '1'
gem 'blueprinter', github: 'midhun-thimmapuram/blueprinter'
elsif ENV['OPTIMIZED'] == '2'
gem 'blueprinter', github: 'skryukov/blueprinter', branch: 'memoize-fields-for'
else
gem 'blueprinter'
end
end
require 'active_support'
require 'active_support/core_ext/object' # For Hash#deep_dup
# Define models
Issue = Struct.new(:id, :number, :title, :user, :labels) do
alias_method :read_attribute_for_serialization, :send
def label_ids
labels.map(&:id)
end
def user_id
user.id
end
end
User = Struct.new(:id, :login) do
alias_method :read_attribute_for_serialization, :send
end
Label = Struct.new(:id, :name, :color) do
alias_method :read_attribute_for_serialization, :send
end
# Define serializers
Blueprinter.configure do |config|
config.generator = Oj
config.sort_fields_by = :definition
end
module BluePrint
class Label < Blueprinter::Base
identifier :id
fields :name, :color
end
class User < Blueprinter::Base
identifier :id
field :login
end
class Issue < Blueprinter::Base
identifier :id
fields :number, :title
association :labels, blueprint: Label
association :user, blueprint: User
end
end
# Generate data
users = Array.new(10) { |i| User.new(i, "User #{i}") }
labels = Array.new(4) { |i| Label.new(i, "Label #{i}", 'ffffff') }
issues = Array.new(10_000) { |i| Issue.new(i, i, "Issue #{i}", users.sample, labels.sample(rand(2..4))) }
serializers = [
{
name: :blueprinter,
serializer: -> { BluePrint::Issue.render_as_hash(issues) },
output_inspector: ->(output) { output.first }
}
]
# Display output
serializers.each do |s|
puts "\n#{s[:name]}:\n"
puts s[:output_inspector].call(s[:serializer].call).inspect
puts
end
# Run benchmarks
require 'benchmark'
Benchmark.bmbm do |b|
serializers.each do |s|
b.report(s[:name], &s[:serializer])
end
end
%i[ips memory].each do |bench|
Benchmark.send(bench) do |b|
b.config(time: 10, warmup: 5, stats: :bootstrap, confidence: 95) if b.respond_to?(:config)
serializers.each do |s|
b.report(s[:name], &s[:serializer])
end
b.compare!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment