Skip to content

Instantly share code, notes, and snippets.

@kyledrake
Created February 14, 2013 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kyledrake/4956808 to your computer and use it in GitHub Desktop.
Save kyledrake/4956808 to your computer and use it in GitHub Desktop.
Testing different binary protocol implementations
require 'json'
require 'bson'
require 'msgpack'
require 'benchmark'
#require 'ruby_protobuf'
test_hash = {'string' => 'test string', 'float' => 5}
json_results = Benchmark.measure do
100_000.times do
@test_one = test_hash.to_json
end
end
puts "JSON: #{json_results}"
puts "JSON Length: #{@test_one.bytesize}"
bson_results = Benchmark.measure do
100_000.times do
@test_one = BSON.serialize(test_hash).to_s
end
end
puts "BSON: #{bson_results}"
puts "BSON Length: #{@test_one.bytesize}"
msgpack_results = Benchmark.measure do
100_000.times do
@test_one = test_hash.to_msgpack
end
end
puts "MessagePack: #{msgpack_results}"
puts "MessagePack Length: #{@test_one.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)
JSON Length: 34
BSON Length: 40
MessagePack Length: 27
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment