61 lines
1.9 KiB
PHP
61 lines
1.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
This test file puts the PATRICIA trie 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.
|
|
*/
|
|
|
|
require_once __DIR__ . '/../TrieRouter.php';
|
|
require_once 'tools.php';
|
|
|
|
// Blog router
|
|
$b = new TrieRouter();
|
|
$blog = readAndAddRoutes('blog.txt', $b);
|
|
|
|
// Github router
|
|
$g = new TrieRouter();
|
|
$github = readAndAddRoutes('github.txt', $g);
|
|
|
|
// Big router
|
|
$big = new TrieRouter();
|
|
$bigRoutes = readAndAddRoutes('big.txt', $big);
|
|
|
|
function runIterations(int $iterations, TrieRouter $r, array $routes) {
|
|
echo "Running $iterations iterations\n";
|
|
$start = microtime(true);
|
|
$interval = $iterations / 10;
|
|
for ($i = 0; $i < $iterations; $i++) {
|
|
// pick a random route from the array
|
|
[$method, $uri] = $routes[array_rand($routes)];
|
|
$res = $r->handleRequest($method, $uri);
|
|
if ($res !== true) {
|
|
echo "Failed to handle request for $uri\n";
|
|
exit(1);
|
|
}
|
|
if ($i !== 0 && $i % ($interval) === 0) echoMemoryAndTime($i, $start);
|
|
}
|
|
echo "Time: " . Color::cyan(number_format(microtime(true) - $start, 10) . " s\n");
|
|
// echo the average time per request
|
|
echo "Avg/lookup: " . Color::yellow(number_format((microtime(true) - $start) / $iterations, 10) . " s\n");
|
|
echo "\n";
|
|
}
|
|
|
|
echoTitle("Starting blog lookups");
|
|
runIterations(100000, $b, $blog);
|
|
runIterations(1000000, $b, $blog);
|
|
|
|
echoTitle("Starting github lookups");
|
|
runIterations(10000, $g, $github);
|
|
runIterations(100000, $g, $github);
|
|
runIterations(1000000, $g, $github);
|
|
|
|
echoTitle("Starting big lookups");
|
|
runIterations(10000, $big, $bigRoutes);
|
|
runIterations(100000, $big, $bigRoutes);
|
|
runIterations(1000000, $big, $bigRoutes);
|
|
|
|
exit(0);
|