linux使用msgpack及測試

來源:互聯網
上載者:User

在網路程式的開發中,免不了會涉及到伺服器與用戶端之間的協議互動,由於用戶端與伺服器端的平台的差異性(有可能是windows,android,linux等等),以及網路位元組序等問題,通訊包一般會做序列化與還原序列化的處理,也就是通常說的打包解包工作。google的protobuf是一個很棒的東西,它不僅提供了多平台的支援,而且直接支援從設定檔產生代碼。但是這麼強大的功能,意味著它的代碼量以及編譯產生的庫檔案等等都不會小,如果相對於手機遊戲一兩M的安裝包來說,這個顯然有點太重量級了(ps:查了一下protobuf2.4.1的jar的包大小有400k左右),而且在手機遊戲用戶端與伺服器的互動過程中,很多時候基本的打包解包功能就足夠了。

今天經同事推薦,查閱了一下msgpack相關的資料。msgpack提供快速的打包解包功能,官網上給出的圖號稱比protobuf快4倍,可以說相當高效了。最大的好處在與msgpack支援多語言,伺服器端可以用C++,android用戶端可以用java,能滿足實際需求。

在實際安裝msgpack的過程中,碰到了一點小問題,因為開發機器是32位機器,i686的,所以安裝完後跑一個簡單的sample時,c++編譯報錯,錯誤的表現為連結時報錯:undefined reference to
`__sync_sub_and_fetch_4',後來參考了下面的部落格,在configure時指定CFLAGS="-march=i686"解決,注意要make clean先。

msgpack官網地址:http://msgpack.org/

安裝過程中參考的部落格地址:http://blog.csdn.net/xiarendeniao/article/details/6801338


====================================================================================

====================================================================================

補上近期簡單的測試結果

測試機器,cpu 4核 Dual-Core AMD Opteron(tm) Processor 2212,單核2GHz,1024KB cache, 記憶體4G。


1. 簡單包測試

struct ProtoHead{    uint16_t uCmd;      // 命令字    uint16_t uBodyLen;  // 包體長度(打包之後的字串長度)    uint32_t uSeq;      //訊息的序號,唯一標識一個請求    uint32_t uCrcVal;           // 對包體的crc32校正值(如果校正不正確,伺服器會中斷連線)};struct ProtoBody{    int num;    std::string str;    std::vector<uint64_t> uinlst;    MSGPACK_DEFINE(num, str, uinlst);};

測試中省略了包頭本地位元組序與網路位元組序之間的轉化,只有包體做msgpack打包處理.

vector數組中元素數量為16個,每次迴圈做一次打包與解包,並驗證前後資料是否一致,得到的測試結果如下:

總耗時(s)

迴圈次數

平均每次耗時(ms)

0.004691

100

0.04691

0.044219

1000

0.044219

0.435725

10000

0.043573

4.473818

100000

0.044738

總結:基本每次耗時0.045ms左右,每秒可以打包解包22k次,速度很理想。

2. 複雜包測試(vector嵌套)

struct test_node{    std::string str;    std::vector<uint32_t> idlst;    test_node()    {        str = "it's a test node";        for (int i = 0; i++; i < 10)        {            idlst.push_back(i);        }    }    bool operator == (const test_node& node) const    {        if (node.str != str)        {            return false;        }        if (node.idlst.size() != idlst.size())        {            return false;        }        for (int i = 0; i < idlst.size(); i++){            if (idlst[i] != node.idlst[i])            {                return false;            }}        return true;    }    MSGPACK_DEFINE(str, idlst);};struct ProtoBody{    int num;    std::string str;    std::vector<uint64_t> uinlst;    std::vector<test_node> nodelst;    MSGPACK_DEFINE(num, str, uinlst, nodelst);};

每個nodelst中插入16個node,每個node中的idlst插入16個id,同1中的測試方法,得到測試結果如下:

總耗時(s)

迴圈次數

平均每次耗時(ms)

0.025401

100

0.25401

0.248396

1000

0.248396

2.533385

10000

0.253339

25.823562

100000

0.258236

基本上每次打包解包一次要耗時0.25ms,每秒估算可以做4k次打包解包,速度還是不錯的。


3. 加上crc校正

如果每個迴圈中,打包過程加上crc的計算,解包過程中加上crc校正,得到測試結果如下:

總耗時(s)

迴圈次數

平均每次耗時(ms)

0.025900

100

0.25900

0.260424

1000

0.260424

2.649585

10000

0.264959

26.855452

100000

0.268555

基本上每次打包解包耗時0.26ms左右,與沒有crc差別不大;



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.