Skip to content

Instantly share code, notes, and snippets.

@zanker
Created November 10, 2015 21:44
Show Gist options
  • Save zanker/90fb9f5e316711f9618c to your computer and use it in GitHub Desktop.
Save zanker/90fb9f5e316711f9618c to your computer and use it in GitHub Desktop.
Benchmark of various Ruby protocol buffer libraries.
require 'benchmark'
require 'beefcake' # 1.1.0
require 'protocol_buffers' # 1.6.1
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
# beefcake
class BeefRow
include Beefcake::Message
optional :number, :int32, 1
optional :chars, :string, 2
end
class Beef
include Beefcake::Message
optional :number, :int32, 1
optional :chars, :string, 2
optional :bool, :bool, 4
optional :float, :float, 5
repeated :array, BeefRow, 6
end
# 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
# ruby-protocol-buffers
class ProtoBuffersRow < ProtocolBuffers::Message
optional :int32, :number, 1
optional :string, :chars, 2
end
class ProtoBuffers < ProtocolBuffers::Message
optional :int32, :number, 1
optional :string, :chars, 2
optional :bool, :bool, 4
optional :float, :float, 5
repeated ProtoBuffersRow, :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_beef
Test::Beef.new(
number: 1234,
chars: "ABC ABC ABC 1234",
bool: true,
float: 5.13,
array: 500.times.map do |i|
{ number: i * 9531, chars: "DEF ITA 915 ABC #{i}" }
end
).encode
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_protocol_buffers
model = Test::ProtoBuffers.new(
number: 4321,
chars: "AIT ABC ABC ABC 1234",
bool: true,
float: 5.12
)
500.times do |i|
model.array << Test::ProtoBuffersRow.new(number: i * 9537, chars: "TES ITA 915 ABC #{i}")
end
model.serialize_to_string
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("beefcake encoding") do
ITERATIONS.times do
encode_beef
end
end
x.report("protobuf encoding") do
ITERATIONS.times do
encode_protobuf
end
end
x.report("ruby-protocol-buffers encoding") do
ITERATIONS.times do
encode_protocol_buffers
end
end
x.report("google-protobuf encoding") do
ITERATIONS.times do
encode_google_protobuf
end
end
# Decoding
proto = encode_beef
x.report("beefcake decoding") do
ITERATIONS.times do
Test::Beef.decode(proto)
end
end
proto = encode_protobuf
x.report("protobuf decoding") do
ITERATIONS.times do
Test::Proto.decode(proto)
end
end
proto = encode_protocol_buffers
x.report("ruby-protocol-buffers decoding") do
ITERATIONS.times do
Test::ProtoBuffers.parse(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