mirror of
https://github.com/dannyvankooten/AltoRouter.git
synced 2025-08-06 00:17:29 +02:00
Add benchmark script to tests directory for easy benchmarking.
This commit is contained in:
54
tests/benchmark.php
Normal file
54
tests/benchmark.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Benchmark Altorouter
|
||||
*
|
||||
* Usage: php ./tests/benchmark.php <iterations>
|
||||
*
|
||||
* Options:
|
||||
*
|
||||
* <iterations>:
|
||||
* 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( $length = 20 ) {
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyz/';
|
||||
$charactersLength = strlen($characters);
|
||||
$randomString = '/';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||||
}
|
||||
return $randomString;
|
||||
}
|
||||
|
||||
// generate a random request method
|
||||
function random_request_method() {
|
||||
static $methods = array( 'GET', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'GET|POST', 'PUT|POST' );
|
||||
$random_key = array_rand( $methods );
|
||||
return $methods[ $random_key ];
|
||||
}
|
||||
|
||||
|
||||
$router = new AltoRouter();
|
||||
|
||||
// map 1000 random routes
|
||||
for( $i=0; $i < $n; $i++ ) {
|
||||
$router->map( random_request_method(), random_request_url(), function() {} );
|
||||
}
|
||||
|
||||
// match 1000 random routes
|
||||
$start = microtime(true);
|
||||
for( $i=0; $i < $n; $i++ ) {
|
||||
$router->match( random_request_url(), random_request_method() );
|
||||
}
|
||||
$end = microtime( true );
|
||||
|
||||
// print execution time
|
||||
echo "Time: " . number_format(( $end - $start ), 4 ). ' seconds' . PHP_EOL;
|
||||
echo "Peak memory usage: " . ( memory_get_peak_usage( true ) / 1024 / 1024 ) . 'MB';
|
Reference in New Issue
Block a user