Skip to content

Instantly share code, notes, and snippets.

@billdueber
Created October 9, 2014 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billdueber/e375a35ebabd2de73616 to your computer and use it in GitHub Desktop.
Save billdueber/e375a35ebabd2de73616 to your computer and use it in GitHub Desktop.
Ruby MARC serialization / deserialization benchmark
require 'benchmark'
require 'marc'
require 'msgpack'
require 'json'
unless defined? JRUBY_VERSION
require 'oj'
end
jsonfile = 'topics.ndj'
marcfile = 'topics.mrc'
marshalfile = 'topics.msh'
msgpackfile = 'topics.msgpack'
tmpfile = 'tmpfile.delete'
records = MARC::Reader.new(marcfile).each_with_object([]) {|x,a| a << x}
puts "Got #{records.size} records"
puts "\n\n\n" + RUBY_DESCRIPTION
Benchmark.bmbm do |x|
x.report("load marc21") do
reader = MARC::Reader.new(marcfile)
total = 0
reader.each do |r|
total += 1
end
end
x.report("load json ") do
reader = File.open(jsonfile)
total = 0
reader.each do |json|
r = MARC::Record.new_from_hash(JSON.parse(json))
total += 1
end
end
unless defined? JRUBY_VERSION
x.report("load json (oj)") do
reader = File.open(jsonfile)
total = 0
reader.each do |json|
r = MARC::Record.new_from_hash(Oj.strict_load(json))
total += 1
end
end
end
x.report('load msgpack') do
reader = File.open(msgpackfile)
total = 0
MessagePack::Unpacker.new(reader).each do |h|
r = MARC::Record.new_from_hash(h)
total += 1
end
end
x.report("load marshal") do
infile = File.open(marshalfile)
total = 0
while !infile.eof?
r = Marshal.load(infile)
total += 1
end
end
x.report("write marc21") do
writer = MARC::Writer.new(tmpfile)
records.each {|r| writer.write(r)}
writer.close
end
x.report('write json') do
out = File.open(tmpfile, 'w:utf-8')
records.each {|r| out.puts JSON.dump(r.to_hash)}
out.close
end
unless defined? JRUBY_VERSION
x.report('write json (oj)') do
out = File.open(tmpfile, 'w:utf-8')
records.each {|r| Oj.to_stream(out, r.to_hash)}
out.close
end
end
x.report('write msgpack') do
out = File.open(tmpfile, 'wb')
records.each {|r| out.write MessagePack.dump(r.to_hash) }
out.close
end
x.report('write marshal') do
out = File.open(tmpfile, 'wb')
records.each {|r| Marshal.dump(r, out)}
out.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment