#!/usr/bin/env php * @author Simon Dann * @license MIT */ // Valid test types $test_types = [ 'json', 'bson', 'native', 'messagepack', 'igbinary' ]; // 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 === 'messagepack') { $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 = [ '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;