igbinary msgpack bson json php中使用

[b]简介[/b]

igbinary, mspack, bson在数据格式话,类似json使用


[b]安装igbinary[/b]

#git clone https://github.com/igbinary/igbinary.git

#/usr/local/php5.3/bin/phpize

#./configure CFLAGS="-O2 -g" --enable-igbinary --with-php-config=/usr/local/php5.3/bin/php-config
#make
#make install

增加igbinary.so 到php.ini中


[b]安装msgpack[/b]

[b]#[/b]/usr/local/php5.3/bin/pecl install channel://pecl.php.net/msgpack-0.5.5

增加msgpack.so到 php.ini 中

安装bson
安装mongodb的php driver即可支持


[b]使用[/b]

#!/usr/bin/env php
<?php
// Valid test types
$test_types = array(
'json',
'bson',
'native',
'igbinary',
'msgpack',
);

// Get our options
$options = getopt('n:t:');

// Get our args and set defaults
$iterations = isset($options['n']) ? (int) $options['n'] : 20000;
$test_type = strtolower(isset($options['t']) ? $options['t'] : $test_types[0]);

// Validate test type
if (!in_array($test_type, $test_types)) {
echo 'Please choose a valid test.'. PHP_EOL.PHP_EOL;
echo ' Valid test types: '. PHP_EOL;

foreach ($test_types as $type) {
echo ' - '. $type .PHP_EOL;
}

exit();
}

// Figure out the functions to use
if ($test_type === 'native') {
$encode_function = 'serialize';
$decode_function = 'unserialize';
} elseif ($test_type === 'msgpack') {
$encode_function = 'msgpack_pack';
$decode_function = 'msgpack_unpack';
} elseif ($test_type === 'igbinary') {
$encode_function = 'igbinary_serialize';
$decode_function = 'igbinary_unserialize';
} else {
$encode_function = $test_type . '_encode';
$decode_function = $test_type . '_decode';
}

$test_data = array(
'meta' => array(
'status_code' => 200,
'status' => 'OK',
'message' => '',
),
'data' => array(
array(
'id' => 4,
'first_name' => 'Test',
'last_name' => 'User',
'access_level' => 100,
'created_at' => 'Mon, 26 Aug 2013 20:54:29 +0000',
'updated_at' => 'Mon, 26 Aug 2013 20:54:29 +0000',
),
array(
'id' => 3,
'first_name' => 'Testing',
'last_name' => 'Suarez',
'access_level' => 100,
'created_at' => 'Mon, 12 Aug 2013 14:12:48 +0000',
'updated_at' => 'Mon, 12 Aug 2013 14:13:53 +0000',
),
array(
'id' => 2,
'first_name' => 'Jake',
'last_name' => 'Suarez',
'access_level' => 100,
'created_at' => 'Mon, 12 Aug 2013 14:12:44 +0000',
'updated_at' => 'Mon, 12 Aug 2013 14:13:53 +0000',
),
array(
'id' => 1,
'first_name' => 'Trevor',
'last_name' => 'Suarez',
'access_level' => 1000,
'created_at' => 'Mon, 12 Aug 2013 14:12:40 +0000',
'updated_at' => 'Thu, 29 Aug 2013 16:18:41 +0000',
),
),
'paging' => array(
'page' => 1,
'per_page' => 10,
'order_descending' => true,
),
);

// Print benchmark info
echo 'Running benchmark for...'. PHP_EOL;
echo ' '. $test_type. PHP_EOL;
echo ' '. $iterations .' times'. PHP_EOL.PHP_EOL;

// Start timer
$pre_test_timer = microtime(true);

for ($i = 0; $i < $iterations; $i++) {
$encoded = $encode_function($test_data);
}

$post_encode_time = microtime(true);

for ($j = 0; $j < $iterations; $j++) {
$decoded = $decode_function($encoded);
}

$post_decode_time = microtime(true);

// Print report
echo 'Test completed!!'. PHP_EOL;
echo ' Encoding time: '. ($post_encode_time - $pre_test_timer). PHP_EOL;
echo ' Decoding time: '. ($post_decode_time - $post_encode_time). PHP_EOL;
echo ' Total time: '. ($post_decode_time - $pre_test_timer). PHP_EOL;
echo ' Encoded size: '. (strlen($encoded)). ' bytes'. PHP_EOL;
echo PHP_EOL;



[b]测试输出[/b]

$ ./bench.php -n 50000 -t native
Running benchmark for...
native
50000 times

Test completed!!
Encoding time: 0.53830790519714
Decoding time: 0.54197597503662
Total time: 1.0802838802338
Encoded size: 1078 bytes

$ ./bench.php -n 50000 -t json
Running benchmark for...
json
50000 times

Test completed!!
Encoding time: 0.66888904571533
Decoding time: 1.1631889343262
Total time: 1.8320779800415
Encoded size: 778 bytes

$ ./bench.php -n 50000 -t igbinary
Running benchmark for...
igbinary
50000 times

Test completed!!
Encoding time: 0.69265794754028
Decoding time: 0.39665293693542
Total time: 1.0893108844757
Encoded size: 491 bytes

$ ./bench.php -n 50000 -t bson
Running benchmark for...
bson
50000 times

Test completed!!
Encoding time: 0.31515908241272
Decoding time: 0.34058809280396
Total time: 0.65574717521667
Encoded size: 824 bytes

$ ./bench.php -n 50000 -t msgpack
Running benchmark for...
msgpack
50000 times

Test completed!!
Encoding time: 0.30013704299927
Decoding time: 0.50173997879028
Total time: 0.80187702178955
Encoded size: 645 bytes



[b]总结:[/b]
从运行性能在看来:bson > msgpack > igbinary > navtive > json
从格式话数据大小:igbinary < msgpack < bson < json < navtive
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值