標籤:des class http tar com get
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檔案如下:
////////////////////////////////////////////////////////// FILE : tellist.fbs//// DESC : basic message for msg-center//// AUTHOR : v 0.1 written by Alex Stocks on June 22, 2014//// LICENCE ://// MOD :////////////////////////////////////////////////////////namespace as.tellist;enum GENDER_TYPE : byte{MALE= 0,FEMALE= 1,OTHER= 2}table personal_info{id : uint;name : string;age : byte;gender : GENDER_TYPE;phone_num : ulong;}table personal_info_list{info : [personal_info];}root_type personal_info_list;
因為要以protobuf做效能參考,列出protobuf的idl檔案如下:
////////////////////////////////////////////////////////// FILE : tellist.proto//// DESC : basic message for msg-center//// AUTHOR : v 0.1 written by Alex Stocks on June 22, 2014//// LICENCE ://// MOD :////////////////////////////////////////////////////////package as.tellist;enum gender_type{MALE= 0;FEMALE= 1;OTHER= 2;}message personal_info{optional uint32id = 1;optional stringname = 2;optional uint32age = 3;optional gender_typegender = 4;optional uint64phone_num = 5;}message personal_info_list{repeated personal_infoinfo = 1;}
在記憶體中構造37個personal_info對象,並序列化之,重複這個過程100萬次。
測試結果如下:
測試環境:12Core Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz free total used free shared buffers cachedMem: 66081944 65831100 250844 0 182240 46903452-/+ buffers/cache: 18745408 47336536Swap: 975864 724648 251216protobuf三次測試結果:./bin/tellist_test loop = 1000000, time diff = 14283msbuf size:841bin/tellist_test loop = 1000000, time diff = 14096msbuf size:841bin/tellist_test loop = 1000000, time diff = 14229msbuf size:841佔用記憶體空間841Byte,平均運算時間42608ms / 3 = 14202.7msflatbuffers三次測試結果:bin/tellist_test loop = 1000000, time diff = 11694msbuf size:1712 bin/tellist_test loop = 1000000, time diff = 11710msbuf size:1712bin/tellist_test loop = 1000000, time diff = 11774msbuf size:1712佔用記憶體空間1712Byte,平均運算時間35178ms / 3 = 11726ms測試環境:1 Core Intel(R) Core(TM) i5-3210M CPU @ 2.50GHzMEM total used free shared buffers cachedMem: 753932 432672 321260 0 89236 258052-/+ buffers/cache: 85384 668548Swap: 1324028 0 1324028protobuf三次測試結果:bin/tellist_test loop = 1000000, time diff = 12779msbuf size:841bin/tellist_test loop = 1000000, time diff = 13475msbuf size:841bin/tellist_test loop = 1000000, time diff = 12604msbuf size:841佔用記憶體空間841Byte,平均運算時間38858ms / 3 = 12952.7msflatbuffers三次測試結果:bin/tellist_test loop = 1000000, time diff = 9424msbuf size:1712 bin/tellist_test loop = 1000000, time diff = 9277msbuf size:1712bin/tellist_test loop = 1000000, time diff = 9265msbuf size:1712info vecotor size:37, its right size:37佔用記憶體空間1712Byte,平均運算時間28036ms / 3 = 9345ms
從以上資料看出,在記憶體空間佔用這個指標上,FlatBuffers佔用的記憶體空間比protobuf多了兩倍,而二者的cpu計算時間雖然相差3000ms左右,但考慮到測試用了100萬次,二者每次計算時間幾乎沒有差別。
從以上測試資料來看,FlatBuffers的效能並不如它吹噓的那麼好,個人稍有點失望。FB大量使用了C++11的文法,其從idl產生的程式碼介面也不如protubuf友好。不過相比使用protobuf時的一堆標頭檔和佔18M之多的lib庫,FlatBuffers僅僅一個"flatbuffers/flatbuffers.h"就足夠了。
測試程式已經上傳到百度網盤,點擊這個連結即可下載。歡迎各位的批評意見。