Skip to content

Commit

Permalink
Fixed profiler formatting (#28)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SmetDenis authored Mar 29, 2024
1 parent afb6b31 commit eaf5e09
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
4 changes: 4 additions & 0 deletions demo/Commands/DemoOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ protected function executeAction(): int
// `cli($text)` is global alias for `$this->_();`

\usleep(25000);
// \sleep(5);
$range = \range(1, 1000000);

// Info output
// ./my-app examples:output -v
Expand All @@ -75,6 +77,8 @@ protected function executeAction(): int
cli();
}

unset($range);

// Debug output
// ./my-app examples:output -vvv
cli("Low-level message for devs #1 {$code('OutLvl::VVV')} (-vvv)", OutLvl::VVV);
Expand Down
23 changes: 12 additions & 11 deletions src/OutputMods/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function onExecAfter(int $exitCode, ?string $outputLevel = null): void
$outputLevel,
);

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

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

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

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

$memoryDiff = FS::format($profile['memory_usage_diff'], 0);
$memoryDiff = \str_pad($memoryDiff, 6, ' ', \STR_PAD_LEFT);
if ($profile['memory_usage_diff'] === 0) {
$memoryDiff = '<gray>' . \str_pad('0', 6, ' ', \STR_PAD_LEFT) . '</gray>';
if (\abs($profile['memory_usage_diff']) < $oneKb) {
$memoryDiff = '<gray>' . \str_pad($memoryDiff, 6, ' ', \STR_PAD_LEFT) . '</gray>';
} else {
$memoryDiff = $profile['memory_usage_diff'] > 0
? "<yellow>{$memoryDiff}</yellow>"
Expand All @@ -260,25 +261,25 @@ protected function printMessage(
$profilerData[] = $timeTotal;
$profilerData[] = $timeDiff;
$profilerData[] = $memoryDiff;
$profilerData[] = FS::format($profile['memory_usage'], 0);
$profilerData[] = 'P: ' . FS::format($profile['memory_peak'], 0);
$profilerData[] = \str_pad(FS::format($profile['memory_usage'], 0), 7, ' ', \STR_PAD_LEFT);
$profilerData[] = 'P:' . FS::format($profile['memory_peak'], 0);
} elseif ($this->showMessage(OutputInterface::VERBOSITY_VERY_VERBOSE)) {
$profilerData[] = $timeTotal;
$profilerData[] = $timeDiff;
$profilerData[] = $memoryDiff;
$profilerData[] = FS::format($profile['memory_usage'], 0);
$profilerData[] = \str_pad(FS::format($profile['memory_usage'], 0), 7, ' ', \STR_PAD_LEFT);
} elseif ($this->showMessage(OutputInterface::VERBOSITY_VERBOSE)) {
$profilerData[] = $timeDiff;
$profilerData[] = $memoryDiff;
$profilerData[] = FS::format($profile['memory_usage'], 0);
$profilerData[] = \str_pad(FS::format($profile['memory_usage'], 0), 7, ' ', \STR_PAD_LEFT);
} else {
$profilerData[] = $timeDiff;
$profilerData[] = $memoryDiff;
}
}

$profilePrefix .= '<green>[</green>'
. \implode(' <green>|</green> ', $profilerData)
. \implode('<green>|</green>', $profilerData)
. '<green>]</green> ';
}
$printCallback($profilePrefix);
Expand Down
3 changes: 1 addition & 2 deletions tests/CliOutputTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function testDebug(): void
isContain('; Real peak:', $cmdResult->std);
isContain('; Time:', $cmdResult->std);
isNotContain(' ms (bootstrap)', $cmdResult->std);
isContain('Info: Exit Code is "0"', $cmdResult->std);
isContain('Debug: Exit Code is "0"', $cmdResult->std);
}

public function testDebugAndProfiler(): void
Expand Down Expand Up @@ -187,7 +187,6 @@ public function testProfile(): void
isContain('Memory:', $cmdResult->std);
isContain('Real peak:', $cmdResult->std);
isContain('Time:', $cmdResult->std);
isContain('Exit Code is "0"', $cmdResult->std);
}

public function testStdoutOnly(): void
Expand Down

0 comments on commit eaf5e09

Please sign in to comment.