php MessagePack introduction_PHP tutorial

WBOY
Release: 2016-07-21 16:13:16
Original
959 people have browsed it

1. Today I saw a lot of people arguing about messagepack on hacknews. First understand 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 purpose of MessagePack. The author explains that it has two major uses: one is Space-efficient storage for Memcache entries (Pinterest), a space-saving mamcache application; the other 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 controversial point is that the benchmark of MessagePack says that it is many times faster than protocolBuffer and Json. But some people don't believe it and do a test under javascript (json and messagePack). It is found that MessagePack only compresses about 10% less data than json, and the compression and decompression time is much more time-consuming than the json parser.

4. "MsgPack vs. JSON: Cut your client-server exchange traffic by 50% with one 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 bandwidth. The author emphasized that they would rather waste 0.5ms-1ms on the client, but the server uses ruby's MessagePack parser, which 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.

Copy code The code is as follows:

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

The author of MessagePack also believes that MessagePack may not be the best choice for client-side serialization as described by the blog author. The author citing 2 is a little tragic.

5. Bson is the binary form of Json, but it is grammatically incompatible with Json. But MessagePack guarantees semantic consistency.

6. Different scene requirements lead to differences in the application of technology.

PHP Trial MessagePack

It's like JSON. but fast and small.

This sentence attracted me and I went to take a look.

Official website: http://msgpack.org

The official installation method is misleading, there is no php directory in the msgpack directory... I only see csharp, erlang, go, java, ruby etc. directory.

Copy code The code is as follows:

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


Still found the extension on the PHP official website: http://pecl.php. net/package/msgpack
Last updated: 2012-09-14, yesterday’s version.
Attached is the installation process:

Copy code The code is as follows:

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 php.ini, restart php, and complete the installation.

Start testing:
$data = array(0=>'abcdefghijklmnopqrstuvwxyz',1=>'Xiamen','abc'=>'1234567890');

msgpack_pack, json_encode, serialize respectively, the length is: 50, 62, 87
and then executed 10000 times respectively, time consuming: 9.95 milliseconds, 17.45 milliseconds, 8.85 milliseconds
Unlock and execute 10000 times, time consuming: 14.76 Milliseconds, 23.93 milliseconds, 14.61 milliseconds

The performance of msgpack is at least 50% better than json. Although it is almost the same speed as serialize, serialize takes up significantly more space.

In addition, the GBK program is convenient. You can also msgpack_pack Chinese. If you use json, you need to batch convert it to utf-8 before you can json_encode.

Quote:

1, MessagePack official website

2. MsgPack vs. JSON: Cut your client-server exchange traffic by 50% with one line of code

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

3, My thoughts on MessagePack

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

4 Performance comparison between MessagePack and JSON under JS

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313487.htmlTechArticle1. Today I saw many people arguing about messagepack on hacknews. First understand what MessagePack is: MessagePack is a binary-based efficient object serialization library. It enables...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template