Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created December 6, 2012 07:03
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 yuroyoro/4222358 to your computer and use it in GitHub Desktop.
Save yuroyoro/4222358 to your computer and use it in GitHub Desktop.
MultiJsonのBackendをyajlに変えたら素のjson.gemの2倍のパフォーマンスになった件
#!/usr/bin/env ruby
# rails r tools/json_bench.rb
require 'rbench'
ar_objects1 = # 適当なActiveRecordオブジェクト一個
json_str1 = ar_objects1.to_json
ar_objects2 = # 3個ほど子テーブルをincludeさせたActiveRecordオブジェクトを70個くらいの配列に
json_str2 = ar_objects2.to_json
puts ""
puts "-" * 80
puts "Benchmark of json encoding/decoding"
puts " json_gem vs yajl"
puts "-" * 80
puts ""
RBench.run(10000) do
column :times
column :one, :title => "json_gem"
column :two, :title => "yajl"
column :diff, :title => "json_gem/yajl", :compare => [:one,:two]
group 'Single ActiveRecord Object ' do
report 'encode' do
# ActiveSupport::JSON.encode
one {
MultiJson.engine = :json_gem unless MultiJson.engine.name == 'MultiJson::Adapters::JsonGem'
ar_objects1.to_json
}
# as_json and MultiJson.encode with yaji
two {
MultiJson.engine = :yajl unless MultiJson.engine == MultiJson::Adapters::Yajl
MultiJson.encode(ar_objects1.as_json)
}
end
report 'decode' do
# ActiveSupport::JSON.decode with json_gem
one {
MultiJson.engine = :json_gem unless MultiJson.engine == MultiJson::Adapters::JsonGem
ActiveSupport::JSON.decode(json_str1)
}
# ActiveSupport::JSON.decode with yaji
two {
MultiJson.engine = :yajl unless MultiJson.engine == MultiJson::Adapters::Yajl
ActiveSupport::JSON.decode(json_str1)
}
end
end
group 'Array ActiveRecord Objects' do
report 'encode' do
# ActiveSupport::JSON.encode
one {
MultiJson.engine = :json_gem unless MultiJson.engine == MultiJson::Adapters::JsonGem
ar_objects2.to_json
}
# as_json and MultiJson.encode with yaji
two {
MultiJson.engine = :yajl unless MultiJson.engine.name == 'MultiJson::Adapters::Yajl'
MultiJson.encode(ar_objects2.as_json)
}
end
report 'decode' do
# ActiveSupport::JSON.decode with json_gem
one {
MultiJson.engine = :json_gem unless MultiJson.engine.name == 'MultiJson::Adapters::JsonGem'
ActiveSupport::JSON.decode(json_str2)
}
# ActiveSupport::JSON.decode with yaji
two {
MultiJson.engine = :yajl unless MultiJson.engine.name == 'MultiJson::Adapters::Yajl'
ActiveSupport::JSON.decode(json_str2)
}
end
end
end
--------------------------------------------------------------------------------
Benchmark of json encoding/decoding
json_gem vs yajl
--------------------------------------------------------------------------------
| json_gem | yajl | json_gem/yajl |
--Single ActiveRecord Object -----------------------------------------------------
encode x10000 | 12.130 | 6.167 | 1.97x |
decode x10000 | 1.085 | 0.437 | 2.48x |
--Array ActiveRecord Objects-----------------------------------------------------
encode x10000 | 508.319 | 225.235 | 2.26x |
decode x10000 | 39.069 | 19.869 | 1.97x |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment