2024-09-07 13:02:52 -05:00
|
|
|
<?php
|
|
|
|
|
2024-09-12 09:28:53 -05:00
|
|
|
require_once 'color.php';
|
|
|
|
require_once __DIR__ . '/../src/Router.php';
|
2024-09-13 12:35:42 -05:00
|
|
|
require_once __DIR__ . '/../src/SegmentRouter.php';
|
2024-09-12 09:28:53 -05:00
|
|
|
|
2024-09-13 12:35:42 -05:00
|
|
|
const ROUTES = __DIR__ . '/routes/';
|
|
|
|
const TREES = __DIR__ . '/trees/';
|
|
|
|
|
|
|
|
use Sharkk\Router\SegmentRouter;
|
2024-09-12 09:28:53 -05:00
|
|
|
|
2024-09-11 07:31:14 -05:00
|
|
|
// if there's a flag, reset the opcache
|
|
|
|
if (in_array('-f', $argv)) {
|
|
|
|
opcache_reset();
|
|
|
|
echoTitle("opcache reset");
|
|
|
|
}
|
|
|
|
|
2024-09-13 22:16:24 -05:00
|
|
|
// create the trees directory if it doesn't exist
|
|
|
|
if (!is_dir(TREES)) mkdir(TREES);
|
|
|
|
|
2024-09-13 12:35:42 -05:00
|
|
|
$r = new SegmentRouter();
|
2024-09-12 09:28:53 -05:00
|
|
|
|
|
|
|
// Blog lookups
|
2024-09-13 12:35:42 -05:00
|
|
|
$blog = readAndAddRoutes(ROUTES . 'blog.txt', $r);
|
|
|
|
writeRoutesToFile($r->routes, TREES . 'blog.txt');
|
2024-09-12 09:28:53 -05:00
|
|
|
echoTitle("Starting blog lookups");
|
|
|
|
runIterations(10000, $r, $blog);
|
|
|
|
runIterations(100000, $r, $blog);
|
|
|
|
runIterations(1000000, $r, $blog);
|
|
|
|
unset($blog);
|
|
|
|
|
|
|
|
// Github lookups
|
|
|
|
$r->clear();
|
2024-09-13 12:35:42 -05:00
|
|
|
$github = readAndAddRoutes(ROUTES . 'github.txt', $r);
|
|
|
|
writeRoutesToFile($r->routes, TREES . 'github.txt');
|
2024-09-12 09:28:53 -05:00
|
|
|
echoTitle("Starting github lookups");
|
|
|
|
runIterations(10000, $r, $github);
|
|
|
|
runIterations(100000, $r, $github);
|
|
|
|
runIterations(1000000, $r, $github);
|
|
|
|
unset($github);
|
|
|
|
|
|
|
|
// Big lookups
|
|
|
|
$r->clear();
|
2024-09-13 12:35:42 -05:00
|
|
|
$big = readAndAddRoutes(ROUTES . 'big.txt', $r);
|
|
|
|
writeRoutesToFile($r->routes, TREES . 'big.txt');
|
2024-09-12 09:28:53 -05:00
|
|
|
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);
|
|
|
|
}
|
2024-09-07 13:02:52 -05:00
|
|
|
|
2024-09-12 09:28:53 -05:00
|
|
|
for ($i = 0; $i < 10; $i++) {
|
|
|
|
[$method, $uri] = $routes[array_rand($routes)];
|
2024-09-07 13:02:52 -05:00
|
|
|
|
2024-09-12 09:28:53 -05:00
|
|
|
// 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);
|
2024-09-07 13:02:52 -05:00
|
|
|
|
2024-09-12 09:28:53 -05:00
|
|
|
$res = $r->lookup($method, $uri);
|
|
|
|
if ($res['code'] !== 200) {
|
|
|
|
echo "Failed to handle request for $uri - $res\n";
|
|
|
|
exit(1);
|
2024-09-07 13:02:52 -05:00
|
|
|
}
|
2024-09-12 09:28:53 -05:00
|
|
|
$res['handler'](...$res['params']);
|
2024-09-07 13:02:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function echoTitle(string $title) {
|
|
|
|
echo "\n";
|
|
|
|
echo Color::bold(Color::black("===============================================================")) . "\n";
|
|
|
|
echo "\n";
|
|
|
|
echo Color::bold(Color::blue($title))."\n";
|
|
|
|
echo "\n";
|
|
|
|
echo Color::bold(Color::black("===============================================================")) . "\n";
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
|
2024-09-13 12:35:42 -05:00
|
|
|
function readAndAddRoutes(string $file, &$r): array
|
2024-09-07 13:02:52 -05:00
|
|
|
{
|
|
|
|
$array = [];
|
|
|
|
$routes = file($file);
|
|
|
|
foreach ($routes as $route) {
|
|
|
|
[$method, $path] = explode(' ', $route);
|
|
|
|
$path = trim($path);
|
|
|
|
$array[] = [$method, $path];
|
|
|
|
$r->add($method, $path, function() {
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
function runIterations(int $iterations, $r, array $routes) {
|
2024-09-11 11:40:13 -05:00
|
|
|
echo "\n🚀 Running $iterations iterations...\n";
|
|
|
|
|
|
|
|
$start = microtime(true); // start the timer
|
|
|
|
$reqs = 0; // track the timing of lookups
|
|
|
|
$longest = 0; // track the longest lookup time
|
|
|
|
$shortest = 0; // track the shortest lookup time
|
|
|
|
$longestRoute = '';
|
|
|
|
$shortestRoute = '';
|
|
|
|
|
2024-09-07 13:02:52 -05:00
|
|
|
for ($i = 0; $i < $iterations; $i++) {
|
|
|
|
// pick a random route from the array
|
2024-09-07 17:41:20 -05:00
|
|
|
[$method, $path] = $routes[array_rand($routes)];
|
2024-09-11 11:40:13 -05:00
|
|
|
|
2024-09-07 17:41:20 -05:00
|
|
|
// replace all :params/ with random values
|
|
|
|
$uri = preg_replace_callback('/:(\w+)/', function($matches) {
|
|
|
|
return rand(1, 100);
|
|
|
|
}, $path);
|
2024-09-11 11:40:13 -05:00
|
|
|
|
|
|
|
$start2 = microtime(true); // start the timer for the lookup
|
|
|
|
|
|
|
|
$res = $r->lookup($method, $uri); // run the lookup
|
|
|
|
|
|
|
|
$reqs += microtime(true) - $start2; // add this lookup time
|
|
|
|
if ($shortest == 0 || microtime(true) - $start2 < $shortest) {
|
|
|
|
$shortest = microtime(true) - $start2; // track the shortest lookup time
|
|
|
|
$shortestRoute = "$method $uri";
|
|
|
|
}
|
|
|
|
if (microtime(true) - $start2 > $longest) {
|
|
|
|
$longest = microtime(true) - $start2; // track the longest lookup time
|
|
|
|
$longestRoute = "$method $uri";
|
|
|
|
}
|
|
|
|
|
|
|
|
// if any error was encountered, print it and exit
|
2024-09-11 17:05:09 -05:00
|
|
|
if ($res['code'] !== 200) {
|
|
|
|
echo Color::red("Failed to handle request.\n$method {$res['code']}\n"."├─ URI: $uri\n└─ Path: $path\n");
|
2024-09-07 18:51:37 -05:00
|
|
|
echo Color::yellow("Completed $i iterations before failure.\n");
|
2024-09-07 13:02:52 -05:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2024-09-11 11:40:13 -05:00
|
|
|
echo Color::blue("✔️ Done!")."\n";
|
|
|
|
|
|
|
|
// echo peak memory usage
|
|
|
|
echo "Peak memory: " . Color::magenta(round(memory_get_peak_usage() / 1024, 1) . " kb\n");
|
|
|
|
|
|
|
|
// total time used for this run
|
2024-09-07 13:02:52 -05:00
|
|
|
echo "Time: " . Color::cyan(number_format(microtime(true) - $start, 10) . " s\n");
|
2024-09-11 11:40:13 -05:00
|
|
|
|
2024-09-07 13:02:52 -05:00
|
|
|
// echo the average time per request
|
2024-09-11 11:40:13 -05:00
|
|
|
echo "Avg/lookup: " . Color::yellow(number_format($reqs / $iterations, 10) . " s\n");
|
|
|
|
|
|
|
|
// echo the shortest lookup time
|
|
|
|
echo "Shortest lookup: " . Color::green(number_format($shortest, 10) . " s\n");
|
|
|
|
|
|
|
|
// echo the longest lookup time
|
|
|
|
echo "Longest lookup: " . Color::red(number_format($longest, 10) . " s\n");
|
|
|
|
|
|
|
|
echo Color::black("==============================================") . "\n";
|
|
|
|
|
|
|
|
// echo the longest and shortest routes
|
|
|
|
echo "Shortest route: " . Color::green($shortestRoute) . "\n";
|
|
|
|
echo "Longest route: " . Color::red($longestRoute) . "\n";
|
2024-09-07 13:02:52 -05:00
|
|
|
}
|
2024-09-07 16:39:57 -05:00
|
|
|
|
|
|
|
// take a route tree (array) and store it in a file to be read
|
|
|
|
function writeRoutesToFile(array $routes, string $file) {
|
|
|
|
// Clear the file before writing
|
|
|
|
file_put_contents($file, '');
|
|
|
|
|
|
|
|
$fp = fopen($file, 'w');
|
|
|
|
|
|
|
|
// write a / to the first line of the file
|
|
|
|
fwrite($fp, "/\n");
|
|
|
|
|
|
|
|
// Start writing from the root level with an indentation of 0 and no prefix
|
|
|
|
writeNode($routes, 0, '', $fp);
|
|
|
|
|
|
|
|
fclose($fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeNode($node, $indent, $prefix, $fp) {
|
|
|
|
$totalItems = count($node);
|
|
|
|
$currentItem = 0;
|
|
|
|
|
|
|
|
foreach ($node as $key => $value) {
|
|
|
|
$currentItem++;
|
|
|
|
$isLastChild = ($currentItem === $totalItems);
|
|
|
|
$connector = $isLastChild ? '└── ' : '├── ';
|
|
|
|
|
|
|
|
$key = empty($key) ? '/' : $key;
|
|
|
|
|
|
|
|
// Write the current node's key with the tree symbol
|
|
|
|
fwrite($fp, $prefix . $connector . $key . "\n");
|
|
|
|
|
|
|
|
// If the value is an array, it represents a child node, so recurse
|
|
|
|
if (is_array($value)) {
|
|
|
|
$newPrefix = $prefix . ($isLastChild ? ' ' : '│ ');
|
|
|
|
writeNode($value, $indent + 1, $newPrefix, $fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|