php MessagePack介绍


Posted in PHP onOctober 06, 2013

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 相关文章推荐
php实现网站插件机制的方法
Nov 10 PHP
php SQL Injection with MySQL
Feb 27 PHP
基于PHP文件操作的详细诠释
Jun 21 PHP
(PHP实现)只使用++运算实现加法,减法,乘法,除法
Jun 27 PHP
php中rename函数用法分析
Nov 15 PHP
PHP判断手机是IOS还是Android
Dec 09 PHP
在Mac OS上自行编译安装Apache服务器和PHP解释器
Dec 24 PHP
php使用number_format函数截取小数的方法分析
May 27 PHP
深入理解PHP之OpCode原理详解
Jun 01 PHP
PHP实现深度优先搜索算法(DFS,Depth First Search)详解
Sep 16 PHP
laravel开发环境homestead搭建过程详解
Jul 03 PHP
eval(cmd)与eval($cmd)的区别与联系
Jul 07 PHP
php 批量替换程序的具体实现代码
Oct 04 #PHP
php5.5中类级别的常量使用介绍
Oct 02 #PHP
php mysql_real_escape_string函数用法与实例教程
Sep 30 #PHP
PHP文件上传主要代码讲解
Sep 30 #PHP
php中利用str_pad函数生成数字递增形式的产品编号
Sep 30 #PHP
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
Sep 30 #PHP
PHP设置一边执行一边输出结果的代码
Sep 30 #PHP
You might like
PHP数据库操作面向对象的优点
2006/10/09 PHP
PHP与SQL注入攻击[三]
2007/04/17 PHP
比file_get_contents稳定的curl_get_contents分享
2012/01/11 PHP
php计划任务之验证是否有多个进程调用同一个job的方法
2015/12/07 PHP
yii2 页面底部加载css和js的技巧
2016/04/21 PHP
javascript中字符串拼接需注意的问题
2010/07/13 Javascript
表单序列化与jq中的serialize使用示例
2014/02/21 Javascript
jquery获取form表单input元素值的简单实例
2016/05/30 Javascript
js友好的时间返回函数
2016/08/24 Javascript
jQuery实现简单的网页换肤效果示例
2016/09/18 Javascript
Bootstrap 下拉多选框插件Bootstrap Multiselect
2017/01/22 Javascript
HTML5+Canvas调用手机拍照功能实现图片上传(下)
2017/04/21 Javascript
jQuery EasyUI tree增加搜索功能的实现方法
2017/04/27 jQuery
深入理解ES6学习笔记之块级作用域绑定
2017/08/19 Javascript
webpack 插件html-webpack-plugin的具体使用
2018/04/09 Javascript
javascript实现贪吃蛇经典游戏
2020/04/10 Javascript
[02:33]2014DOTA2 TI每日综述 LGD涉险晋级DK闯入胜者组
2014/07/14 DOTA
[15:56]Heroes18_暗影萨满(完美)
2014/10/31 DOTA
[02:47]DOTA2亚洲邀请赛 HR战队出场宣传片
2015/02/07 DOTA
Zookeeper接口kazoo实例解析
2018/01/22 Python
python爬虫正则表达式之处理换行符
2018/06/08 Python
解决yum对python依赖版本问题
2019/07/05 Python
PyTorch学习:动态图和静态图的例子
2020/01/06 Python
Python字符串hashlib加密模块使用案例
2020/03/10 Python
python实现俄罗斯方块游戏(改进版)
2020/03/13 Python
TensorFlow tf.nn.conv2d_transpose是怎样实现反卷积的
2020/04/20 Python
使用python实现时间序列白噪声检验方式
2020/06/03 Python
python Timer 类使用介绍
2020/12/28 Python
html5自动播放mov格式视频的实例代码
2020/01/14 HTML / CSS
StubHub哥伦比亚:购买和出售您的门票
2016/10/20 全球购物
eBay法国购物网站:eBay.fr
2017/10/21 全球购物
网络教育自我鉴定
2013/11/01 职场文书
护士长2014年终工作总结
2014/11/11 职场文书
上课睡觉检讨书300字
2014/11/18 职场文书
综合素质评价思想道德自我评价
2015/03/09 职场文书
「Manga Time Kirara MAX」2022年5月号封面公开
2022/03/21 日漫