php MessagePack introduction

  • 2020-10-23 20:03:21
  • OfStack

1. Many people are arguing about messagepack on hacknews today. First, learn what MessagePack is: MessagePack is a binary-based efficient object serialization library. It enables to exchange structured objects between many languages like JSON. But unlike JSON, it is very fast and small.

2, the main use of MessagePack, the author explained that there are two main USES: 1 is ES36en-ES37en storage for Memcache entries (Pinterest), space saving type of mamcache application; 2. The other one is for RPC transmission, This use case is fairly close to my original intent. When one is designing an RPC system, one of the first tasks is to specify and implement a communication protocol. This process can get pretty hairy as you need to worry about a lot of low-level issues like Endian-ness. By using MessagePack, one can skip designing and implementing a communication protocol entirely and accelerate development.

3. The point of contention is that benchmark of MessagePack says that he is many times faster than protocolBuffer, Json. But some people do not believe that, do a test under javasript (json and messagePack). MessagePack was found to have only about 10% less compressed data than json, and the compression and decompression time was much more time-consuming than json's parser.

4, "MsgPack vs: Cut your client by 50% with line of code" this article USES messagePack to optimize the server, reduce the amount of data on the server, and make more reasonable use of the bandwidth. The authors emphasize that they would rather waste 0.5ES142en -- 1ms on the client, but the server USES ruby's MessagePack parser and can be 5 times faster than JSON.

The difference to JSON is, that MsgPack is binary-based - this gives the possibility to make the exchanged data a) smaller and use less bytes, I guess we all know the advantages of that, however there is an even bigger advantage: b) It is faster to parse and encode, having a parser parse 40 bytes takes about twice as long as parsing 20 bytes.

 
myJSONString = JSON.stringify(myObject); 
myObject = JSON.parse(myJSONString); 
var myByteArray = msgpack.pack(myObject); 
myObject = msgpack.unpack(myByteArray); 

MessagePack author also thinks MessagePack may not be the best choice for client - side serialization as described by the blog author. The author of quote 2 is a bit of a tragedy.

5, BSon is the base 2 form of Json, but there are some grammatical incompatibility with JSon. However, MessagePack guarantees that it is semantically 1 to 1.

6. Different scenario requirements lead to different application of technologies.

PHP trial MessagePack

It's like JSON. but fast and small.

That caught my eye, and I went to check it out.

Liverpoolfc.tv: http: / / msgpack org

The official installation method is misleading. There is no msgpack directory... Only see csharp erlang, go java, ruby directory, etc.

 
git clone https://github.com/msgpack/msgpack.git 
cd msgpack/php 
phpize 
./configure && make && make install 


Or in PHP website extension found: http: / / pecl php. net/package/msgpack
Last updated: 2012-09-14, yesterday's edition.
Attached installation process:

 
wget http://pecl.php.net/get/msgpack-0.5.2.tgz 
tar zxf msgpack-0.5.2.tgz 
cd msgpack-0.5.2 
/usr/local/hx/php/bin/phpize 
./configure --with-php-config=/usr/local/hx/php/bin/php-config 
make && make install 


Then add msgpack.so to ES225en.ini and restart php to complete the installation.

Start the test:
$data = array(0= > 'abcdefghijklmnopqrstuvwxyz',1= > 'xiamen', 'abc = > '1234567890');

For msgpack_pack, json_encode, serialize respectively, the length is: 50,62,87
Then execute 10,000 times, taking 9.95 ms, 17.45 ms, and 8.85 ms
Undo 10,000 times, 14.76 ms, 23.93 ms, 14.61 ms

The PERFORMANCE of msgpack is at least 50% better than es250EN, which takes up significantly more space than serialize, although it is actually about the same speed.

In addition, GBK program is convenient, Chinese can also msgpack_pack, json to batch conversion to utf-8 before json_encode.

Reference:

1. MessagePack official website

2, MsgPack vs JSON your server exchange by 50% with one of code

Address HN comment: http: / / news ycombinator. com/item? id=4090831

3, My thoughts on MessagePack

Address HN comment: http: / / news ycombinator. com/item? id=4092969

4 Performance comparison between MessagePack and JSON under JS

Address HN comment: http: / / news ycombinator. com/item? id=4091051


Related articles: