Created
July 30, 2010 08:48
-
-
Save deepak/500174 to your computer and use it in GitHub Desktop.
benchmark json generation and parsing in rails/ruby
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 | |
require 'benchmark' | |
require 'rubygems' | |
require 'nokogiri' | |
require 'yajl' | |
require 'active_support' | |
ActiveSupport::XmlMini.backend='Nokogiri' | |
require 'active_resource' | |
TIMES = 1000 | |
data_hash = ('a'..'zz').inject([]) {|acc, x| acc << {x => x.to_s } } | |
json_str = File.read('json_gen_file.json') | |
xml_str = File.read('json_gen_file.xml') | |
Benchmark.bmbm do |x| | |
x.report('nokogiri-generate') do | |
TIMES.times do | |
builder = Nokogiri::XML::Builder.new do |xml| | |
xml.test do | |
data_hash.each { |k,v| xml.tag!(k, v) } | |
end | |
end | |
builder.to_xml | |
end | |
end | |
x.report('yajl-generate') do | |
TIMES.times { Yajl::Encoder.encode(data_hash) } | |
end | |
x.report('activeresource-generate-xml') do | |
TIMES.times { ActiveResource::Formats::XmlFormat.encode(data_hash) } | |
end | |
x.report('activeresource-generate-json') do | |
TIMES.times { ActiveResource::Formats::JsonFormat.encode(data_hash) } | |
end | |
x.report('nokogiri-read') do | |
TIMES.times do | |
doc = Nokogiri::XML(xml_str) { |cfg| cfg.noblanks } | |
raise doc.errors.first if doc.errors.length > 0 | |
end | |
end | |
x.report('yajl-read') do | |
TIMES.times { Yajl::Parser.parse(json_str) } | |
end | |
x.report('activeresource-read-xml') do | |
TIMES.times { ActiveResource::Formats::XmlFormat.decode(xml_str) } | |
end | |
x.report('activeresource-read-json') do | |
TIMES.times { ActiveResource::Formats::JsonFormat.decode(json_str) } | |
end | |
end | |
__END__ | |
# ree 2010.01 with rails2.3.5 | |
user system total real | |
nokogiri-generate 21.810000 0.010000 21.820000 ( 21.811517) | |
yajl-generate 0.780000 0.000000 0.780000 ( 0.795478) | |
activeresource-generate-xml 66.590000 0.010000 66.600000 ( 66.590918) | |
activeresource-generate-json 25.300000 0.020000 25.320000 ( 25.311039) | |
nokogiri-read 1.500000 0.000000 1.500000 ( 1.491643) | |
yajl-read 1.270000 0.000000 1.270000 ( 1.276286) | |
activeresource-read-xml 184.660000 0.070000 184.730000 (184.748111) | |
activeresource-read-json 21.530000 0.000000 21.530000 ( 21.527788) | |
# ree 2010.01 with rails3-rc | |
nokogiri-generate 21.680000 0.040000 21.720000 ( 21.723874) | |
yajl-generate 0.780000 0.000000 0.780000 ( 0.780465) | |
activeresource-generate-xml 82.330000 0.100000 82.430000 ( 82.544486) | |
activeresource-generate-json 16.300000 0.000000 16.300000 ( 16.307495) | |
nokogiri-read 1.340000 0.000000 1.340000 ( 1.336867) | |
yajl-read 1.210000 0.010000 1.220000 ( 1.215114) | |
activeresource-read-xml 56.410000 0.040000 56.450000 ( 56.608312) | |
activeresource-read-json 1.240000 0.000000 1.240000 ( 1.261060) | |
# memory benchmark | |
# ree 2010.01 with rails2.3.5 | |
# with the default values for GC tuning | |
# RUBY_HEAP_MIN_SLOTS,RUBY_HEAP_SLOTS_INCREMENT, | |
# RUBY_HEAP_SLOTS_GROWTH_FACTOR, RUBY_GC_MALLOC_LIMIT | |
# have added, using http://github.com/brianmario/yajl-rails on rails 2.3.5 | |
# getting the stats by: | |
# GC.enable_stats | |
# #<code> | |
# GC.disable_stats | |
# puts GC.collections | |
# puts ObjectSpace.live_objects | |
user live_objects gc_cycles | |
nokogiri-generate 152166 5 | |
yajl-generate 156617 0 | |
activeresource-generate-xml 326285 5 | |
activeresource-generate-json-normal 197040 2 | |
activeresource-generate-json-yajl-rails 206439 2 | |
nokogiri-read 121409 0 | |
yajl-read 96135 1 | |
activeresource-read-xml 185695 8 | |
activeresource-read-json-normal 197820 3 | |
activeresource-read-json-yajl-rails 358533 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you post the json/xml files used as well?