Created
December 6, 2012 07:03
-
-
Save yuroyoro/4222358 to your computer and use it in GitHub Desktop.
MultiJsonのBackendをyajlに変えたら素のjson.gemの2倍のパフォーマンスになった件
This file contains 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 | |
# 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 |
This file contains 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
-------------------------------------------------------------------------------- | |
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