Last active
August 29, 2015 14:24
-
-
Save groyoh/0dbbd3c80a07dc56ef2d to your computer and use it in GitHub Desktop.
Delegator vs Inheritance benchmark
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| require 'bundler/inline' | |
| require 'securerandom' | |
| require 'delegate' | |
| gemfile(true) do | |
| source 'https://rubygems.org' | |
| gem 'rails' | |
| gem 'active_model_serializers', github: "runtastic/active_model_serializers" | |
| gem 'activesupport', require: 'active_support/inflector' | |
| gem 'benchmark-ips', require: 'benchmark/ips' | |
| end | |
| module ActiveModel | |
| class Serializer | |
| class Adapter | |
| def cache_check(serializer) | |
| yield | |
| end | |
| end | |
| end | |
| end | |
| class Model | |
| def initialize(hash = {}) | |
| @attributes = hash | |
| end | |
| def read_attribute_for_serialization(attr) | |
| @attributes[attr] | |
| end | |
| end | |
| N = 50 | |
| S = Set.new((0..N).map { SecureRandom.hex }) | |
| puts "With #{S.size} attributes" | |
| Organizer = Class.new(Model) | |
| class OrganizerSerializer < ActiveModel::Serializer | |
| S.each do |attr| | |
| attribute attr | |
| end | |
| end | |
| class TypePluralizer < SimpleDelegator | |
| def type | |
| object.class.name.pluralize | |
| end | |
| end | |
| class SerializerTypePluralizer < DelegateClass(ActiveModel::Serializer) | |
| def type | |
| object.class.name.pluralize | |
| end | |
| end | |
| class BaseSerializer < ActiveModel::Serializer | |
| def type | |
| object.class.name.pluralize | |
| end | |
| end | |
| class PluralTypeOrganizerSerializer < BaseSerializer | |
| S.each do |attr| | |
| attribute attr | |
| end | |
| end | |
| organizer_attrs = S.each_with_object({}) do |attr_name, attrs| | |
| attrs[attr_name] = SecureRandom.hex | |
| end | |
| OBJECT = Organizer.new(organizer_attrs) | |
| Benchmark.ips do |x| | |
| x.config(time: 15) | |
| # x.report("bare") do | |
| # serializer = OrganizerSerializer.new(OBJECT) | |
| # ActiveModel::Serializer::Adapter.create(serializer).to_json | |
| # end | |
| x.report("with inheritance") do | |
| serializer = PluralTypeOrganizerSerializer.new(OBJECT) | |
| ActiveModel::Serializer::Adapter.create(serializer).to_json | |
| end | |
| x.report("SimpleDelegator") do | |
| serializer = OrganizerSerializer.new(OBJECT) | |
| serializer = TypePluralizer.new(serializer) | |
| ActiveModel::Serializer::Adapter.create(serializer).to_json | |
| end | |
| x.report("DelegatorClass") do | |
| serializer = OrganizerSerializer.new(OBJECT) | |
| serializer = SerializerTypePluralizer.new(serializer) | |
| ActiveModel::Serializer::Adapter.create(serializer).to_json | |
| end | |
| x.compare! | |
| end |
This file contains hidden or 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
| $ ./delegator_vs_inheritance.rb # using ruby 2.2 | |
| With 51 attributes | |
| Calculating ------------------------------------- | |
| with inheritance 131.000 i/100ms | |
| SimpleDelegator 131.000 i/100ms | |
| DelegatorClass 124.000 i/100ms | |
| ------------------------------------------------- | |
| with inheritance 1.324k (± 3.5%) i/s - 19.912k | |
| SimpleDelegator 1.326k (± 3.2%) i/s - 19.912k | |
| DelegatorClass 1.328k (± 2.8%) i/s - 19.964k | |
| Comparison: | |
| DelegatorClass: 1328.5 i/s | |
| SimpleDelegator: 1326.3 i/s - 1.00x slower | |
| with inheritance: 1323.7 i/s - 1.00x slower | |
| $ ./delegator_vs_inheritance.rb # using jruby 1.7.19 | |
| With 51 attributes | |
| Calculating ------------------------------------- | |
| with inheritance 98.000 i/100ms | |
| SimpleDelegator 166.000 i/100ms | |
| DelegatorClass 192.000 i/100ms | |
| ------------------------------------------------- | |
| with inheritance 2.652k (±13.2%) i/s - 38.906k | |
| SimpleDelegator 2.782k (± 9.5%) i/s - 41.334k | |
| DelegatorClass 2.840k (± 6.9%) i/s - 42.432k | |
| Comparison: | |
| DelegatorClass: 2840.1 i/s | |
| SimpleDelegator: 2781.9 i/s - 1.02x slower | |
| with inheritance: 2652.4 i/s - 1.07x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment