Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand php MessagePack

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand php MessagePack". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand php MessagePack.

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

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 to reduce the amount of data on the server and make more reasonable use of bandwidth. The authors emphasize that they would rather waste client-side 0.5ms-1ms, but the server uses ruby's MessagePack parser, which is five 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.

The copy 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 thinks that MessagePack may not be the best choice for client-side serialization as described by the blog author. The author of Quote 2 is a little tragic.

5BSon is the binary form of Json, but it is syntactically incompatible with JSon. But MessagePack guarantees semantic consistency.

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

PHP try MessagePack

It's like JSON. But fast and small.

This sentence attracted me and went to have a look.

Official website: http://msgpack.org

The official installation method deceives people that there is no php directory under the msgpack directory. Only see directories such as csharp,erlang,go,java,ruby.

The copy code is as follows:

Git clone https://github.com/msgpack/msgpack.git

Cd msgpack/php

Phpize

. / configure & & make & & make install

Or in the PHP official website extension to find: http://pecl.php.net/package/msgpack

Last updated: 2012-09-14, yesterday's version.

Attach the installation process:

The copy 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')

For their msgpack_pack,json_encode,serialize, the length is 50, 62, and 87 respectively.

And then executed 10000 times, time-consuming: 9.95ms, 17.45ms, 8.85ms.

Unlock execution 10000 times, time: 14.76ms, 23.93ms, 14.61ms

The performance of msgpack is at least better than that of json50%, although it is about the same speed as serialize, but serialize takes up a lot of space.

In addition, the program of GBK is convenient, Chinese can also be msgpack_pack, and if you use json, you have to convert it into utf-8 in batch before you can json_encode.

Reference:

Official website of 1JI MessagePack

2MsgPack 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

Performance comparison between MessagePack and JSON under 4 JS

At this point, I believe you have a deeper understanding of "how to understand php MessagePack". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report