#!/usr/bin/env ruby require 'bundler/inline' $gem_versions = { "oj" => "3.0.5", "msgpack" => "1.1.0", "google-protobuf" => "3.2.0.2" } gemfile do $gem_versions.each do |name,version| gem name, version end gem "benchmark-ips" end $data = { first_name: "Sebastian", last_name: "Röbke", age: 35, hobbies: ["Tennis", "Programming"] } # oj def oj_dump Oj.dump($data) end $data_as_json = Oj.dump($data) def oj_load Oj.load($data_as_json) end # msgpack def msgpack_dump $data.to_msgpack end $data_as_msgpack = $data.to_msgpack def msgpack_load MessagePack.unpack($data_as_msgpack) end # protobuf pool = Google::Protobuf::DescriptorPool.new pool.build do add_message "Person" do optional :first_name, :string, 1 optional :last_name, :string, 2 optional :age, :uint32, 3 repeated :hobbies, :string, 4 end end Person = pool.lookup("Person").msgclass $protobuf_data = Person.new($data) def protobuf_dump Person.encode($protobuf_data) end $data_as_protobuf = Person.encode($protobuf_data) def protobuf_load Person.decode($data_as_protobuf) end puts "=== Gem versions" puts $gem_versions puts "=== serialize" Benchmark.ips do |x| x.report("oj") { oj_dump } x.report("msgpack") { msgpack_dump } x.report("google-protobuf") { protobuf_dump } x.compare! end puts "=== deserialize" Benchmark.ips do |x| x.report("oj") { oj_load } x.report("msgpack") { msgpack_load } x.report("google-protobuf") { protobuf_load } x.compare! end # Results # # $ ruby bm_serialization.rb # # === Gem versions # {"oj"=>"3.0.5", "msgpack"=>"1.1.0", "google-protobuf"=>"3.2.0.2"} # === serialize # Warming up -------------------------------------- # oj 83.925k i/100ms # msgpack 58.297k i/100ms # google-protobuf 71.993k i/100ms # Calculating ------------------------------------- # oj 1.141M (± 3.9%) i/s - 5.707M in 5.010548s # msgpack 713.883k (± 2.8%) i/s - 3.614M in 5.067156s # google-protobuf 922.269k (± 3.3%) i/s - 4.608M in 5.001378s # # Comparison: # oj: 1140746.2 i/s # google-protobuf: 922268.9 i/s - 1.24x slower # msgpack: 713883.0 i/s - 1.60x slower # # === deserialize # Warming up -------------------------------------- # oj 33.616k i/100ms # msgpack 31.927k i/100ms # google-protobuf 29.652k i/100ms # Calculating ------------------------------------- # oj 381.786k (± 3.0%) i/s - 1.916M in 5.023434s # msgpack 368.309k (± 6.6%) i/s - 1.852M in 5.050387s # google-protobuf 330.019k (± 2.7%) i/s - 1.661M in 5.035301s # # Comparison: # oj: 381786.3 i/s # msgpack: 368309.1 i/s - same-ish: difference falls within error # google-protobuf: 330019.5 i/s - 1.16x slower #