Linux uses Msgpack and test __linux

Source: Internet
Author: User
Tags unpack

Http://www.cppblog.com/tx7do/archive/2014/05/29/207141.html

In the development of the network program, inevitably involves the server and the client interface between the protocol interaction, because the client and server platform of the difference (there may be windows,android,linux, etc.), as well as the network byte order problems, communication packets will do serialization and deserialization processing, Which is usually said to pack and unpack. Google's protobuf is a great thing, not only provides multi-platform support, but also directly supports generating code from configuration files. But such a powerful feature, meaning that its code and compile the generated library files and so on are not small, if relative to the mobile game one or two M installation package, This is obviously a bit too heavy (PS: Check the protobuf2.4.1 jar packet size of about 400k), and in the mobile game client and server interaction process, a lot of time the basic package unpack function is enough.

Today, recommended by colleagues, look up the relevant information msgpack. Msgpack provides a fast package unpack function, the official on the net given the figure is 4 times times faster than the PROTOBUF, can be said to be quite efficient. The biggest benefit in msgpack with the support of multiple languages, server-side can use the C++,android client can use Java, can meet the actual needs.

In the actual installation Msgpack process, encountered a little problem, because the development machine is 32-bit machine, i686, so after the installation run a simple sample, C + + compile error, wrong performance for link times wrong: undefined reference to ' __ Sync_sub_and_fetch_4 ', later referred to the following blog, in configure when designated cflags= "-march=i686" solution, note to make clean first.

Msgpack official website address: http://msgpack.org/

Blog address for reference during installation: http://blog.csdn.net/xiarendeniao/article/details/6801338



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

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

Make up for recent simple test results

Test machine, CPU 4 core Dual-core AMD Opteron (tm) Processor 2212, single core 2GHZ,1024KB cache, memory 4G.


1. Simple Package Test

[CPP] view plain copy struct Protohead {uint16_t ucmd;  command word uint16_t Ubodylen;      Package Length (string length after packing) uint32_t useq;           The serial number of the message, uniquely identifying a request uint32_t ucrcval;      The Crc32 check value of the package body (if the checksum is incorrect, the server disconnects)};       struct Protobody {int num;       std::string str;       Std::vector<uint64_t> Uinlst;   Msgpack_define (num, str, UINLST); };

The transformation between Baotou local byte order and network byte sequence is omitted in the test, only the package body does msgpack package processing. The number of elements in the vector array is 16, one at a time to package and unpack, and verify that the data is consistent before and after the test results are as follows:

Total time consuming (s)

Number of Cycles

Average time consuming (ms)

0.004691

100

0.04691

0.044219

1000

0.044219

0.435725

10000

0.043573

4.473818

100000

0.044738

Summary: The basic time to spend about 0.045ms per second can be packaged to unpack 22k times, the speed is very ideal.


2. Complex package test (vector nesting)

[CPP]   View plain copy 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&NBSP;!=&NBSP;STR)             {                return false;           }              if  (Node.idlst.size ()  != idlst.size ())             {                return false;           }      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.