简介
igbinary, mspack, bson在数据格式话,类似json使用
安装igbinary
#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中
安装msgpack
#/usr/local/php5.3/bin/pecl install channel://pecl.php.net/msgpack-0.5.5
增加msgpack.so到 php.ini 中
安装bson
安装mongodb的php driver即可支持
使用
#!/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;
测试输出
$ ./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
总结:
从运行性能在看来:bson > msgpack > igbinary > navtive > json
从格式话数据大小:igbinary < msgpack < bson < json < navtive