PhpMessagePack introduction _ PHP

Source: Internet
Author: User
PhpMessagePack introduction 1. on hacknews today, many people are arguing about messagepack. First understand what is MessagePack: MessagePack is a binary-based efficient object serialization library. It enables to exchange structured objects between ages like JSON. But unlike JSON, it is very fast and small.

2. the main use of MessagePack is explained by the Author. The first is Space-efficient storage for Memcache entries (Pinterest), which saves Space for mamcache applications; 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 controversy lies in benchmark of MessagePack. he is much faster than protocolBuffer and Json. But some people don't believe it. do a test under javasrept (json and messagePack ). It is found that MessagePack only compresses about 10% less data than json, while the compression and decompression time is much time-consuming compared with the json parser.

4. "MsgPack. JSON: Cut your client-server exchange traffic by 50% with one line of code "this article uses messagePack to optimize the server, reduce the server data volume, and make more reasonable use of bandwidth. The author stressed that they would rather waste 0.5 ms-1 ms of the client, but the server uses the ruby MessagePack parser, which is 5 times faster than JSON.

The differenceJSONIs, that MsgPack isBinary-based-This gives the possibility to make the exchanged dataA) 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 parseAndEncode, Having a parser parse 40 bytes takes about twice as long as parsing 20 bytes.

The code is as follows:
MyJSONString = JSON. stringify (myObject );
MyObject = JSON. parse (myJSONString );
Var myByteArray = msgpack. pack (myObject );
MyObject = msgpack. unpack (myByteArray );

The authors of MessagePack also think that MessagePack may not be the best choice for client-side serialization as described by the blog author. References 2 are a little tragic.

5. BSon is a binary form of Json, but it is not compatible with JSon syntax. However, MessagePack ensures semantic consistency.

6. different scenarios lead to different technical applications.

Try MessagePack in PHP

It's like JSON. but fast and small.

This sentence attracted me and gave me a look.

Official website: http://msgpack.org

The official installation method is confusing. there is no php directory under the msgpack directory... only the directories such as csharp, erlang, go, java, and ruby are displayed.

The code is as follows:
Git clone https://github.com/msgpack/msgpack.git
Cd msgpack/php
Phpize
./Configure & make install


Still found in PHP official website extension: http://pecl.php.net/package/msgpack
Last Updated:, yesterday's version.
Installation process:

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


Add msgpack. so to php. ini, restart php, and complete the installation.

Start test:
$ Data = array (0 => 'abcdefhijklmnopqrstuvwxy', 1 => 'xiamen ', 'ABC' => '123 ');

Msgpack_pack, json_encode, and serialize are respectively set to 50, 62, and 87.
Then execute the command 10000 times, respectively. the time consumed is 9.95 milliseconds, 17.45 milliseconds, and 8.85 milliseconds.
Unlocks 10000 executions, time consumed: 14.76 milliseconds, 23.93 milliseconds, 14.61 milliseconds

Msgpack has a performance of at least over json50%. although it is similar to the speed of serialize, the space occupied by serialize is much higher.

In addition, the GBK program is convenient. msgpack_pack can also be used in Chinese. if json is used, it must be converted to UTF-8 in batches before json_encode can be used.

Reference:

1. MessagePack official website

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

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

3, My thoughts on MessagePack

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

4. performance comparison between MessagePack and JSON in JS

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

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.