createRequest('/api/health', Method::GET); $result = $this->benchmark( operation: fn() => $this->router->match($request), iterations: 10000, name: 'Static Route Matching' ); // Assert performance threshold: static routes should be very fast $this->assertPerformanceThreshold($result, maxAvgTimeMs: 0.1); // Record metrics $this->recordBenchmark($result, PerformanceCategory::ROUTING); return $result; } /** * Benchmark dynamic route matching (with parameters) */ public function benchmarkDynamicRouteMatching(): PerformanceBenchmarkResult { $request = $this->createRequest('/api/users/123', Method::GET); $result = $this->benchmark( operation: fn() => $this->router->match($request), iterations: 10000, name: 'Dynamic Route Matching' ); // Assert performance threshold: dynamic routes can be slightly slower $this->assertPerformanceThreshold($result, maxAvgTimeMs: 0.5); $this->recordBenchmark($result, PerformanceCategory::ROUTING); return $result; } /** * Benchmark complex route matching (multiple parameters) */ public function benchmarkComplexRouteMatching(): PerformanceBenchmarkResult { $request = $this->createRequest('/api/v1/users/123/posts/456/comments', Method::GET); $result = $this->benchmark( operation: fn() => $this->router->match($request), iterations: 10000, name: 'Complex Route Matching' ); // Assert performance threshold: complex routes with multiple params $this->assertPerformanceThreshold($result, maxAvgTimeMs: 1.0); $this->recordBenchmark($result, PerformanceCategory::ROUTING); return $result; } /** * Benchmark route matching with query parameters */ public function benchmarkRouteWithQueryParams(): PerformanceBenchmarkResult { $request = $this->createRequest( '/api/search?q=test&limit=10&offset=0&sort=name', Method::GET ); $result = $this->benchmark( operation: fn() => $this->router->match($request), iterations: 10000, name: 'Route Matching with Query Params' ); $this->assertPerformanceThreshold($result, maxAvgTimeMs: 0.2); $this->recordBenchmark($result, PerformanceCategory::ROUTING); return $result; } /** * Benchmark route not found scenario */ public function benchmarkRouteNotFound(): PerformanceBenchmarkResult { $request = $this->createRequest('/non-existent-route', Method::GET); $result = $this->benchmark( operation: function() use ($request) { try { $this->router->match($request); } catch (\Exception $e) { // Expected - route not found } }, iterations: 10000, name: 'Route Not Found' ); // Route not found should still be fast (fail fast principle) $this->assertPerformanceThreshold($result, maxAvgTimeMs: 0.1); $this->recordBenchmark($result, PerformanceCategory::ROUTING); return $result; } /** * Benchmark POST route matching */ public function benchmarkPostRouteMatching(): PerformanceBenchmarkResult { $request = $this->createRequest('/api/users', Method::POST); $result = $this->benchmark( operation: fn() => $this->router->match($request), iterations: 10000, name: 'POST Route Matching' ); $this->assertPerformanceThreshold($result, maxAvgTimeMs: 0.2); $this->recordBenchmark($result, PerformanceCategory::ROUTING); return $result; } /** * Run all routing benchmarks and return summary */ public function runAllBenchmarks(): array { return [ 'static_route' => $this->benchmarkStaticRouteMatching(), 'dynamic_route' => $this->benchmarkDynamicRouteMatching(), 'complex_route' => $this->benchmarkComplexRouteMatching(), 'query_params' => $this->benchmarkRouteWithQueryParams(), 'route_not_found' => $this->benchmarkRouteNotFound(), 'post_route' => $this->benchmarkPostRouteMatching(), ]; } /** * Helper: Create HTTP request for testing */ private function createRequest(string $uri, Method $method): HttpRequest { $parsedUri = ParsedUri::fromString('https://localhost' . $uri); $server = new ServerEnvironment([ 'REQUEST_METHOD' => $method->value, 'REQUEST_URI' => $uri, 'SERVER_NAME' => 'localhost', 'SERVER_PORT' => '443', 'HTTPS' => 'on' ]); return new HttpRequest( method: $method, uri: $parsedUri, server: $server, headers: [], body: '', parsedBody: null, queryParameters: $parsedUri->query ?? [], cookies: [], files: [] ); } }