Created
September 5, 2013 22:43
-
-
Save Rican7/6457237 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* JSON/BSON encoding and decoding benchmark | |
* | |
* Now with native serializing! | |
* | |
* @copyright 2013 Trevor N Suarez | |
* @link http://blennd.com/ | |
* @author Trevor Suarez <[email protected]> | |
* @license MIT | |
*/ | |
// Valid test types | |
$test_types = [ | |
'json', | |
'bson', | |
'native', | |
]; | |
// 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'; | |
} else { | |
$encode_function = $test_type . '_encode'; | |
$decode_function = $test_type . '_decode'; | |
} | |
$test_data = [ | |
'meta' => [ | |
'status_code' => 200, | |
'status' => 'OK', | |
'message' => '', | |
'more_info' => [ | |
], | |
], | |
'data' => [ | |
[ | |
'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', | |
], | |
[ | |
'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', | |
], | |
[ | |
'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', | |
], | |
[ | |
'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' => [ | |
'page' => 1, | |
'per_page' => 10, | |
'order_descending' => true, | |
'resources' => [ | |
], | |
], | |
]; | |
// 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment