Compare commits

...

2 Commits

4 changed files with 40 additions and 10 deletions

View File

@ -8,6 +8,11 @@
The requests are randomly picked from the array of routes.
*/
// if there's a flag, reset the opcache
if (in_array('-f', $argv)) {
opcache_reset();
}
require_once 'tools.php';
$r = new SegmentRouter();

View File

@ -1,5 +1,18 @@
<?php
/*
This test file puts the SimpleRouter 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 and two separate arrays. 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.
*/
// if there's a flag, reset the opcache
if (in_array('-f', $argv)) {
opcache_reset();
}
require_once 'tools.php';
require_once __DIR__ . '/../SimpleRouter.php';
@ -17,13 +30,15 @@ sRunIterations(10000, $github);
sRunIterations(100000, $github);
sRunIterations(1000000, $github);
// Big test
SimpleRouter::clearRoutes();
$big = sReadAndAddRoutes('big.txt');
echoTitle("Starting big lookups");
sRunIterations(10000, $big);
sRunIterations(100000, $big);
sRunIterations(1000000, $big);
// Big test; since simplerouter is so much slower, we'll only run the big test if the -b flag is passed
if (in_array('-b', $argv)) {
SimpleRouter::clearRoutes();
$big = sReadAndAddRoutes('big.txt');
echoTitle("Starting big lookups");
sRunIterations(10000, $big);
sRunIterations(100000, $big);
sRunIterations(1000000, $big);
}
function sReadAndAddRoutes(string $file): array
{

View File

@ -106,13 +106,18 @@ function runIterations(int $iterations, $r, array $routes) {
$interval = $iterations / 10;
for ($i = 0; $i < $iterations; $i++) {
// pick a random route from the array
[$method, $uri] = $routes[array_rand($routes)];
[$method, $path] = $routes[array_rand($routes)];
// replace all :params/ with random values
$uri = preg_replace_callback('/:(\w+)/', function($matches) {
return rand(1, 100);
}, $path);
$start2 = microtime(true);
$res = $r->lookup($method, $uri);
if ($res === 404 || $res === 405) {
echo Color::red("Failed to handle request for $uri - $res\n");
echo Color::red("Failed to handle request.\n$method $res\n"."├─ URI: $uri\n├─ Path: $path\n");
exit(1);
}
if ($i !== 0 && $i % ($interval) === 0) echoMemoryAndTime($i, $start);
if ($i !== 0 && $i % ($interval) === 0) echoMemoryAndTime($i, $start2);
}
echo "Time: " . Color::cyan(number_format(microtime(true) - $start, 10) . " s\n");
// echo the average time per request

View File

@ -8,6 +8,11 @@
The requests are randomly picked from the array of routes.
*/
// if there's a flag, reset the opcache
if (in_array('-f', $argv)) {
opcache_reset();
}
require_once 'tools.php';
$r = new TrieRouter();