Skip to content

Commit d0b19cf

Browse files
committed
feat(FOUR-32024): review and fix the calculation of DB timing when loaded by octane
1 parent 8c5f71f commit d0b19cf

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

ProcessMaker/Http/Middleware/ServerTimingMiddleware.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public function handle(Request $request, Closure $next): Response
2828
return $next($request);
2929
}
3030

31+
ProcessMakerServiceProvider::beginRequestTiming();
32+
3133
// Start time for controller execution
3234
$startController = microtime(true);
3335

ProcessMaker/Providers/ProcessMakerServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,14 @@ public static function getBootTime(): ?float
508508
return self::$bootTime;
509509
}
510510

511+
/**
512+
* Reset per-request query timing metrics.
513+
*/
514+
public static function beginRequestTiming(): void
515+
{
516+
self::$queryTime = 0;
517+
}
518+
511519
/**
512520
* Get the query time for the request.
513521
*

tests/Feature/ServerTimingMiddlewareTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Illuminate\Support\Facades\Route;
77
use ProcessMaker\Http\Middleware\ServerTimingMiddleware;
88
use ProcessMaker\Models\User;
9+
use ProcessMaker\Providers\ProcessMakerServiceProvider;
10+
use ReflectionClass;
911
use Tests\Feature\Shared\RequestHelper;
1012
use Tests\TestCase;
1113

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

46+
public function testBeginRequestTimingClearsAccumulatedQueryTime()
47+
{
48+
$reflection = new ReflectionClass(ProcessMakerServiceProvider::class);
49+
$property = $reflection->getProperty('queryTime');
50+
$property->setAccessible(true);
51+
$property->setValue(null, 500);
52+
53+
Route::middleware(ServerTimingMiddleware::class)->get('/timing-reset-test', function () {
54+
DB::select('SELECT 1');
55+
56+
return response()->json(['message' => 'Timing reset test']);
57+
});
58+
59+
$response = $this->get('/timing-reset-test');
60+
$serverTiming = $this->getHeader($response, 'server-timing');
61+
62+
preg_match('/db;dur=([\d.]+)/', implode(',', $serverTiming), $matches);
63+
$dbTime = (float) ($matches[1] ?? 500);
64+
65+
$this->assertLessThan(500, $dbTime);
66+
}
67+
4468
public function testQueryTimeIsMeasured()
4569
{
4670
// Mock a route with a query

0 commit comments

Comments
 (0)