Skip to content

Commit 1aa274a

Browse files
committed
bug #52881 [DoctrineBridge] Global query time always at 0.00 ms on profiler (Maxime THIRY)
This PR was merged into the 6.4 branch. Discussion ---------- [DoctrineBridge] Global query time always at 0.00 ms on profiler | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #52880 | License | MIT (original explanation made from 7.0 branch, now targeting 6.4 cf comments) The related issue, when i explained the possible solution : symfony/symfony#52880 On `Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector`, updated the native return type from `int` to `float` for the method `getTime`. Context : ``` public function getTime(): int { $time = 0; foreach ($this->data['queries'] as $queries) { foreach ($queries as $query) { $time += $query['executionMS']; } } return $time; } ``` Inside this method, the value of `$time` was always a float smaller than 0. The transition to native return type (which was correct regarding to old phpdoc type) made the return cast int to the floating value, hence returning always 0. I updated the return type to float and updated the test to check for the calculus on floating values. I had to switch to usleep instead of sleep, since the value might not be an int. Commits ------- b78ca959f9 [DoctrineBridge] Fix global query time calculation in profiler
2 parents 568ab41 + 44c7cb1 commit 1aa274a

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

DataCollector/DoctrineDataCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function getQueries()
146146
}
147147

148148
/**
149-
* @return int
149+
* @return float
150150
*/
151151
public function getTime()
152152
{

Tests/DataCollector/DoctrineDataCollectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private function createCollector(array $queries): DoctrineDataCollector
183183
$debugDataHolder->addQuery('default', $query);
184184

185185
if (isset($queryData['executionMS'])) {
186-
sleep($queryData['executionMS']);
186+
usleep($queryData['executionMS'] * 1000000);
187187
}
188188
$query->stop();
189189
}

Tests/DataCollector/DoctrineDataCollectorTestTrait.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,36 @@ public function testCollectTime()
7373
$this->assertEquals(3, $c->getTime());
7474
}
7575

76+
public function testCollectTimeWithFloatExecutionMS()
77+
{
78+
$queries = [
79+
['sql' => 'SELECT * FROM table1', 'params' => [], 'types' => [], 'executionMS' => 0.23],
80+
];
81+
$c = $this->createCollector($queries);
82+
$c->collect(new Request(), new Response());
83+
$c = unserialize(serialize($c));
84+
$this->assertEqualsWithDelta(0.23, $c->getTime(), .01);
85+
86+
$queries = [
87+
['sql' => 'SELECT * FROM table1', 'params' => [], 'types' => [], 'executionMS' => 1.02],
88+
['sql' => 'SELECT * FROM table2', 'params' => [], 'types' => [], 'executionMS' => 0.75],
89+
];
90+
$c = $this->createCollector($queries);
91+
$c->collect(new Request(), new Response());
92+
$c = unserialize(serialize($c));
93+
$this->assertEqualsWithDelta(1.77, $c->getTime(), .01);
94+
95+
$queries = [
96+
['sql' => 'SELECT * FROM table1', 'params' => [], 'types' => [], 'executionMS' => 0.15],
97+
['sql' => 'SELECT * FROM table2', 'params' => [], 'types' => [], 'executionMS' => 0.32],
98+
['sql' => 'SELECT * FROM table3', 'params' => [], 'types' => [], 'executionMS' => 0.07],
99+
];
100+
$c = $this->createCollector($queries);
101+
$c->collect(new Request(), new Response());
102+
$c = unserialize(serialize($c));
103+
$this->assertEqualsWithDelta(0.54, $c->getTime(), .01);
104+
}
105+
76106
public function testCollectQueryWithNoTypes()
77107
{
78108
$queries = [

0 commit comments

Comments
 (0)