FlatBuffers发布时,顺便也公布了它的性能数据,具体数据请见Benchmark。
它的测试用例由以下数据构成"a set of about 10 objects containing an array, 4 strings, and a large variety of int/float scalar values of all sizes, meant to be representative of game data, e.g. a scene format."
我感觉这样测试如同儿戏,便自己设计了一个测试用例,主要关注CPU计算时间和内存空间占用两个指标,参考对象是protobuf。
测试用例为:序列化一个通讯录personal_info_list(table),通讯录可以认为是有每个人的信息(personal_info)的集合。每个人信息personal_info(table)有:个人id(uint)、名字(string)、年龄(byte)、性别(enum, byte)和电话号码(ulong)。本来我想用struct表示personal_info(table),但是struct不允许有数组或string成员,无奈我用table描述它了。相应的idl文件如下:
01 |
////////////////////////////////////////////////////// |
02 |
////
FILE : tellist.fbs |
03 |
////
DESC : basic message for msg-center |
04 |
////
AUTHOR : v 0.1 written by Alex Stocks on June 22, 2014 |
05 |
////
LICENCE : |
06 |
////
MOD : |
07 |
//////////////////////////////////////////////////////// |
08 |
09 |
namespace as.tellist; |
10 |
11 |
enum GENDER_TYPE
: byte |
12 |
{ |
13 |
MALE
= 0, |
14 |
FEMALE
= 1, |
15 |
OTHER
= 2 |
16 |
} |
17 |
18 |
table
personal_info |
19 |
{ |
20 |
id
: uint; |
21 |
name
: string; |
22 |
age
: byte; |
23 |
gender
: GENDER_TYPE; |
24 |
phone_num
: ulong; |
25 |
} |
26 |
27 |
table
personal_info_list |
28 |
{ |
29 |
info
: [personal_info]; |
30 |
} |
31 |
32 |
root_type
personal_info_list; |
因为要以protobuf做性能参考,列出protobuf的idl文件如下:
01 |
////////////////////////////////////////////////////// |
02 |
////
FILE : tellist.proto |
03 |
////
DESC : basic message for msg-center |
04 |
////
AUTHOR : v 0.1 written by Alex Stocks on June 22, 2014 |
05 |
////
LICENCE : |
06 |
////
MOD : |
07 |
//////////////////////////////////////////////////////// |
08 |
09 |
package
as.tellist; |
10 |
11 |
enum gender_type |
12 |
{ |
13 |
MALE
= 0; |
14 |
FEMALE
= 1; |
15 |
OTHER
= 2; |
16 |
} |
17 |
18 |
message
personal_info |
19 |
{ |
20 |
optional
uint32 id = 1; |
21 |
optional
string name = 2; |
22 |
optional
uint32 age = 3; |
23 |
optional
gender_type gender = 4; |
24 |
optional
uint64 phone_num = 5; |
25 |
} |
26 |
27 |
message
personal_info_list |
28 |
{ |
29 |
repeated
personal_info info = 1; |
30 |
} |
在内存中构造37个personal_info对象,并序列化之,重复这个过程100万次。
测试结果如下:
01 |
测试环境:12Core
Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz |
02 |
free |
03 |
total
used free shared buffers cached |
04 |
Mem:
66081944 65831100 250844 0 182240 46903452 |
05 |
-/+
buffers/cache: 18745408 47336536 |
06 |
Swap:
975864 724648 251216 |
07 |
08 |
protobuf三次测试结果: |
09 |
./bin/tellist_test |
10 |
loop
= 1000000, time diff = 14283ms |
11 |
buf
size:841 |
12 |
13 |
bin/tellist_test |
14 |
loop
= 1000000, time diff = 14096ms |
15 |
buf
size:841 |
16 |
17 |
bin/tellist_test |
18 |
loop
= 1000000, time diff = 14229ms |
19 |
buf
size:841 |
20 |
占用内存空间841Byte,平均运算时间42608ms
/ 3 = 14202.7ms |
21 |
22 |
flatbuffers三次测试结果: |
23 |
bin/tellist_test |
24 |
loop
= 1000000, time diff = 11694ms |
25 |
buf
size:1712 |
26 |
27 |
bin/tellist_test |
28 |
loop
= 1000000, time diff = 11710ms |
29 |
buf
size:1712 |
30 |
31 |
bin/tellist_test |
32 |
loop
= 1000000, time diff = 11774ms |
33 |
buf
size:1712 |
34 |
占用内存空间1712Byte,平均运算时间35178ms
/ 3 = 11726ms |
35 |
36 |
37 |
测试环境:1
Core Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz |
38 |
MEM |
39 |
total
used free shared buffers cached |
40 |
Mem:
753932 432672 321260 0 89236 258052 |
41 |
-/+
buffers/cache: 85384 668548 |
42 |
Swap:
1324028 0 1324028 |
43 |
44 |
protobuf三次测试结果: |
45 |
bin/tellist_test |
46 |
loop
= 1000000, time diff = 12779ms |
47 |
buf
size:841 |
48 |
49 |
bin/tellist_test |
50 |
loop
= 1000000, time diff = 13475ms |
51 |
buf
size:841 |
52 |
53 |
bin/tellist_test |
54 |
loop
= 1000000, time diff = 12604ms |
55 |
buf
size:841 |
56 |
占用内存空间841Byte,平均运算时间38858ms
/ 3 = 12952.7ms |
57 |
58 |
flatbuffers三次测试结果: |
59 |
bin/tellist_test |
60 |
loop
= 1000000, time diff = 9424ms |
61 |
buf
size:1712 |
62 |
63 |
bin/tellist_test |
64 |
loop
= 1000000, time diff = 9277ms |
65 |
buf
size:1712 |
66 |
67 |
bin/tellist_test |
68 |
loop
= 1000000, time diff = 9265ms |
69 |
buf
size:1712 |
70 |
info
vecotor size:37, its right size:37 |
71 |
占用内存空间1712Byte,平均运算时间28036ms
/ 3 = 9345ms |
从以上数据看出,在内存空间占用这个指标上,FlatBuffers占用的内存空间比protobuf多了两倍,而二者的cpu计算时间虽然相差3000ms左右,但考虑到测试用了100万次,二者每次计算时间几乎没有差别。
从以上测试数据来看,FlatBuffers的性能并不如它吹嘘的那么好。个人稍有点失望。
测试程序已经上传到百度网盘,点击这个链接即可下载。欢迎各位的批评意见。
FlatBuffers与protobuf性能比较,布布扣,bubuko.com
原文:http://blog.csdn.net/menggucaoyuan/article/details/34409433