Skip to content

Instantly share code, notes, and snippets.

@viniciusteles
Created November 4, 2012 16:19
Show Gist options
  • Save viniciusteles/4012466 to your computer and use it in GitHub Desktop.
Save viniciusteles/4012466 to your computer and use it in GitHub Desktop.
Picasa API access benchmarks
require 'rubygems'
require 'benchmark'
require 'httparty'
@picasa_user = '117499753694301708125'
class XmlFetcher
include HTTParty
format :xml
end
class JsonFetcher
include HTTParty
format :json
end
def fetch_albums(format = :xml, headers = {}, &block)
params = "&alt=json" if format == :json
headers.merge!({ "GData-Version" => "2" })
url = "http://picasaweb.google.com/data/feed/api/user/#{@picasa_user}?v=2#{params}"
if block_given?
yield(url, headers)
else
HTTParty.get(url, :headers => headers)
end
end
def fetch_albums_with_gzip(format = :xml)
fetch_albums(format, { "Accept-Encoding" => "gzip, deflate", "User-Agent" => "bench (gzip)" })
end
puts "Measuring fething only (no parsing involved)\n"
Benchmark.bm(10) do |bench|
bench.report("plain (xml) ") { @plain_xml = fetch_albums }
bench.report("plain (json)") { @plain_json = fetch_albums(:json) }
bench.report("gzip (xml) ") { @gzip_xml = fetch_albums_with_gzip }
bench.report("gzip (json)") { @gzip_json = fetch_albums_with_gzip(:json) }
end
puts "\nMeasuse parsing only (no fetching involved)\n"
@xml = @gzip_xml.body
@json = @gzip_json.body
Benchmark.bm(10) do |bench|
bench.report("XML (nokogiri) ") { MultiXml.parser = :nokogiri; MultiXml.parse(@xml) }
bench.report("XML (ox) ") { MultiXml.parser = :ox; MultiXml.parse(@xml) }
bench.report("JSON (json_pure)") { MultiJson.use :json_pure; MultiJson.load(@json) }
bench.report("JSON (yajl) ") { MultiJson.use :yajl; MultiJson.load(@json) }
bench.report("JSON (oj) ") { MultiJson.use :oj; MultiJson.load(@json) }
end
puts "\nMeasure fetching and parsing, all handled by HTTParty\n"
Benchmark.bm(10) do |bench|
bench.report("plain XML (nokogiri) ") { MultiXml.parser = :nokogiri; fetch_albums { |url, headers| XmlFetcher.get(url, :headers => headers).parsed_response } }
bench.report("plain XML (ox) ") { MultiXml.parser = :ox; fetch_albums { |url, headers| XmlFetcher.get(url, :headers => headers).parsed_response } }
bench.report("plain JSON (json_pure)") { MultiJson.use :json_pure; fetch_albums(:json) { |url, headers| JsonFetcher.get(url, :headers => headers).parsed_response } }
bench.report("plain JSON (yajl) ") { MultiJson.use :yajl; fetch_albums(:json) { |url, headers| JsonFetcher.get(url, :headers => headers).parsed_response } }
bench.report("plain JSON (oj) ") { MultiJson.use :oj; fetch_albums(:json) { |url, headers| JsonFetcher.get(url, :headers => headers).parsed_response } }
bench.report("gzip XML (nokogiri) ") { MultiXml.parser = :nokogiri; fetch_albums_with_gzip { |url, headers| XmlFetcher.get(url, :headers => headers).parsed_response } }
bench.report("gzip XML (ox) ") { MultiXml.parser = :ox; fetch_albums_with_gzip { |url, headers| XmlFetcher.get(url, :headers => headers).parsed_response } }
bench.report("gzip JSON (json_pure)") { MultiJson.use :json_pure; fetch_albums_with_gzip(:json) { |url, headers| JsonFetcher.get(url, :headers => headers).parsed_response } }
bench.report("gzip JSON (yajl) ") { MultiJson.use :yajl; fetch_albums_with_gzip(:json) { |url, headers| JsonFetcher.get(url, :headers => headers).parsed_response } }
bench.report("gzip JSON (oj) ") { MultiJson.use :oj; fetch_albums_with_gzip(:json) { |url, headers| JsonFetcher.get(url, :headers => headers).parsed_response } }
end
Results on a Linode box
Measuring fething only (no parsing involved)
user system total real
plain (xml) 0.090000 0.050000 0.140000 ( 2.660800)
plain (json) 0.100000 0.050000 0.150000 ( 2.549367)
gzip (xml) 0.040000 0.020000 0.060000 ( 0.680799)
gzip (json) 0.030000 0.010000 0.040000 ( 0.648499)
Measuse parsing only (no fetching involved)
user system total real
XML (nokogiri) 2.590000 0.210000 2.800000 ( 2.845596)
XML (ox) 1.390000 0.010000 1.400000 ( 1.392543)
JSON (json_pure) 5.250000 0.010000 5.260000 ( 5.303357)
JSON (yajl) 0.220000 0.000000 0.220000 ( 0.239877)
JSON (oj) 0.410000 0.020000 0.430000 ( 0.435273)
Measure fetching and parsing, all handled by HTTParty
user system total real
plain XML (nokogiri) 1.920000 0.050000 1.970000 ( 4.067620)
plain XML (ox) 0.810000 0.010000 0.820000 ( 2.640043)
plain JSON (json_pure) 2.870000 0.030000 2.900000 ( 4.922784)
plain JSON (yajl) 0.310000 0.010000 0.320000 ( 2.494792)
plain JSON (oj) 0.230000 0.030000 0.260000 ( 2.122090)
gzip XML (nokogiri) 0.040000 0.000000 0.040000 ( 0.691813)
gzip XML (ox) 0.040000 0.010000 0.050000 ( 0.624246)
gzip JSON (json_pure) 0.030000 0.000000 0.030000 ( 0.790274)
gzip JSON (yajl) 0.020000 0.010000 0.030000 ( 0.703493)
gzip JSON (oj) 0.040000 0.000000 0.040000 ( 0.621552)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment