mirror of
https://github.com/dannyvankooten/AltoRouter.git
synced 2025-01-16 20:08:12 +01:00
phpcs formatting
This commit is contained in:
parent
cd918de64b
commit
a01166760c
@ -12,10 +12,10 @@ if (file_exists($_SERVER['SCRIPT_FILENAME']) && pathinfo($_SERVER['SCRIPT_FILENA
|
||||
|
||||
$router = new AltoRouter();
|
||||
$router->setBasePath('/AltoRouter/examples/basic');
|
||||
$router->map('GET|POST','/', 'home#index', 'home');
|
||||
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
|
||||
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
|
||||
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
|
||||
$router->map('GET|POST', '/', 'home#index', 'home');
|
||||
$router->map('GET', '/users/', ['c' => 'UserController', 'a' => 'ListAction']);
|
||||
$router->map('GET', '/users/[i:id]', 'users#show', 'users_show');
|
||||
$router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
|
||||
|
||||
// match current request
|
||||
$match = $router->match();
|
||||
@ -24,12 +24,12 @@ $match = $router->match();
|
||||
|
||||
<h3>Current request: </h3>
|
||||
<pre>
|
||||
Target: <?php var_dump($match['target']); ?>
|
||||
Params: <?php var_dump($match['params']); ?>
|
||||
Name: <?php var_dump($match['name']); ?>
|
||||
Target: <?php var_dump($match['target']); ?>
|
||||
Params: <?php var_dump($match['params']); ?>
|
||||
Name: <?php var_dump($match['name']); ?>
|
||||
</pre>
|
||||
|
||||
<h3>Try these requests: </h3>
|
||||
<p><a href="<?php echo $router->generate('home'); ?>">GET <?php echo $router->generate('home'); ?></a></p>
|
||||
<p><a href="<?php echo $router->generate('users_show', array('id' => 5)); ?>">GET <?php echo $router->generate('users_show', array('id' => 5)); ?></a></p>
|
||||
<p><form action="<?php echo $router->generate('users_do', array('id' => 10, 'action' => 'update')); ?>" method="post"><button type="submit"><?php echo $router->generate('users_do', array('id' => 10, 'action' => 'update')); ?></button></form></p>
|
||||
<p><a href="<?php echo $router->generate('users_show', ['id' => 5]); ?>">GET <?php echo $router->generate('users_show', ['id' => 5]); ?></a></p>
|
||||
<p><form action="<?php echo $router->generate('users_do', ['id' => 10, 'action' => 'update']); ?>" method="post"><button type="submit"><?php echo $router->generate('users_do', ['id' => 10, 'action' => 'update']); ?></button></form></p>
|
||||
|
@ -14,10 +14,11 @@
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
global $argv;
|
||||
$n = isset( $argv[1] ) ? intval( $argv[1] ) : 1000;
|
||||
$n = isset($argv[1]) ? intval($argv[1]) : 1000;
|
||||
|
||||
// generates a random request url
|
||||
function random_request_url() {
|
||||
function random_request_url()
|
||||
{
|
||||
$characters = 'abcdefghijklmnopqrstuvwxyz';
|
||||
$charactersLength = strlen($characters);
|
||||
$randomString = '/';
|
||||
@ -26,45 +27,46 @@ function random_request_url() {
|
||||
for ($i = 0; $i < rand(5, 20); $i++) {
|
||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||||
|
||||
if( rand(1, 10) === 1 ) {
|
||||
$randomString .= '/';
|
||||
if (rand(1, 10) === 1) {
|
||||
$randomString .= '/';
|
||||
}
|
||||
}
|
||||
|
||||
// add dynamic route with 10% chance
|
||||
if ( rand(1, 10) === 1 ) {
|
||||
$randomString = rtrim( $randomString, '/' ) . '/[:part]';
|
||||
if (rand(1, 10) === 1) {
|
||||
$randomString = rtrim($randomString, '/') . '/[:part]';
|
||||
}
|
||||
|
||||
return $randomString;
|
||||
}
|
||||
|
||||
// generate a random request method
|
||||
function random_request_method() {
|
||||
static $methods = array( 'GET', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' );
|
||||
$random_key = array_rand( $methods );
|
||||
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 = array();
|
||||
for($i=0; $i<$n; $i++) {
|
||||
$requests[] = array(
|
||||
$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) {
|
||||
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;
|
||||
echo sprintf('Map time: %.2f ms', $map_time) . PHP_EOL;
|
||||
|
||||
|
||||
// pick random route to match
|
||||
@ -75,19 +77,16 @@ $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;
|
||||
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;
|
||||
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;
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user