Skip to content

Instantly share code, notes, and snippets.

@skippy
Created May 3, 2015 18:00
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 skippy/93d6c51947960260ab63 to your computer and use it in GitHub Desktop.
Save skippy/93d6c51947960260ab63 to your computer and use it in GitHub Desktop.
testing various json parsing options
require 'benchmark'
require 'google/protobuf'
pool = Google::Protobuf::DescriptorPool.new
pool.build do
add_message "TestMessage" do
optional :optional_int32, :int32, 1
optional :optional_int64, :int64, 2
optional :optional_uint32, :uint32, 3
optional :optional_uint64, :uint64, 4
optional :optional_bool, :bool, 5
optional :optional_float, :float, 6
optional :optional_double, :double, 7
optional :optional_string, :string, 8
optional :optional_bytes, :bytes, 9
optional :optional_msg, :message, 10, "TestMessage2"
repeated :repeated_int32, :int32, 12
repeated :repeated_int64, :int64, 13
repeated :repeated_uint32, :uint32, 14
repeated :repeated_uint64, :uint64, 15
repeated :repeated_bool, :bool, 16
repeated :repeated_float, :float, 17
repeated :repeated_double, :double, 18
repeated :repeated_string, :string, 19
repeated :repeated_bytes, :bytes, 20
repeated :repeated_msg, :message, 21, "TestMessage2"
end
add_message "TestMessage2" do
optional :foo, :int32, 1
end
end
TestMessage = pool.lookup("TestMessage").msgclass
TestMessage2 = pool.lookup("TestMessage2").msgclass
tm = TestMessage.new
tm.repeated_string << 'ok1'
tm.repeated_string << 'ok2'
tm.optional_string = 'another string'
tm.optional_int32 = 102
raw_json = TestMessage.encode_json(tm)
raw_proto = TestMessage.encode(tm)
iters = 100_000
Benchmark.bm(12) do |b|
b.report('.encode'){ iters.times{ TestMessage.encode(tm) } }
b.report('.encode_json'){ iters.times{ TestMessage.encode_json(tm) } }
b.report('.decode'){ iters.times{ TestMessage.decode(raw_proto) } }
# this is broken on master when running jruby; so skipping
# b.report('.decode_json'){ TestMessage.decode_json(raw_json) }
end; nil
# MRI 2.2.0
#
# using stdlib JSON; gem JSON returned comperable results
user system total real
.encode 0.180000 0.000000 0.180000 ( 0.178334)
.encode_json 4.010000 0.010000 4.020000 ( 4.063277)
.decode 0.870000 0.010000 0.880000 ( 0.889392)
# using Yajl
user system total real
.encode 0.170000 0.000000 0.170000 ( 0.173894)
.encode_json 3.410000 0.030000 3.440000 ( 3.486852)
.decode 0.900000 0.010000 0.910000 ( 0.919666)
# using upb (default)
.encode 0.210000 0.010000 0.220000 ( 0.208851)
.encode_json 0.380000 0.000000 0.380000 ( 0.390611)
.decode 0.920000 0.020000 0.940000 ( 0.945698)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment