Skip to content

Instantly share code, notes, and snippets.

@carakan
Last active June 18, 2022 02:46
Show Gist options
  • Save carakan/321e9e1daf771afa090fa95c65455378 to your computer and use it in GitHub Desktop.
Save carakan/321e9e1daf771afa090fa95c65455378 to your computer and use it in GitHub Desktop.
benchmarking json with openstruct
require 'benchmark/ips'
require 'json'
require 'oj'
require 'yajl'
require 'yajl/version'
puts 'SPECIFICATION',
"OS Name: #{`uname -a`}",
`sw_vers`,
"Ruby Version: #{`ruby -v`}",
"RubyGems Version: #{`gem -v`}",
"RVM Version: #{`asdf -v`}",
"json Version: #{JSON::VERSION}",
"oj Version: #{Oj::VERSION}",
"yajl-ruby Version: #{Yajl::VERSION}"
# Setup
small_load = File.read('./small.json')
small_dump = JSON.parse(File.read('./small.json'))
small_description = "\nSMALL JSON\n\tLOAD: #{small_load.length} length\n\tDUMP: #{small_dump.length} object\n\n"
medium_load = File.read('./medium.json')
medium_dump = JSON.parse(File.read('./medium.json'))
medium_description = "\nMEDIUM JSON\n\tLOAD: #{medium_load.length} length\n\tDUMP: #{medium_dump.length} objects\n\n"
big_load = File.read('./big.json')
big_dump = JSON.parse(File.read('./big.json'))
big_description = "\nBIG JSON\n\tLOAD: #{big_load.length} length\n\tDUMP: #{big_dump.length} objects\n\n"
time = 5
warmup = 2
puts "Benchamrk time: #{time}",
"Benchamrk warmup: #{warmup}"
def line(length: 49, style: '-', text: nil)
text.nil? ? style * length : text.ljust(length, style)
end
# Benchamrk
[
[:small, small_description, small_load, small_dump],
[:medium, medium_description, medium_load, medium_dump],
[:big, big_description, big_load, big_dump],
].each do |type, description, load, dump|
print line,
description,
line(text: 'LOAD ')
puts ''
Benchmark.ips(time, warmup) do |x|
x.report('json') do
JSON.parse(load.dup)
end
x.report('oj')do
Oj.load(load.dup)
end
x.report('json openstruct') do
JSON.parse(load.dup, object_class: OpenStruct)
end
x.report('oj openstruct')do
Oj.load(load.dup, mode: :compat, object_class: OpenStruct)
end
x.report('yajl')do
Yajl::Parser.new.parse(load.dup)
end
x.compare!
end
puts line(text: 'DUMP ')
Benchmark.ips(time, warmup) do |x|
x.report('json') do
JSON.dump(dump.dup)
end
x.report('oj')do
Oj.dump(dump.dup)
end
x.report('yajl')do
Yajl::Encoder.new.encode(dump.dup)
end
x.compare!
end
end
SPECIFICATION
OS Name: Darwin iMac 20.6.0 Darwin Kernel Version 20.6.0: Tue Apr 19 21:04:45 PDT 2022; root:xnu-7195.141.29~1/RELEASE_X86_64 x86_64
ProductName: macOS
ProductVersion: 11.6.7
BuildVersion: 20G630
Ruby Version: ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-darwin20]
RubyGems Version: 3.0.3
RVM Version:
json Version: 2.2.0
oj Version: 3.13.14
yajl-ruby Version: 1.4.1
Benchamrk time: 5
Benchamrk warmup: 2
-------------------------------------------------
SMALL JSON
LOAD: 5647 length
DUMP: 10 object
LOAD --------------------------------------------
Warming up --------------------------------------
raw json 1.118k i/100ms
raw oj 2.835k i/100ms
json openstruct 166.000 i/100ms
oj openstruct 185.000 i/100ms
yajl 1.401k i/100ms
Calculating -------------------------------------
raw json 10.950k (± 2.5%) i/s - 54.782k in 5.006053s
raw oj 26.979k (± 5.1%) i/s - 136.080k in 5.059664s
json openstruct 1.612k (± 3.0%) i/s - 8.134k in 5.049940s
oj openstruct 1.795k (± 3.0%) i/s - 9.065k in 5.054679s
yajl 13.842k (± 2.1%) i/s - 70.050k in 5.062897s
Comparison:
raw oj: 26979.3 i/s
yajl: 13842.0 i/s - 1.95x (± 0.00) slower
raw json: 10950.3 i/s - 2.46x (± 0.00) slower
oj openstruct: 1795.0 i/s - 15.03x (± 0.00) slower
json openstruct: 1612.2 i/s - 16.73x (± 0.00) slower
DUMP --------------------------------------------
Warming up --------------------------------------
json 1.424k i/100ms
oj 9.959k i/100ms
yajl 2.992k i/100ms
Calculating -------------------------------------
json 14.727k (± 6.9%) i/s - 74.048k in 5.055281s
oj 103.987k (± 3.2%) i/s - 527.827k in 5.081367s
yajl 32.773k (± 2.9%) i/s - 164.560k in 5.025615s
Comparison:
oj: 103987.3 i/s
yajl: 32773.5 i/s - 3.17x (± 0.00) slower
json: 14727.3 i/s - 7.06x (± 0.00) slower
-------------------------------------------------
MEDIUM JSON
LOAD: 28725 length
DUMP: 100 objects
LOAD --------------------------------------------
Warming up --------------------------------------
raw json 410.000 i/100ms
raw oj 846.000 i/100ms
json openstruct 65.000 i/100ms
oj openstruct 76.000 i/100ms
yajl 437.000 i/100ms
Calculating -------------------------------------
raw json 4.054k (± 2.1%) i/s - 20.500k in 5.059612s
raw oj 8.027k (± 7.4%) i/s - 40.608k in 5.089141s
json openstruct 657.591 (± 4.7%) i/s - 3.315k in 5.052444s
oj openstruct 727.872 (± 5.4%) i/s - 3.648k in 5.027373s
yajl 4.294k (± 3.4%) i/s - 21.850k in 5.094481s
Comparison:
raw oj: 8026.9 i/s
yajl: 4294.2 i/s - 1.87x (± 0.00) slower
raw json: 4053.6 i/s - 1.98x (± 0.00) slower
oj openstruct: 727.9 i/s - 11.03x (± 0.00) slower
json openstruct: 657.6 i/s - 12.21x (± 0.00) slower
DUMP --------------------------------------------
Warming up --------------------------------------
json 515.000 i/100ms
oj 2.016k i/100ms
yajl 861.000 i/100ms
Calculating -------------------------------------
json 5.184k (± 8.4%) i/s - 25.750k in 5.010375s
oj 19.347k (± 7.5%) i/s - 96.768k in 5.032190s
yajl 7.898k (± 6.5%) i/s - 39.606k in 5.036590s
Comparison:
oj: 19346.7 i/s
yajl: 7898.4 i/s - 2.45x (± 0.00) slower
json: 5183.7 i/s - 3.73x (± 0.00) slower
-------------------------------------------------
BIG JSON
LOAD: 1141477 length
DUMP: 5000 objects
LOAD --------------------------------------------
Warming up --------------------------------------
raw json 5.000 i/100ms
raw oj 11.000 i/100ms
json openstruct 1.000 i/100ms
oj openstruct 1.000 i/100ms
yajl 7.000 i/100ms
Calculating -------------------------------------
raw json 62.317 (± 8.0%) i/s - 310.000 in 5.005926s
raw oj 136.226 (± 8.1%) i/s - 682.000 in 5.041981s
json openstruct 9.504 (±21.0%) i/s - 46.000 in 5.053608s
oj openstruct 11.200 (±26.8%) i/s - 52.000 in 5.065371s
yajl 74.151 (± 9.4%) i/s - 371.000 in 5.042258s
Comparison:
raw oj: 136.2 i/s
yajl: 74.2 i/s - 1.84x (± 0.00) slower
raw json: 62.3 i/s - 2.19x (± 0.00) slower
oj openstruct: 11.2 i/s - 12.16x (± 0.00) slower
json openstruct: 9.5 i/s - 14.33x (± 0.00) slower
DUMP --------------------------------------------
Warming up --------------------------------------
json 10.000 i/100ms
oj 66.000 i/100ms
yajl 17.000 i/100ms
Calculating -------------------------------------
json 98.617 (± 6.1%) i/s - 500.000 in 5.088933s
oj 551.447 (±18.7%) i/s - 2.706k in 5.098413s
yajl 173.877 (± 6.9%) i/s - 867.000 in 5.009955s
Comparison:
oj: 551.4 i/s
yajl: 173.9 i/s - 3.17x (± 0.00) slower
json: 98.6 i/s - 5.59x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment