From 79a068ceaa29dcacec1b5c8b25b0de059dcdb084 Mon Sep 17 00:00:00 2001 From: Sebastian Helzle Date: Wed, 12 May 2021 14:16:06 +0200 Subject: [PATCH] BUGFIX: Measure server timing again Due to the switch to middlewares the start variable was not persisted until the end of the request. Also due to a configuration mistake the finalisiing middleware was never triggered. --- .../Middleware/AddServerTimingMiddleware.php | 1 + .../Middleware/MeasureServerTimingMiddleware.php | 9 ++++++--- Classes/Service/DebugService.php | 16 ++++++++++++---- Configuration/Settings.yaml | 4 ++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Classes/Http/Middleware/AddServerTimingMiddleware.php b/Classes/Http/Middleware/AddServerTimingMiddleware.php index 87bbcbe..9b7c009 100644 --- a/Classes/Http/Middleware/AddServerTimingMiddleware.php +++ b/Classes/Http/Middleware/AddServerTimingMiddleware.php @@ -49,6 +49,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface } $serverTiming = ''; + $this->debugService->setStartRequestAt($request->getAttribute(MeasureServerTimingMiddleware::TIMING_ATTRIBUTE)); $metrics = $this->debugService->getMetrics(); foreach ($metrics as $key => ['value' => $value, 'description' => $description]) { $serverTiming .= ($serverTiming ? ', ' : '') . $key; diff --git a/Classes/Http/Middleware/MeasureServerTimingMiddleware.php b/Classes/Http/Middleware/MeasureServerTimingMiddleware.php index 5831d1b..c20815b 100644 --- a/Classes/Http/Middleware/MeasureServerTimingMiddleware.php +++ b/Classes/Http/Middleware/MeasureServerTimingMiddleware.php @@ -23,6 +23,8 @@ class MeasureServerTimingMiddleware implements MiddlewareInterface { + public const TIMING_ATTRIBUTE = 't3nNeosDebugTimingStart'; + /** * @Flow\InjectConfiguration(path="serverTimingHeader.enabled") * @@ -39,10 +41,11 @@ class MeasureServerTimingMiddleware implements MiddlewareInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - $response = $handler->handle($request); - if ($this->enabled) { - $this->debugService->startRequestTimer(); + $timerStart = $this->debugService->startRequestTimer(); + $response = $handler->handle($request->withAttribute(self::TIMING_ATTRIBUTE, $timerStart)); + } else { + $response = $handler->handle($request); } return $response; diff --git a/Classes/Service/DebugService.php b/Classes/Service/DebugService.php index d9e3fbb..5661fb0 100644 --- a/Classes/Service/DebugService.php +++ b/Classes/Service/DebugService.php @@ -22,12 +22,12 @@ class DebugService { /** - * @var int + * @var float */ protected $startRequestAt; /** - * @var int + * @var float */ protected $stopRequestAt; @@ -39,9 +39,17 @@ class DebugService /** * Starts the timer for the request process */ - public function startRequestTimer(): void + public function startRequestTimer(): float { - $this->startRequestAt = microtime(true) * 1000; + return $this->startRequestAt = microtime(true) * 1000; + } + + /** + * Sets the starttime of the request + */ + public function setStartRequestAt(float $startRequestAt): void + { + $this->startRequestAt = $startRequestAt; } /** diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index f0f95ea..971b6c8 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -20,10 +20,10 @@ Neos: http: middlewares: t3n.Neos.Debug:MeasureServerTiming: - position: 'start' + position: 'start 999' middleware: 't3n\Neos\Debug\Http\Middleware\MeasureServerTimingMiddleware' t3n.Neos.Debug:AddServerTimingHeader: - position: 'after dispatch' + position: 'before dispatch 999' middleware: 't3n\Neos\Debug\Http\Middleware\AddServerTimingMiddleware' reflection: