Skip to content

Instantly share code, notes, and snippets.

@etehtsea
Forked from aishfenton/serializer_benchmarks.rb
Last active August 29, 2015 14:27
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 etehtsea/71a7aebd1755ebe12bbd to your computer and use it in GitHub Desktop.
Save etehtsea/71a7aebd1755ebe12bbd to your computer and use it in GitHub Desktop.
Performance comparison of different ruby serializer methods
source 'https://rubygems.org'
gem 'benchmark-ips'
gem 'msgpack'
gem 'yajl-ruby', platforms: :ruby
gem 'oj', platforms: :ruby
gem 'jrjackson', platforms: :jruby
gem 'gson', platforms: :jruby
GEM
remote: https://rubygems.org/
specs:
benchmark-ips (2.3.0)
gson (0.6.1-java)
jrjackson (0.2.9)
msgpack (0.6.2)
msgpack (0.6.2-java)
oj (2.12.12)
yajl-ruby (1.1.0)
PLATFORMS
java
ruby
DEPENDENCIES
benchmark-ips
gson
jrjackson
msgpack
oj
yajl-ruby
BUNDLED WITH
1.10.6
# coding: utf-8
require 'benchmark/ips'
require 'yaml'
require 'json'
require 'msgpack'
if defined?(JRUBY_VERSION)
require 'gson'
require 'jrjackson'
else
require 'yajl'
require 'oj'
end
def encode(msg, format)
case format
when :yaml
msg.to_yaml
when :binary
Marshal.dump(msg)
when :json
JSON.generate(msg)
when :yajl
Yajl::Encoder.encode(msg)
when :oj
Oj.dump(msg)
when :gson
Gson::Encoder.new.encode(msg)
when :jrjackson
JrJackson::Json.dump(msg)
when :msgpack
MessagePack.pack(msg)
end
end
def decode(str, format)
case format
when :yaml
YAML.load(str)
when :binary
Marshal.load(str)
when :json
JSON.parse(str)
when :yajl
Yajl::Parser.parse(str)
when :oj
Oj.load(str)
when :gson
Gson::Decoder.new.decode(str)
when :jrjackson
JrJackson::Json.load(str)
when :msgpack
MessagePack.unpack(str)
end
end
obj = {
:name => "Fredrick Smith",
:quantity => 1_000_000,
:addresses => {
:address1 => "12 Heather Street, Parnell, Auckland, New Zealand",
:address2 => "1 Queen Street, CBD, Auckland, New Zealand"
}
}
Benchmark.ips do |x|
x.config(time: 5, warmup: 10)
x.report("Marshal") { decode(encode(obj, :binary), :binary) }
x.report("JSON (built-in)") { decode(encode(obj, :json), :json) }
if defined?(JRUBY_VERSION)
x.report("JSON (using Gson)") { decode(encode(obj, :gson), :gson) }
x.report("JSON (using JrJackson)") do
decode(encode(obj, :jrjackson), :jrjackson)
end
else
x.report("JSON (using Yajl)") { decode(encode(obj, :yajl), :yajl) }
x.report("JSON (using Oj)") { decode(encode(obj, :oj), :oj) }
end
x.report("YAML") { decode(encode(obj, :yaml), :yaml) }
x.report("MessagePack") { decode(encode(obj, :msgpack), :msgpack) }
x.compare!
end
# jruby-9.0.1.0-150821
# $ JRUBY_OPTS="--server -J-Xmx2G" jruby -G -S serializer_benchmarks.rb
# MessagePack: 355148.9 i/s
# Marshal: 184758.5 i/s - 1.92x slower
# JSON (using Gson): 161817.3 i/s - 2.19x slower
# JSON (built-in): 122530.0 i/s - 2.90x slower
#JSON (using JrJackson): 10736.1 i/s - 33.08x slower
# YAML: 4309.6 i/s - 82.41x slower
# jruby-1.7.20.1
# $ JRUBY_OPTS="--server -J-Xmx2G" jruby -G -S serializer_benchmarks.rb
# MessagePack: 395416.6 i/s
# Marshal: 181686.8 i/s - 2.18x slower
# JSON (using Gson): 138398.9 i/s - 2.86x slower
# JSON (built-in): 133260.0 i/s - 2.97x slower
# JSON (using JrJackson): 11603.0 i/s - 34.08x slower
# YAML: 5030.7 i/s - 78.60x slower
# MRI 2.2.3
# $ bundle exec ruby serializer_benchmarks.rb
# MessagePack: 224382.1 i/s
# JSON (using Oj): 181642.9 i/s - 1.24x slower
# Marshal: 119823.9 i/s - 1.87x slower
# JSON (using Yajl): 92159.5 i/s - 2.43x slower
# JSON (built-in): 82719.3 i/s - 2.71x slower
# YAML: 3200.7 i/s - 70.10x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment