php MessagePack介绍

yipeiwu_com5年前PHP代码库

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 JSON, it is very fast and small.

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 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,争议的地方是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 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作者也认为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.93 毫秒,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

相关文章

PHP处理JSON字符串key缺少双引号的解决方法

本文实例讲述了PHP处理JSON字符串key缺少引号的解决方法,分享给大家供大家参考之用。具体方法如下: 通常来说,JSON字符串是key:value形式的字符串,正常key是由双引号括...

PHP多线程编程之管道通信实例分析

本文实例讲述了PHP多线程编程之管道通信用法。分享给大家供大家参考。具体分析如下: 一个线程如果是个人英雄主义,那么多线程就是集体主义,你不再是一个独行侠,而是一个指挥家。 管道通信:...

PHP排序算法的复习和总结

直接上代码吧! 复制代码 代码如下: <?php /* * 插入排序(一维数组) * 每次将一个待排序的数据元素,插入到前面已经排好序的数列中的适当的位置,使数列依然有序;直到待排...

require(),include(),require_once()和include_once()区别

我把这两个语句放在一起介绍,读者可以比较学习。 1.require()语句 require()语句用于指定的文件代替语句本身,就象C语言中的include()语句一样。如果php配置文件...

snoopy PHP版的网络客户端提供本地下载

magpierss中就用到了snoopy,这让我有点兴趣去研究下这个咚咚。再SF上,找到了这个源代码。居然就是一个类,但不要笑看哦,功能可是很强大的。    ...