Skip to content

Instantly share code, notes, and snippets.

@ajturner
Forked from kyledrake/gist:4956808
Created February 17, 2013 20:52
Show Gist options
  • Save ajturner/4973393 to your computer and use it in GitHub Desktop.
Save ajturner/4973393 to your computer and use it in GitHub Desktop.
Added Avro and total file output
require 'json'
require 'bson'
require 'msgpack'
require 'benchmark'
require 'avro'
#require 'ruby_protobuf'
test_hash = {'string' => 'test string', 'float' => 5}
file = File.open("test.json", 'wb')
json_results = Benchmark.measure do
100_000.times do
file << @test_one = test_hash.to_json
end
end
file.close
puts "JSON: #{json_results}"
puts "JSON Feature Length: #{@test_one.bytesize}"
file = File.open("test.bson", 'wb')
bson_results = Benchmark.measure do
100_000.times do
file << @test_one = BSON.serialize(test_hash).to_s
end
end
file.close
puts "BSON: #{bson_results}"
puts "BSON Feature Length: #{@test_one.bytesize}"
file = File.open("test.msgpack", 'wb')
msgpack_results = Benchmark.measure do
100_000.times do
file << @test_one = test_hash.to_msgpack
end
end
file.close
puts "MessagePack: #{msgpack_results}"
puts "JSON Feature Length: #{@test_one.bytesize}"
file = File.open("test.avr", 'wb')
avro_results = Benchmark.measure do
schema = Avro::Schema.parse({ "type" => "record",
"name" => "test",
"fields" => [
{"name" => "string", "type" => "string"},
{"name" => "float", "type" => "float"}]}.to_json)
writer = Avro::IO::DatumWriter.new(schema)
dw = Avro::DataFile::Writer.new(file, writer, schema)
100_000.times do
dw << test_hash
end
dw.close
end
puts "Avro: #{avro_results}"
# puts "Avro Feature Length: #{@test_one.bytesize}"
# puts "Avro Collection Length: #{@test_all.bytesize}"
# puts "Avro Length: #{dw.last.bytesize}"
=begin
JSON: 1.000000 0.000000 1.000000 ( 1.007621)
BSON: 0.590000 0.000000 0.590000 ( 0.587634)
MessagePack: 0.160000 0.010000 0.170000 ( 0.169203)
Avro: 2.500000 0.010000 2.510000 ( 2.501266)
JSON Length: 34
BSON Length: 40
MessagePack Length: 27
Files:
test.json 3.2M
test.bson 3.8M
test.msgpack 2.6M
test.avr 1.5M
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment