$:.unshift 'lib' require 'marc4j4r' require 'benchmark' # require 'yajl' # require 'json/pure' # require 'msgpack' jsonsize = 0.0 marcsize = 0.0 mpsize = 0.0 # Use Benchmark.measure # sjptime = Benchmark::Tms.new(0,0,0,0,0, "JSON Pure") smtime = Benchmark::Tms.new(0,0,0,0,0, "MARC") smptime = Benchmark::Tms.new(0,0,0,0,0, "Msgpack") sjptime = Benchmark::Tms.new(0,0,0,0,0, "JSON") djptime = Benchmark::Tms.new(0,0,0,0,0, "JSON") dmtime = Benchmark::Tms.new(0,0,0,0,0, "MARC") dmptime = Benchmark::Tms.new(0,0,0,0,0, "Msgpack") i = 0 iterations = 1 iterations.times do reader = MARC4J4R::Reader.new('topics.xml', :marcxml) reader.each_with_index do |r, i| marc = nil json = nil mp = nil copy = nil smtime += Benchmark.measure {marc = r.to_marc} dmtime += Benchmark.measure {copy = MARC4J4R::Record.from_string(marc)} sjptime += Benchmark.measure {json = r.to_marc_in_json} djptime += Benchmark.measure {copy = MARC4J4R::Record.new_from_marc_in_json(json)} # break if i > 1000 end end puts "Total of #{i} records run #{iterations} times" puts "\nSERIALIZING" base = smtime.total puts ' %-15s %8.2f s (%3.0f%%)' % ['MARC', smtime.total, smtime.total / base * 100] puts ' %-15s %8.2f s (%3.0f%%)' % ['Json', sjptime.total, sjptime.total / base * 100] # puts ' %-15s %8.2f s (%3.0f%%)' % ['Msgpack', smptime.total, smptime.total / base * 100] base = dmtime.total puts "\nDESERIALIZING" puts ' %-15s %8.2f s (%3.0f%%)' % ['MARC', dmtime.total, dmtime.total / base * 100] puts ' %-15s %8.2f s (%3.0f%%)' % ['Json', djptime.total, djptime.total / base * 100] # puts ' %-15s %8.2f s (%3.0f%%)' % ['Msgpack', dmptime.total, dmptime.total / base * 100] base = dmtime.total + smtime.total puts "\nSERIALIZE + DESERIALIZE" puts ' %-15s %8.2f s (%3.0f%%)' % ['MARC', dmtime.total + smtime.total, (dmtime.total + smtime.total) / base * 100] puts ' %-15s %8.2f s (%3.0f%%)' % ['Json', djptime.total + sjptime.total, (djptime.total + sjptime.total) / base * 100] # puts ' %-15s %8.2f s (%3.0f%%)' % ['Msgpack', dmptime.total + smptime.total, (dmptime.total + smptime.total) / base * 100]