From a51c74793a5fc2c294443305d78904c1b431ab13 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Sun, 10 Nov 2019 21:16:42 +0100 Subject: [PATCH] benchmark using real-life api for a better comparison --- tests/benchmark-parse-api.php | 106 ++++++++++++++++++++++++++++++++++ tests/benchmark.php | 92 ----------------------------- 2 files changed, 106 insertions(+), 92 deletions(-) create mode 100644 tests/benchmark-parse-api.php delete mode 100644 tests/benchmark.php diff --git a/tests/benchmark-parse-api.php b/tests/benchmark-parse-api.php new file mode 100644 index 0000000..8cb3816 --- /dev/null +++ b/tests/benchmark-parse-api.php @@ -0,0 +1,106 @@ + + * + * Options: + * + * : + * The number of routes to map & match. Defaults to 1000. + */ + +require __DIR__ . '/../vendor/autoload.php'; +$routes = [ + ["POST", "/1/classes/[a:className]"], + ["GET", "/1/classes/[a:className]/[i:objectId]"], + ["PUT", "/1/classes/[a:className]/[i:objectId]"], + ["GET", "/1/classes/[a:className]"], + ["DELETE", "/1/classes/[a:className]/[i:objectId]"], + + // Users + ["POST", "/1/users"], + ["GET", "/1/login"], + ["GET", "/1/users/[i:objectId]"], + ["PUT", "/1/users/[i:objectId]"], + ["GET", "/1/users"], + ["DELETE", "/1/users/[i:objectId]"], + ["POST", "/1/requestPasswordReset"], + + // Roles + ["POST", "/1/roles"], + ["GET", "/1/roles/[i:objectId]"], + ["PUT", "/1/roles/[i:objectId]"], + ["GET", "/1/roles"], + ["DELETE", "/1/roles/[i:objectId]"], + + // Files + ["POST", "/1/files/:fileName"], + + // Analytics + ["POST", "/1/events/[a:eventName]"], + + // Push Notifications + ["POST", "/1/push"], + + // Installations + ["POST", "/1/installations"], + ["GET", "/1/installations/[i:objectId]"], + ["PUT", "/1/installations/[i:objectId]"], + ["GET", "/1/installations"], + ["DELETE", "/1/installations/[i:objectId]"], + + // Cloud Functions + ["POST", "/1/functions"], +]; +$total_time = 0; +$router = new AltoRouter(); + +// map requests +$start = microtime(true); +foreach ($routes as $r) { + $router->map($r[0], $r[1], ''); +} +$end = microtime(true); +$time = $end - $start; +$total_time += $time; +echo sprintf('Map time: %.3f ms', $time * 1000) . PHP_EOL; + +// match a static route +$start = microtime(true); +$router->match('/1/login', 'GET'); +$end = microtime(true); +$time = $end - $start; +$total_time += $time; +echo sprintf('Match time (existing route, no params): %.3f ms', $time * 1000) . PHP_EOL; + +// match a route with 1 parameter +$start = microtime(true); +$res = $router->match('/1/classes/foo', 'GET'); +$end = microtime(true); +$time = $end - $start; +$total_time += $time; +echo sprintf('Match time (existing route, 1 param): %.3f ms', $time * 1000) . PHP_EOL; + +// match a route with 2 parameters +$start = microtime(true); +$res = $router->match('/1/classes/foo/500', 'GET'); +$end = microtime(true); +$time = $end - $start; +$total_time += $time; +echo sprintf('Match time (existing route, 2 params): %.3f ms', $time * 1000) . PHP_EOL; + + +// match unexisting route +$start = microtime(true); +$router->match('/55-foo-bar', 'GET'); +$end = microtime(true); +$time = $end - $start; +$total_time += $time; +echo sprintf('Match time (unexisting route): %.3f ms', $time * 1000) . PHP_EOL; + +// print totals +echo sprintf('Total time: %.3f ms', $total_time * 1000) . PHP_EOL; +echo sprintf('Memory usage: %d KB', round(memory_get_usage() / 1024)) . PHP_EOL; +echo sprintf('Peak memory usage: %d KB', round(memory_get_peak_usage(true) / 1024)) . PHP_EOL; diff --git a/tests/benchmark.php b/tests/benchmark.php deleted file mode 100644 index ff469e9..0000000 --- a/tests/benchmark.php +++ /dev/null @@ -1,92 +0,0 @@ - - * - * Options: - * - * : - * The number of routes to map & match. Defaults to 1000. - */ - -require __DIR__ . '/../vendor/autoload.php'; - -global $argv; -$n = isset($argv[1]) ? intval($argv[1]) : 1000; - -// generates a random request url -function random_request_url() -{ - $characters = 'abcdefghijklmnopqrstuvwxyz'; - $charactersLength = strlen($characters); - $randomString = '/'; - - // create random path of 5-20 characters - for ($i = 0; $i < rand(5, 20); $i++) { - $randomString .= $characters[rand(0, $charactersLength - 1)]; - - if (rand(1, 10) === 1) { - $randomString .= '/'; - } - } - - // add dynamic route with 10% chance - if (rand(1, 10) === 1) { - $randomString = rtrim($randomString, '/') . '/[:part]'; - } - - return $randomString; -} - -// generate a random request method -function random_request_method() -{ - static $methods = [ 'GET', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' ]; - $random_key = array_rand($methods); - return $methods[ $random_key ]; -} - -// prepare benchmark data -$requests = []; -for ($i=0; $i<$n; $i++) { - $requests[] = [ - 'method' => random_request_method(), - 'url' => random_request_url(), - ]; -} - -$router = new AltoRouter(); - -// map requests -$start = microtime(true); -foreach ($requests as $r) { - $router->map($r['method'], $r['url'], ''); -} -$end = microtime(true); -$map_time = ($end - $start) * 1000; -echo sprintf('Map time: %.2f ms', $map_time) . PHP_EOL; - - -// pick random route to match -$r = $requests[array_rand($requests)]; - -// match random known route -$start = microtime(true); -$router->match($r['url'], $r['method']); -$end = microtime(true); -$match_time_known_route = ($end - $start) * 1000; -echo sprintf('Match time (known route): %.2f ms', $match_time_known_route) . PHP_EOL; - -// match unexisting route -$start = microtime(true); -$router->match('/55-foo-bar', 'GET'); -$end = microtime(true); -$match_time_unknown_route = ($end - $start) * 1000; -echo sprintf('Match time (unknown route): %.2f ms', $match_time_unknown_route) . PHP_EOL; - -// print totals -echo sprintf('Total time: %.2f seconds', ($map_time + $match_time_known_route + $match_time_unknown_route)) . PHP_EOL; -echo sprintf('Memory usage: %d KB', round(memory_get_usage() / 1024)) . PHP_EOL; -echo sprintf('Peak memory usage: %d KB', round(memory_get_peak_usage(true) / 1024)) . PHP_EOL;