Skip to content

Instantly share code, notes, and snippets.

@deepak
Created July 30, 2010 08:48
Show Gist options
  • Save deepak/500174 to your computer and use it in GitHub Desktop.
Save deepak/500174 to your computer and use it in GitHub Desktop.
benchmark json generation and parsing in rails/ruby
#!/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
@deepak
Copy link
Author

deepak commented Aug 2, 2010

wanted to use memprof but was not working on the server, gives a segmentation fault:
http://github.com/ice799/memprof/issues/7

@thatdutchguy
Copy link

Could you post the json/xml files used as well?

@deepak
Copy link
Author

deepak commented May 21, 2011

@thatdutchguy the json files must be in my old laptop, i will try to find them but cannot promise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment