介绍两个Erlang处理google protobuff的库。

介绍

两个项目都是Erlang的probobuf打包和解包用的,不过gpb会比较新点。

erlang_protobuffs的项目地址是https://github.com/basho/erlang_protobuffs

gpb的项目地址是https://github.com/tomas-abrahamsson/gpb

gpb本身提供benchmarks脚本,我在这个基础加了下erlang_protobuffs的测试,这样就可以对比两者的性能了。

测试

将erlang_protobuffs clone到和gpb同一级的目录,然后进去make,这样erlang_protobuffs就可以了。

将benchmarks目录下的Makefile改成其他名字,下载https://gist.github.com/roowe/0031a0f3a7dff89a7c73

然后make,接着将_pb.erl代码找个合适地方添加

-export([encode_msg/1, decode_msg/2]).

encode_msg(R) ->
    encode(R).
decode_msg(Bin, Name) ->
    decode(Name, Bin).

重新make下

proto-bench添加 -pa ../../erlang_protobuffs/ebin/
然后 make benchmarks即可

测试结果

Benchmarking msg Message1 with file google_message1.dat
Serialize to binary 228 bytes: 5367843 iterations in 31.261s; 37.34MB/s
Deserialize from binary 228 bytes: 6643649 iterations in 30.031s; 48.10MB/s

Benchmarking msg Message2 with file google_message2.dat
Serialize to binary 84584 bytes: 7636 iterations in 31.052s; 19.84MB/s
Deserialize from binary 84584 bytes: 10549 iterations in 28.969s; 29.37MB/s

Benchmarking msg_pb message1 with file google_message1.dat
Serialize to binary 228 bytes: 3546416 iterations in 30.788s; 25.05MB/s
Deserialize from binary 228 bytes: 643662 iterations in 28.783s; 4.86MB/s

Benchmarking msg_pb message2 with file google_message2.dat
Serialize to binary 84584 bytes: 5718 iterations in 30.717s; 15.02MB/s
Deserialize from binary 84584 bytes: 2123 iterations in 29.735s; 5.76MB/s

结论

gpb胜出,erlang_protobuffs的Deserialize比Serialize慢多了。