首頁  >  文章  >  後端開發  >  php MessagePack介紹_PHP教程

php MessagePack介紹_PHP教程

WBOY
WBOY原創
2016-07-21 16:13:16962瀏覽

1,今天在hacknews上看到很多人對messagepack的爭論。首先了解什麼是MessagePack:MessagePack is a binary-based efficient object serialization library. It enables to exchange structured objects between many languages like JSON. But unlike JverySON, it is very.

2,MessagePack的主要用途,作者解釋說有兩大用途:一是Space-efficient storage for Memcache entries (Pinterest),節省空間類型的mamcache應用;另一個是用於RPC傳輸, 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 worian about a lotue of lvelop lots lot lvel le 書 need. -ness. By using MessagePack, one can skip designing and implementing a communication protocol entirely and accelerate development.

3,爭議的地方是MessagePack的benchmark說,他比protocolBuffer,Json快很多倍。但有人不相信,做個javasript下的測試(json與messagePack)。發現MessagePack僅是壓縮後的資料比json少10%左右,而壓縮和解壓時間則和json的解析器比起來要費時很多。

4,「MsgPack vs. JSON: Cut your client-server exchange traffic by 50% with one line of code」這篇文章使用了messagePack做伺服器的最佳化,降低伺服器的資料量,更合理的利用頻寬。作者強調了他們寧願浪費客戶端的0.5ms—1ms,但是伺服器使用ruby的MessagePack解析器,效率能夠比JSON快5倍。

The difference to

JSON is, that MsgPack is binary-based - this gives the possibility to make the exchanged data a) smaller and less lessby> , 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. as long as parsing 20 bytes.

複製程式碼 程式碼如下:
myJSONString = JSON. ;
myObject = JSON.parse(myJSONString);
var myByteArray = msgpack.pack(myObject);
myObject = msgpack.unpack(myByteArray);

msgpack.unpack(myByteArray);
MessagePack作者也認為MessagePack may not be the best choice for client-side serialization as described by the blog author.引用2的作者有點小悲劇。

5,BSon是Json的二進位形式,但是與JSon有語法不相容的地方。但是MessagePack保證語意上能夠做到一致。

6,場景需求不同,導致技術的應用有所差異。



PHP試試MessagePack
It's like JSON. but fast and small.

這句吸引了我,瞧了下去。

官網:http://msgpack.org

官方的安裝方法忽悠人,msgpack目錄下根本沒php目錄...只看到csharp,erlang,go,java,ruby等目錄。


複製程式碼 程式碼如下:
git clone https://github.com/msgpack/msgpack.git
cd msgpack/php
phpize
./configure && make && make install


還是在PHP官網擴充找到了:http://pecl.php. net/package/msgpack
最後更新時間:2012-09-14,昨天的版本。
附安裝過程:


複製程式碼 程式碼如下:
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



然後把msgpack.so加到php.ini裡,重啟php,完成安裝。

開始測試:
$data = array(0=>'abcdefghijklmnopqrstuvwxyz',1=>'廈門','abc'=>'1234567890');

分別對其msgpack_pack,json_encode,serialize,長度為:50,62,87
然後分別執行10000次,耗時:9.95 毫秒,17.45 毫秒,8.85 毫秒
解開執行10000次,耗時:14.76 毫秒,23.毫秒,14.61 毫秒

msgpack的效能至少超過json50%,雖然和serialize其實速度差不多,但serialize佔用空間明顯比較多。

另外,GBK的程式方便了,中文也可以msgpack_pack,用json的話還要批量轉換成utf-8之後才能json_encode。

引用:

1,MessagePack官方網站

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

HN評論網址:http://news.ycombinator.com/item?id=4090831

3,My thoughts on MessagePack

HN評論網址:http://news.ycombinator.com/item?id=4092969

4 JS下MessagePack與JSON效能比較

HN評論網址:http://news.ycombinator.com/item?id=4091051

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313487.htmlTechArticle1,今天在hacknews上看到很多人對messagepack的爭論。首先了解什麼是MessagePack:MessagePack is a binary-based efficient object serialization library. It enables...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn