简要记述一下在c++环境下protobuf、thrift与avro序列化性能结果。
测试方法:分别用三种协议定义一个包含同样字段的数据结构,然后重复调用多次,比较三者之间花费的时间。具体定义的数据结构为:
- Person {
- int id;
- string name;
- string email;
- }
测试环境:一台两年前购置的Mac电脑(具体配置有待补充)。
测试结果:迭代次数分别为:1、10000、100000(thrift使用了两种编码方式:binary和compact)
- [avro encode] times: 1, milli: 0.046078
- [avro decode] times: 1, milli: 0.030119
- [avro encode] times: 10000, milli: 21.9206
- [avro decode] times: 10000, milli: 15.3672
- [protobuf encode] times: 1, milli: 0.023208
- [protobuf decode] times: 1, milli: 0.007489
- [protobuf encode] times: 10000, milli: 12.4098
- [protobuf decode] times: 10000, milli: 8.18684
- [thrift binary encode] times: 1, milli: 0.042721
- [thrift binary decode] times: 1, milli: 0.012054
- [thrift binary encode] times: 10000, milli: 22.8659
- [thrift binary decode] times: 10000, milli: 25.6858
- [thrift compact encode] times: 1, milli: 0.055757
- [thrift compact decode] times: 1, milli: 0.02131
- [thrift compact encode] times: 10000, milli: 29.9698
- [thrift compact decode] times: 10000, milli: 38.1937
- [avro encode] times: 1, milli: 0.038088
- [avro decode] times: 1, milli: 0.021185
- [avro encode] times: 100000, milli: 219.917
- [avro decode] times: 100000, milli: 167.694
- [protobuf encode] times: 1, milli: 0.04218
- [protobuf decode] times: 1, milli: 0.01303
- [protobuf encode] times: 100000, milli: 98.2691
- [protobuf decode] times: 100000, milli: 74.7578
- [thrift binary encode] times: 1, milli: 0.046932
- [thrift binary decode] times: 1, milli: 0.015221
- [thrift binary encode] times: 100000, milli: 232.316
- [thrift binary decode] times: 100000, milli: 266.397
- [thrift compact encode] times: 1, milli: 0.034175
- [thrift compact decode] times: 1, milli: 0.013485
- [thrift compact encode] times: 100000, milli: 307.872
- [thrift compact decode] times: 100000, milli: 381.324
从以上结果可以看出:protobuf表现优异,avro次之,thrift表现总体较差(binary比compact好些)。
结束语:以上结果仅供参考,实际选用要结合项目的具体情况。如需要性能绝对优先,那就应该选择protobuf;否则需要支持多种开发语言则应该选择thrift;avro的动态特性似乎也颇具吸引力。