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调用Java对象的方法
Oct 09 PHP
wordpress之wp-settings.php
Aug 17 PHP
php采集速度探究总结(原创)
Apr 18 PHP
php file_exists 检查文件或目录是否存在的函数
May 10 PHP
PHP开发规范手册之PHP代码规范详解
Jan 13 PHP
解析在PHP中使用mysqli扩展库对mysql的操作
Jul 03 PHP
php中autoload的用法总结
Nov 08 PHP
自己写的php中文截取函数mb_strlen和mb_substr
Feb 09 PHP
PHP获取文件相对路径的方法
Feb 26 PHP
PHP基于GD库的图像处理方法小结
Sep 27 PHP
php生成二维码不保存服务器还有下载功能的实现代码
Aug 09 PHP
yii2.0框架多模型操作示例【添加/修改/删除】
Apr 13 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 文件上传源码分析(RFC1867)
2009/10/30 PHP
ThinkPHP与PHPExcel冲突解决方法
2011/08/08 PHP
Smarty中的注释和截断功能介绍
2015/04/09 PHP
php上传大文件设置方法
2016/04/14 PHP
thinkphp在php7环境下提示Cannot use ‘String’ as class name as it is reserved的解决方法
2016/09/30 PHP
yii2.0整合阿里云oss的示例代码
2017/09/19 PHP
关于PHP虚拟主机概念及如何选择稳定的PHP虚拟主机
2018/11/20 PHP
jquery 经典动画菜单效果代码
2010/01/26 Javascript
JavaScript游戏之是男人就下100层代码打包
2010/11/08 Javascript
php,js,css字符串截取的办法集锦
2014/09/26 Javascript
nodejs入门教程五:连接数据库的方法分析
2017/04/24 NodeJs
让bootstrap的carousel支持滑动滚屏的实现代码
2017/11/27 Javascript
vue结合element-ui使用示例
2019/01/24 Javascript
vue使用element-ui实现表单验证
2020/12/13 Vue.js
python实现ftp客户端示例分享
2014/02/17 Python
Python中的zip函数使用示例
2015/01/29 Python
python 的列表遍历删除实现代码
2020/04/12 Python
Python处理JSON数据并生成条形图
2016/08/05 Python
Python编程之基于概率论的分类方法:朴素贝叶斯
2017/11/11 Python
python实现超简单的视频对象提取功能
2018/06/04 Python
python获取url的返回信息方法
2018/12/17 Python
python抓取需要扫微信登陆页面
2019/04/29 Python
PyCharm下载和安装详细步骤
2019/12/17 Python
Python基础之字符串常见操作经典实例详解
2020/02/26 Python
如何在Windows中安装多个python解释器
2020/06/16 Python
使用数据结构给女朋友写个Html5走迷宫游戏
2019/11/26 HTML / CSS
Html5定位终极解决方案
2020/02/05 HTML / CSS
文明单位申报材料
2014/12/23 职场文书
英语导游词
2015/02/13 职场文书
团员年度个人总结
2015/02/26 职场文书
学校捐书活动总结
2015/05/08 职场文书
2015年敬老院工作总结
2015/05/18 职场文书
公司车辆维修管理制度
2015/08/05 职场文书
Python趣味实战之手把手教你实现举牌小人生成器
2021/06/07 Python
Vue提供的三种调试方式你知道吗
2022/01/18 Vue.js
JavaCV实现照片马赛克效果
2022/01/22 Java/Android