测试代码

根据https://github.com/tomas-abrahamsson/gpb/tree/master/benchmarks 做了相应的修改。

测试代码地址,https://gitcafe.com/Roowe/term_serialization_benchmarks

测试环境

Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
Erlang/OTP 18 [erts-7.0]

测试

git clone https://gitcafe.com/Roowe/term_serialization_benchmarks
cd term_serialization_benchmarks
make ebin
erlc -o ebin src/*
 ./term_serialization_bench term_0 data/term_1_0.dat term_0 data/term_2_0.dat term_6 data/term_1_6.dat term_6 data/term_2_6.dat  term_9 data/term_1_9.dat term_9 data/term_2_9.dat

测试输出

Benchmarking term_0 with file data/term_1_0.dat
Serialize to binary 783 byte: 10258155 iterations in 28.608s; 267.75MB/s
Deserialize from binary 783 byte: 4293362 iterations in 33.414s; 95.95MB/s

Benchmarking term_0 with file data/term_2_0.dat
Serialize to binary 350911 byte: 22443 iterations in 28.046s; 267.80MB/s
Deserialize from binary 350911 byte: 10157 iterations in 32.055s; 106.04MB/s

Benchmarking term_6 with file data/term_1_6.dat
Serialize to binary 311 byte: 1346787 iterations in 31.552s; 12.66MB/s
Deserialize from binary 311 byte: 3316697 iterations in 31.315s; 31.41MB/s

Benchmarking term_6 with file data/term_2_6.dat
Serialize to binary 77222 byte: 4472 iterations in 28.550s; 11.54MB/s
Deserialize from binary 77222 byte: 8794 iterations in 30.763s; 21.05MB/s

Benchmarking term_9 with file data/term_1_9.dat
Serialize to binary 311 byte: 1302531 iterations in 30.289s; 12.75MB/s
Deserialize from binary 311 byte: 3020752 iterations in 27.687s; 32.36MB/s

Benchmarking term_9 with file data/term_2_9.dat
Serialize to binary 75818 byte: 1195 iterations in 29.628s; 2.92MB/s
Deserialize from binary 75818 byte: 8923 iterations in 30.438s; 21.20MB/s

测试结论

无压缩

  • 小包 783 byte,Serialize 267.75MB/s,Deserialize 95.95MB/s
  • 大包 350911 byte,Serialize 267.80MB/s,Deserialize 106.04MB/s

默认压缩(6)

  • 小包 311 byte,Serialize 12.66MB/s,Deserialize 31.41MB/s
  • 大包 77222 byte,Serialize 11.54MB/s,Deserialize 21.05MB/s

极端压缩(9)

  • 小包 311 byte,Serialize 12.75MB/s,Deserialize 32.36MB/s
  • 大包 75818 byte,Serialize 2.92MB/s,Deserialize 21.20MB/s

不带压缩的序列化和反序列化,性能非常的高,但是相比protobuf的话,占的空间也会比较大,测试数据probobuf对应的小包是228 byte,大包是84584 byte。protobuf的大小跟极端压缩之后的差别不大,不过Erlang支持更复杂的数据结构,压缩得很严重之后,效率也降得很厉害。

所以,Erlang也一直在强调,小消息大计算。大消息的话,在Erlang并不什么划算的事情。

所以,term_to_binary来做广域网的通信协议,并不是一件很合适的事情,相同的数据,被序列化之后,并没有比protobuf小,而且另外一端非Erlang的话,还要实现其对应的解析。但是局域网集群的话,包的大小就不是很重要,当然前提不是特别大的话。