add($method, $path, function() { return true; }); } return $array; } function runIterations(int $iterations, $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->lookup($method, $uri); if ($res === 404 || $res === 405) { echo Color::red("Failed to handle request for $uri - $res\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"; } // 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, ''); // Open the file for writing $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); // Close the file 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); } } }