Skip to content

Commit eaf5e09

Browse files
authored
Fixed profiler formatting (#28)
Some minor adjustments were made to improve readability and accuracy of output. Padding was added to further standardize the spacing of output values, some conditionals were adjusted, and 'Exit Code' output was changed from 'Info' to 'Debug' level. Extraneous test assertions were removed accordingly.
1 parent afb6b31 commit eaf5e09

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

demo/Commands/DemoOutput.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ protected function executeAction(): int
5858
// `cli($text)` is global alias for `$this->_();`
5959

6060
\usleep(25000);
61+
// \sleep(5);
62+
$range = \range(1, 1000000);
6163

6264
// Info output
6365
// ./my-app examples:output -v
@@ -75,6 +77,8 @@ protected function executeAction(): int
7577
cli();
7678
}
7779

80+
unset($range);
81+
7882
// Debug output
7983
// ./my-app examples:output -vvv
8084
cli("Low-level message for devs #1 {$code('OutLvl::VVV')} (-vvv)", OutLvl::VVV);

src/OutputMods/Text.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function onExecAfter(int $exitCode, ?string $outputLevel = null): void
7373
$outputLevel,
7474
);
7575

76-
$this->_("Exit Code is \"{$exitCode}\"", $outputLevel);
76+
$this->_("Exit Code is \"{$exitCode}\"", OutLvl::DEBUG);
7777
}
7878

7979
public function onExecException(\Exception $exception): void
@@ -235,16 +235,17 @@ protected function printMessage(
235235
if ($executePrint && $printCallback !== null) {
236236
if ($this->isDisplayProfiling()) {
237237
$profile = $this->getProfileInfo();
238+
$oneKb = 1024;
238239

239-
$timeTotal = \number_format($profile['time_total_ms'] / 1000, 2);
240+
$timeTotal = \str_pad(\number_format($profile['time_total_ms'] / 1000, 2), 5, ' ', \STR_PAD_LEFT);
240241

241-
$timeDiff = \number_format($profile['time_diff_ms'] / 1000, 2);
242-
$timeDiff = $timeDiff === '0.00' ? '<gray> -</gray>' : "{$timeDiff}s";
242+
$timeDiff = \str_pad(\number_format($profile['time_diff_ms'] / 1000, 2), 5, ' ', \STR_PAD_LEFT);
243+
$timeDiff = $timeDiff === ' 0.00' ? '<gray> 0</gray>' : $timeDiff;
243244

244245
$memoryDiff = FS::format($profile['memory_usage_diff'], 0);
245246
$memoryDiff = \str_pad($memoryDiff, 6, ' ', \STR_PAD_LEFT);
246-
if ($profile['memory_usage_diff'] === 0) {
247-
$memoryDiff = '<gray>' . \str_pad('0', 6, ' ', \STR_PAD_LEFT) . '</gray>';
247+
if (\abs($profile['memory_usage_diff']) < $oneKb) {
248+
$memoryDiff = '<gray>' . \str_pad($memoryDiff, 6, ' ', \STR_PAD_LEFT) . '</gray>';
248249
} else {
249250
$memoryDiff = $profile['memory_usage_diff'] > 0
250251
? "<yellow>{$memoryDiff}</yellow>"
@@ -260,25 +261,25 @@ protected function printMessage(
260261
$profilerData[] = $timeTotal;
261262
$profilerData[] = $timeDiff;
262263
$profilerData[] = $memoryDiff;
263-
$profilerData[] = FS::format($profile['memory_usage'], 0);
264-
$profilerData[] = 'P: ' . FS::format($profile['memory_peak'], 0);
264+
$profilerData[] = \str_pad(FS::format($profile['memory_usage'], 0), 7, ' ', \STR_PAD_LEFT);
265+
$profilerData[] = 'P:' . FS::format($profile['memory_peak'], 0);
265266
} elseif ($this->showMessage(OutputInterface::VERBOSITY_VERY_VERBOSE)) {
266267
$profilerData[] = $timeTotal;
267268
$profilerData[] = $timeDiff;
268269
$profilerData[] = $memoryDiff;
269-
$profilerData[] = FS::format($profile['memory_usage'], 0);
270+
$profilerData[] = \str_pad(FS::format($profile['memory_usage'], 0), 7, ' ', \STR_PAD_LEFT);
270271
} elseif ($this->showMessage(OutputInterface::VERBOSITY_VERBOSE)) {
271272
$profilerData[] = $timeDiff;
272273
$profilerData[] = $memoryDiff;
273-
$profilerData[] = FS::format($profile['memory_usage'], 0);
274+
$profilerData[] = \str_pad(FS::format($profile['memory_usage'], 0), 7, ' ', \STR_PAD_LEFT);
274275
} else {
275276
$profilerData[] = $timeDiff;
276277
$profilerData[] = $memoryDiff;
277278
}
278279
}
279280

280281
$profilePrefix .= '<green>[</green>'
281-
. \implode(' <green>|</green> ', $profilerData)
282+
. \implode('<green>|</green>', $profilerData)
282283
. '<green>]</green> ';
283284
}
284285
$printCallback($profilePrefix);

tests/CliOutputTextTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function testDebug(): void
157157
isContain('; Real peak:', $cmdResult->std);
158158
isContain('; Time:', $cmdResult->std);
159159
isNotContain(' ms (bootstrap)', $cmdResult->std);
160-
isContain('Info: Exit Code is "0"', $cmdResult->std);
160+
isContain('Debug: Exit Code is "0"', $cmdResult->std);
161161
}
162162

163163
public function testDebugAndProfiler(): void
@@ -187,7 +187,6 @@ public function testProfile(): void
187187
isContain('Memory:', $cmdResult->std);
188188
isContain('Real peak:', $cmdResult->std);
189189
isContain('Time:', $cmdResult->std);
190-
isContain('Exit Code is "0"', $cmdResult->std);
191190
}
192191

193192
public function testStdoutOnly(): void

0 commit comments

Comments
 (0)