Skip to content

Commit 70ff7dc

Browse files
committed
Improve return type of Sql::getDetailedProfilingStats()
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent b38b011 commit 70ff7dc

2 files changed

Lines changed: 82 additions & 64 deletions

File tree

libraries/classes/Sql.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
use PhpMyAdmin\Utils\ForeignKey;
2525

2626
use function __;
27+
use function array_column;
2728
use function array_keys;
29+
use function array_sum;
2830
use function bin2hex;
2931
use function ceil;
3032
use function count;
@@ -247,36 +249,41 @@ public function getHtmlForRelationalColumnDropdown(
247249
* @psalm-param non-empty-list<array{Status: non-empty-string, Duration: numeric-string}> $profilingResults
248250
*
249251
* @psalm-return array{
250-
* total_time: int|float,
251-
* states: array<string, array{total_time: int|float|numeric-string, calls: int<1, max>}>,
252-
* chart: array<string, int|float|numeric-string>,
252+
* total_time: float,
253+
* states: array<string, array{total_time: float, calls: int<1, max>}>,
254+
* chart: array<string, float>,
253255
* profile: list<array{status: string, duration: string, duration_raw: numeric-string}>
254-
* }
256+
* }|array{}
255257
*/
256258
private function getDetailedProfilingStats(array $profilingResults): array
257259
{
258-
$profiling = ['total_time' => 0, 'states' => [], 'chart' => [], 'profile' => []];
260+
$totalTime = (float) array_sum(array_column($profilingResults, 'Duration'));
261+
if ($totalTime === 0.0) {
262+
return [];
263+
}
259264

260-
foreach ($profilingResults as $oneResult) {
261-
$status = ucwords($oneResult['Status']);
262-
$profiling['total_time'] += $oneResult['Duration'];
263-
$profiling['profile'][] = [
265+
$states = [];
266+
$chart = [];
267+
$profile = [];
268+
foreach ($profilingResults as $result) {
269+
$status = ucwords($result['Status']);
270+
$profile[] = [
264271
'status' => $status,
265-
'duration' => Util::formatNumber($oneResult['Duration'], 3, 1),
266-
'duration_raw' => $oneResult['Duration'],
272+
'duration' => Util::formatNumber($result['Duration'], 3, 1),
273+
'duration_raw' => $result['Duration'],
267274
];
268275

269-
if (! isset($profiling['states'][$status])) {
270-
$profiling['states'][$status] = ['total_time' => $oneResult['Duration'], 'calls' => 1];
271-
$profiling['chart'][$status] = $oneResult['Duration'];
276+
if (! isset($states[$status])) {
277+
$states[$status] = ['total_time' => (float) $result['Duration'], 'calls' => 1];
278+
$chart[$status] = (float) $result['Duration'];
272279
} else {
273-
$profiling['states'][$status]['calls']++;
274-
$profiling['states'][$status]['total_time'] += $oneResult['Duration'];
275-
$profiling['chart'][$status] += $oneResult['Duration'];
280+
$states[$status]['calls']++;
281+
$states[$status]['total_time'] += $result['Duration'];
282+
$chart[$status] += $result['Duration'];
276283
}
277284
}
278285

279-
return $profiling;
286+
return ['total_time' => $totalTime, 'states' => $states, 'chart' => $chart, 'profile' => $profile];
280287
}
281288

282289
/**
@@ -1021,7 +1028,9 @@ private function getQueryResponseForNoResultsReturned(
10211028
$scripts->addFile('sql.js');
10221029

10231030
$profiling = $this->getDetailedProfilingStats($profilingResults);
1024-
$profilingChart = $this->template->render('sql/profiling_chart', ['profiling' => $profiling]);
1031+
if ($profiling !== []) {
1032+
$profilingChart = $this->template->render('sql/profiling_chart', ['profiling' => $profiling]);
1033+
}
10251034
}
10261035

10271036
$bookmark = '';
@@ -1441,7 +1450,9 @@ private function getQueryResponseForResultsReturned(
14411450
$profilingChartHtml = '';
14421451
if ($profilingResults !== []) {
14431452
$profiling = $this->getDetailedProfilingStats($profilingResults);
1444-
$profilingChartHtml = $this->template->render('sql/profiling_chart', ['profiling' => $profiling]);
1453+
if ($profiling !== []) {
1454+
$profilingChartHtml = $this->template->render('sql/profiling_chart', ['profiling' => $profiling]);
1455+
}
14451456
}
14461457

14471458
$missingUniqueColumnMessage = $this->getMessageIfMissingColumnIndex($table, $db, $editable, $hasUnique);

test/classes/SqlTest.php

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,17 @@ public function testExecuteQueryAndSendQueryResponse(): void
617617
public function testGetDetailedProfilingStatsWithoutData(): void
618618
{
619619
$method = new ReflectionMethod($this->sql, 'getDetailedProfilingStats');
620-
$this->assertSame(
621-
['total_time' => 0, 'states' => [], 'chart' => [], 'profile' => []],
622-
$method->invoke($this->sql, []),
623-
);
620+
$this->assertSame([], $method->invoke($this->sql, []));
621+
}
622+
623+
public function testGetDetailedProfilingStatsWithZeroTotalTime(): void
624+
{
625+
$method = new ReflectionMethod($this->sql, 'getDetailedProfilingStats');
626+
$profiling = [
627+
['Status' => 'Starting', 'Duration' => '0'],
628+
['Status' => 'checking permissions', 'Duration' => '0'],
629+
];
630+
$this->assertSame([], $method->invoke($this->sql, $profiling));
624631
}
625632

626633
public function testGetDetailedProfilingStatsWithData(): void
@@ -659,50 +666,50 @@ public function testGetDetailedProfilingStatsWithData(): void
659666
$expected = [
660667
'total_time' => 0.000299,
661668
'states' => [
662-
'Starting' => ['total_time' => '0.000017', 'calls' => 1],
663-
'Checking Permissions' => ['total_time' => '0.000003', 'calls' => 1],
669+
'Starting' => ['total_time' => 0.000017, 'calls' => 1],
670+
'Checking Permissions' => ['total_time' => 0.000003, 'calls' => 1],
664671
'Opening Tables' => ['total_time' => 0.00016, 'calls' => 2],
665-
'After Opening Tables' => ['total_time' => 6.0E-6, 'calls' => 2],
666-
'System Lock' => ['total_time' => 4.0E-6, 'calls' => 2],
667-
'Table Lock' => ['total_time' => 1.5E-5, 'calls' => 2],
668-
'Unlocking Tables' => ['total_time' => 4.0E-6, 'calls' => 2],
669-
'Closing Tables' => ['total_time' => 9.0E-6, 'calls' => 3],
670-
'Init' => ['total_time' => '0.000007', 'calls' => 1],
671-
'Optimizing' => ['total_time' => '0.000004', 'calls' => 1],
672-
'Statistics' => ['total_time' => '0.000006', 'calls' => 1],
673-
'Preparing' => ['total_time' => '0.000006', 'calls' => 1],
674-
'Executing' => ['total_time' => '0.000002', 'calls' => 1],
675-
'Sending Data' => ['total_time' => '0.000029', 'calls' => 1],
676-
'End Of Update Loop' => ['total_time' => '0.000003', 'calls' => 1],
677-
'Query End' => ['total_time' => '0.000002', 'calls' => 1],
678-
'Commit' => ['total_time' => '0.000002', 'calls' => 1],
679-
'Starting Cleanup' => ['total_time' => '0.000002', 'calls' => 1],
680-
'Freeing Items' => ['total_time' => '0.000002', 'calls' => 1],
681-
'Updating Status' => ['total_time' => '0.000007', 'calls' => 1],
682-
'Reset For Next Command' => ['total_time' => '0.000009', 'calls' => 1],
672+
'After Opening Tables' => ['total_time' => 0.000006, 'calls' => 2],
673+
'System Lock' => ['total_time' => 0.000004, 'calls' => 2],
674+
'Table Lock' => ['total_time' => 0.000015, 'calls' => 2],
675+
'Unlocking Tables' => ['total_time' => 0.000004, 'calls' => 2],
676+
'Closing Tables' => ['total_time' => 0.000009, 'calls' => 3],
677+
'Init' => ['total_time' => 0.000007, 'calls' => 1],
678+
'Optimizing' => ['total_time' => 0.000004, 'calls' => 1],
679+
'Statistics' => ['total_time' => 0.000006, 'calls' => 1],
680+
'Preparing' => ['total_time' => 0.000006, 'calls' => 1],
681+
'Executing' => ['total_time' => 0.000002, 'calls' => 1],
682+
'Sending Data' => ['total_time' => 0.000029, 'calls' => 1],
683+
'End Of Update Loop' => ['total_time' => 0.000003, 'calls' => 1],
684+
'Query End' => ['total_time' => 0.000002, 'calls' => 1],
685+
'Commit' => ['total_time' => 0.000002, 'calls' => 1],
686+
'Starting Cleanup' => ['total_time' => 0.000002, 'calls' => 1],
687+
'Freeing Items' => ['total_time' => 0.000002, 'calls' => 1],
688+
'Updating Status' => ['total_time' => 0.000007, 'calls' => 1],
689+
'Reset For Next Command' => ['total_time' => 0.000009, 'calls' => 1],
683690
],
684691
'chart' => [
685-
'Starting' => '0.000017',
686-
'Checking Permissions' => '0.000003',
692+
'Starting' => 0.000017,
693+
'Checking Permissions' => 0.000003,
687694
'Opening Tables' => 0.00016,
688-
'After Opening Tables' => 6.0E-6,
689-
'System Lock' => 4.0E-6,
690-
'Table Lock' => 1.5E-5,
691-
'Unlocking Tables' => 4.0E-6,
692-
'Closing Tables' => 9.0E-6,
693-
'Init' => '0.000007',
694-
'Optimizing' => '0.000004',
695-
'Statistics' => '0.000006',
696-
'Preparing' => '0.000006',
697-
'Executing' => '0.000002',
698-
'Sending Data' => '0.000029',
699-
'End Of Update Loop' => '0.000003',
700-
'Query End' => '0.000002',
701-
'Commit' => '0.000002',
702-
'Starting Cleanup' => '0.000002',
703-
'Freeing Items' => '0.000002',
704-
'Updating Status' => '0.000007',
705-
'Reset For Next Command' => '0.000009',
695+
'After Opening Tables' => 0.000006,
696+
'System Lock' => 0.000004,
697+
'Table Lock' => 0.000015,
698+
'Unlocking Tables' => 0.000004,
699+
'Closing Tables' => 0.000009,
700+
'Init' => 0.000007,
701+
'Optimizing' => 0.000004,
702+
'Statistics' => 0.000006,
703+
'Preparing' => 0.000006,
704+
'Executing' => 0.000002,
705+
'Sending Data' => 0.000029,
706+
'End Of Update Loop' => 0.000003,
707+
'Query End' => 0.000002,
708+
'Commit' => 0.000002,
709+
'Starting Cleanup' => 0.000002,
710+
'Freeing Items' => 0.000002,
711+
'Updating Status' => 0.000007,
712+
'Reset For Next Command' => 0.000009,
706713
],
707714
'profile' => [
708715
['status' => 'Starting', 'duration' => '17 µ', 'duration_raw' => '0.000017'],

0 commit comments

Comments
 (0)