In the development of network programs, protocol interaction between the server and the client is inevitable. Due to the differences between the client and the server platform (possibly windows, android, and linux ), communication packets are usually serialized and deserialized, that is, packaging and unpacking. Google's protobuf is a great thing. It not only supports multiple platforms, but also directly supports generating code from the configuration file. However, such a powerful function means that the amount of code and the library files generated by compilation will not be small. If the installation package is a bit too heavy compared to one or two MB of mobile games (ps: check that the jar package size of protobuf2.4.1 is about KB. In addition, during the interaction between the mobile game client and the server, the basic package unpacking function is sufficient.
Today, I was recommended by my colleagues to check msgpack related materials. Msgpack provides a fast package unpacking function. The diagram shown on the official website is four times faster than protobuf, which can be said to be quite efficient. The biggest benefit is that msgpack supports multiple languages, the server can use C ++, And the android client can use java to meet actual needs.
In the actual installation of msgpack, a small problem occurs, because the development machine is a 32-bit machine, i686, so after installation, run a simple sample, c ++ compilation error. The error is displayed as a link error: undefined reference
'_ Sync_sub_and_fetch_4'. Later, refer to the following blog and specify CFLAGS = "-march = i686" in configure. make sure 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
========================================================== ========================================================== ====
========================================================== ========================================================== ====
Complete the recent simple test results
Test Machine: 4-Core Dual-Core AMD Opteron (tm) Processor 2212, Single Core 2 GHz, kb cache, 4 GB memory.
1. Simple package test
Struct ProtoHead {uint16_t uCmd; // command word uint16_t uBodyLen; // package body length (String Length After packaging) uint32_t uSeq; // message serial number, uniquely identifies a request uint32_t uCrcVal; // The crc32 check value for the package body (if the check is incorrect, the server will disconnect)}; struct ProtoBody {int num; std: string str; std: vector <uint64_t> uinlst; MSGPACK_DEFINE (num, str, uinlst );};
In the test, the conversion between the local and network byte order of the packet header is omitted. Only the package body is used for msgpack packaging.
The number of elements in the vector Array is 16. Each cycle is packed and unwrapped, and the data is verified to be consistent. The test result is as follows:
Total time consumed (s) |
Number of cycles |
Average time consumed per time (MS) |
0.004691 |
100 |
0.04691 |
0.044219 |
1000 |
0.044219 |
0.435725 |
10000 |
0.043573 |
4.473818 |
100000 |
0.044738 |
Conclusion: It takes about ms each time. You can package and unpack the package 22 K times per second. The speed is ideal.
2. complex package testing (vector nesting)
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);};
Insert 16 nodes in each nodelst, and insert 16 IDs in each node. The test results are as follows:
Total time consumed (s) |
Number of cycles |
Average time consumed per time (MS) |
0.025401 |
100 |
0.25401 |
0.248396 |
1000 |
0.248396 |
2.533385 |
10000 |
0.253339 |
25.823562 |
100000 |
0.258236 |
Basically, it takes 0.25 ms to package and unpackage each time. It is estimated that 4 K package and unpackage can be done every second. The speed is good.
3.Crc verification
If crc calculation is added during the packaging process in each loop, crc verification is added during the unpacking process. The test results are as follows:
Total time consumed (s) |
Number of cycles |
Average time consumed per time (MS) |
0.025900 |
100 |
0.25900 |
0.260424 |
1000 |
0.260424 |
2.649585 |
10000 |
0.264959 |
26.855452 |
100000 |
0.268555 |
It takes about ms to package and unpackage each time, which is not much different from that without crc;