Router/tests/static.php

45 lines
1.1 KiB
PHP

<?php
require_once 'tools.php';
require_once __DIR__ . '/../StaticRouter.php';
$r = new SegmentRouter();
// Big test
$big = readRoutes('big.txt');
foreach ($big as $route) {
[$method, $path] = $route;
$r->add($method, $path, function() {
return true;
});
}
echoTitle("Starting big lookups");
$start = microtime(true);
$reqs = 0;
for ($i = 0; $i < 1000000; $i++) {
$index = array_rand($big);
[$method, $path] = $big[$index];
$rstart = microtime(true);
$res = StaticRouter::lookup($r->routes, $method, $path);
if ($res === 404 || $res === 405) {
die("404 or 405\n");
}
$reqs += microtime(true) - $rstart;
}
echo "Time: " . Color::cyan(number_format(microtime(true) - $start, 10) . " s\n");
echo "Peak memory: " . Color::magenta(round(memory_get_peak_usage() / 1024, 1) . " kb\n");
echo "Avg/lookup: " . Color::yellow(number_format($reqs / 1000000, 10) . " s\n");
function readRoutes(string $file): array
{
$array = [];
$routes = file($file);
foreach ($routes as $route) {
[$method, $path] = explode(' ', $route);
$path = trim($path);
$array[] = [$method, $path];
}
return $array;
}