Skip to content

Instantly share code, notes, and snippets.

@zanker
Last active November 10, 2015 21:51
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 zanker/6002476f5a957d79d279 to your computer and use it in GitHub Desktop.
Save zanker/6002476f5a957d79d279 to your computer and use it in GitHub Desktop.
Benchmark results of protobuf vs google-protobuf gems
=begin
Decoding a large proto 1,000 times.
Ruby 2.2.3
user system total real
protobuf encoding 13.060000 0.180000 13.240000 ( 13.262584)
google-protobuf encoding 1.780000 0.050000 1.830000 ( 1.823272)
protobuf decoding 8.890000 0.030000 8.920000 ( 8.927971)
google-protobuf decoding 1.000000 0.030000 1.030000 ( 1.029513)
=end
require 'benchmark'
require 'protobuf' # 3.5.5
require 'protobuf/message'
require 'google/protobuf' # 3.0.0.alpha.4.0
require 'varint/varint' # 0.1.1
module Test
# ruby-protobuf
class ProtoRow < ::Protobuf::Message; end
class Proto < ::Protobuf::Message; end
class ProtoRow
optional :int32, :number, 1
optional :string, :chars, 2
end
class Proto
optional :int32, :number, 1
optional :string, :chars, 2
optional :bool, :bool, 4
optional :float, :float, 5
repeated ProtoRow, :array, 6
end
# google-protobuf
pool = Google::Protobuf::DescriptorPool.new
pool.build do
add_message "GoogleProtoRow" do
optional :number, :int32, 1
optional :chars, :string, 2
end
add_message "GoogleProto" do
optional :number, :int32, 1
optional :chars, :string, 2
optional :bool, :bool, 4
optional :float, :float, 5
repeated :array, :message, 6, "GoogleProtoRow"
end
end
GoogleProtoRow = pool.lookup("GoogleProtoRow").msgclass
GoogleProto = pool.lookup("GoogleProto").msgclass
end
def encode_protobuf
Test::Proto.new(
number: 4321,
chars: "DYZ ABC ABC ABC 1234",
bool: true,
float: 5.12,
array: 500.times.map do |i|
{ number: i * 9530, chars: "XYZ ITA 915 ABC #{i}" }
end
).encode
end
def encode_google_protobuf
Test::GoogleProto.encode(
Test::GoogleProto.new(
number: 4322,
chars: "EST ABC ABC ABC 1234",
bool: true,
float: 5.12,
array: 500.times.map do |i|
Test::GoogleProtoRow.new(number: i * 9536, chars: "KAJ ITA 915 ABC #{i}")
end
)
)
end
ITERATIONS = 1000
Benchmark.bm do |x|
# Encoding
x.report("protobuf encoding") do
ITERATIONS.times do
encode_protobuf
end
end
x.report("google-protobuf encoding") do
ITERATIONS.times do
encode_google_protobuf
end
end
# Decoding
proto = encode_protobuf
x.report("protobuf decoding") do
ITERATIONS.times do
Test::Proto.decode(proto)
end
end
proto = encode_google_protobuf
x.report("google-protobuf decoding") do
ITERATIONS.times do
Test::GoogleProto.decode(proto)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment