Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ProcessMaker/Http/Middleware/ServerTimingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function handle(Request $request, Closure $next): Response
return $next($request);
}

ProcessMakerServiceProvider::beginRequestTiming();

// Start time for controller execution
$startController = microtime(true);

Expand Down
8 changes: 8 additions & 0 deletions ProcessMaker/Providers/ProcessMakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,14 @@ public static function getBootTime(): ?float
return self::$bootTime;
}

/**
* Reset per-request query timing metrics.
*/
public static function beginRequestTiming(): void
{
self::$queryTime = 0;
}

/**
* Get the query time for the request.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Feature/ServerTimingMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Support\Facades\Route;
use ProcessMaker\Http\Middleware\ServerTimingMiddleware;
use ProcessMaker\Models\User;
use ProcessMaker\Providers\ProcessMakerServiceProvider;
use ReflectionClass;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;

Expand Down Expand Up @@ -41,6 +43,28 @@ public function testServerTimingHeaderIncludesAllMetrics()
$this->assertStringContainsString('db;dur=', $serverTiming[2]);
}

public function testBeginRequestTimingClearsAccumulatedQueryTime()
{
$reflection = new ReflectionClass(ProcessMakerServiceProvider::class);
$property = $reflection->getProperty('queryTime');
$property->setAccessible(true);
$property->setValue(null, 500);

Route::middleware(ServerTimingMiddleware::class)->get('/timing-reset-test', function () {
DB::select('SELECT 1');

return response()->json(['message' => 'Timing reset test']);
});

$response = $this->get('/timing-reset-test');
$serverTiming = $this->getHeader($response, 'server-timing');

preg_match('/db;dur=([\d.]+)/', implode(',', $serverTiming), $matches);
$dbTime = (float) ($matches[1] ?? 500);

$this->assertLessThan(500, $dbTime);
}

public function testQueryTimeIsMeasured()
{
// Mock a route with a query
Expand Down
Loading