211 lines
6.1 KiB
PHP
211 lines
6.1 KiB
PHP
<?php
|
|
|
|
// if there's a flag, reset the opcache
|
|
if (in_array('-f', $argv)) {
|
|
opcache_reset();
|
|
echoTitle("opcache reset");
|
|
}
|
|
|
|
require_once __DIR__ . '/../Router.php';
|
|
require_once __DIR__ . '/../SegmentRouter.php';
|
|
require_once __DIR__ . '/../TrieRouter.php';
|
|
|
|
// A simple class to return a string wrapped in ANSI color codes for terminal output
|
|
class Color {
|
|
const RESET = "\033[0m";
|
|
const BOLD = "\033[1m";
|
|
const UNDERLINE = "\033[4m";
|
|
const INVERSE = "\033[7m";
|
|
const BLACK = "\033[30m";
|
|
const RED = "\033[31m";
|
|
const GREEN = "\033[32m";
|
|
const YELLOW = "\033[33m";
|
|
const BLUE = "\033[34m";
|
|
const MAGENTA = "\033[35m";
|
|
const CYAN = "\033[36m";
|
|
const WHITE = "\033[37m";
|
|
|
|
private static function format(string $color, string $string): string {
|
|
return $color . $string . self::RESET;
|
|
}
|
|
|
|
public static function bold(string $string): string {
|
|
return self::format(self::BOLD, $string);
|
|
}
|
|
|
|
public static function underline(string $string): string {
|
|
return self::format(self::UNDERLINE, $string);
|
|
}
|
|
|
|
public static function inverse(string $string): string {
|
|
return self::format(self::INVERSE, $string);
|
|
}
|
|
|
|
public static function black(string $string): string {
|
|
return self::format(self::BLACK, $string);
|
|
}
|
|
|
|
public static function red(string $string): string {
|
|
return self::format(self::RED, $string);
|
|
}
|
|
|
|
public static function green(string $string): string {
|
|
return self::format(self::GREEN, $string);
|
|
}
|
|
|
|
public static function yellow(string $string): string {
|
|
return self::format(self::YELLOW, $string);
|
|
}
|
|
|
|
public static function blue(string $string): string {
|
|
return self::format(self::BLUE, $string);
|
|
}
|
|
|
|
public static function magenta(string $string): string {
|
|
return self::format(self::MAGENTA, $string);
|
|
}
|
|
|
|
public static function cyan(string $string): string {
|
|
return self::format(self::CYAN, $string);
|
|
}
|
|
|
|
public static function white(string $string): string {
|
|
return self::format(self::WHITE, $string);
|
|
}
|
|
}
|
|
|
|
function echoMemoryAndTime(int $i, float $start) {
|
|
echo "(".Color::green("$i lookups").") M: " .
|
|
Color::blue(round(memory_get_usage() / 1024, 1) . " kb") .
|
|
" - T: ". Color::blue(number_format(microtime(true) - $start, 10) ." s") .
|
|
"\n";
|
|
}
|
|
|
|
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";
|
|
}
|
|
|
|
function readAndAddRoutes(string $file, Router &$r): array
|
|
{
|
|
$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) {
|
|
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 = '';
|
|
|
|
for ($i = 0; $i < $iterations; $i++) {
|
|
// pick a random route from the array
|
|
[$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); // 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
|
|
if ($res['code'] !== 200) {
|
|
echo Color::red("Failed to handle request.\n$method {$res['code']}\n"."├─ URI: $uri\n└─ Path: $path\n");
|
|
echo Color::yellow("Completed $i iterations before failure.\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
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
|
|
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($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";
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
}
|
|
|