84 lines
2.4 KiB
PHP
84 lines
2.4 KiB
PHP
<?php
|
|
|
|
/*
|
|
This test file puts the SegmentRouter to the test by running a million lookups on a handful
|
|
of routes. The routes are read from two files, blog.txt and github.txt, and added to two separate
|
|
routers. The lookups are then run in two separate loops, one for each router.
|
|
Each lookup is timed and the memory usage is also printed out at regular intervals.
|
|
The requests are randomly picked from the array of routes.
|
|
*/
|
|
|
|
require_once 'tools.php';
|
|
|
|
$r = new SegmentRouter();
|
|
|
|
// Blog lookups
|
|
$blog = readAndAddRoutes('blog.txt', $r);
|
|
writeRoutesToFile($r->routes, 'storage/segment/blog.txt');
|
|
echoTitle("Starting blog lookups");
|
|
runIterations(10000, $r, $blog);
|
|
runIterations(100000, $r, $blog);
|
|
runIterations(1000000, $r, $blog);
|
|
unset($blog);
|
|
|
|
// Github lookups
|
|
$r->clear();
|
|
$github = readAndAddRoutes('github.txt', $r);
|
|
writeRoutesToFile($r->routes, 'storage/segment/github.txt');
|
|
echoTitle("Starting github lookups");
|
|
runIterations(10000, $r, $github);
|
|
runIterations(100000, $r, $github);
|
|
runIterations(1000000, $r, $github);
|
|
unset($github);
|
|
|
|
// Big lookups
|
|
$r->clear();
|
|
$big = readAndAddRoutes('big.txt', $r);
|
|
writeRoutesToFile($r->routes, 'storage/segment/big.txt');
|
|
echoTitle("Starting big lookups");
|
|
runIterations(10000, $r, $big);
|
|
runIterations(100000, $r, $big);
|
|
runIterations(1000000, $r, $big);
|
|
unset($big);
|
|
|
|
// Parameter testing
|
|
$r->clear();
|
|
echoTitle("Testing parameters");
|
|
|
|
$routes = [
|
|
['GET', '/blog/:id', function($id) {
|
|
echo $id."\n";
|
|
}],
|
|
['GET', '/blog/:id/:slug', function($id, $slug) {
|
|
echo $id . ' - ' . $slug."\n";
|
|
}],
|
|
['GET', '/blog/:id/:slug/:page', function($id, $slug, $page) {
|
|
echo $id . ' - ' . $slug . ' - ' . $page."\n";
|
|
}],
|
|
['GET', '/blog/:id/:slug/:page/:extra', function($id, $slug, $page, $extra) {
|
|
echo $id . ' - ' . $slug . ' - ' . $page . ' - ' . $extra."\n";
|
|
}],
|
|
];
|
|
|
|
foreach ($routes as $route) {
|
|
[$method, $path, $handler] = $route;
|
|
$r->add($method, $path, $handler);
|
|
}
|
|
|
|
for ($i = 0; $i < 10; $i++) {
|
|
[$method, $uri] = $routes[array_rand($routes)];
|
|
|
|
// Generate some random parameters
|
|
$uri = str_replace(':id', rand(1, 100), $uri);
|
|
$uri = str_replace(':slug', 'slug-' . rand(1, 100), $uri);
|
|
$uri = str_replace(':page', rand(1, 100), $uri);
|
|
$uri = str_replace(':extra', 'extra-' . rand(1, 100), $uri);
|
|
|
|
$res = $r->lookup($method, $uri);
|
|
if ($res === 404 || $res === 405) {
|
|
echo "Failed to handle request for $uri - $res\n";
|
|
exit(1);
|
|
}
|
|
$res[0](...$res[1]);
|
|
}
|