Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better readability of elapsed build time #89

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/Command/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@

final class BuildCommand extends CommandBase
{
/**
* Get a human-readable representation of the time difference between the
* start time and now.
*
* @param float $startTime The start time (acquired from e.g. `microtime(true)`).
*/
public function getTimeElapsed(float $startTime): string
{
$total = round(microtime(true) - $startTime);
$parts = [
'hour' => 60 * 60,
'minute' => 60,
'second' => 1,
];
$amounts = [];
foreach ($parts as $partName => $partSize) {
$amount = ($total - ( $total % $partSize ) ) / $partSize;
$total = $total - ( $amount * $partSize);
if ($amount) {
$amounts[] = $amount . ' ' . $partName . ($amount > 1 ? 's' : '');
}
}
if (!$amounts) {
return 'no time';
} elseif (count($amounts) === 1) {
return $amounts[0];
} else {
return join(', ', $amounts);
}
}

protected function configure(): void
{
parent::configure();
Expand Down Expand Up @@ -68,8 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
self::$io->write('Processing site . . . ');
$timeStartProcessing = microtime(true);
$db->processSite($site);
$seconds = max(1, round(microtime(true) - $timeStartProcessing, 0));
self::$io->writeln('<info>OK</info> (' . $seconds . ' ' . ($seconds > 1 ? 'seconds' : 'second') . ')');
self::$io->writeln('<info>OK</info> (' . $this->getTimeElapsed($timeStartProcessing) . ')');
}

// Render all pages.
Expand Down Expand Up @@ -147,7 +177,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
self::$io->success([
'Site output to ' . $outDir,
'Memory usage: ' . (memory_get_peak_usage(true) / 1024 / 1024) . ' MiB',
'Total time: ' . round(microtime(true) - $timeStart, 1) . ' seconds',
'Total time: ' . $this->getTimeElapsed($timeStart),
'Output size: ' . substr($outputSize, 0, strpos($outputSize, "\t")),
]);
return 0;
Expand Down
36 changes: 36 additions & 0 deletions tests/Command/BuildCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Test\Command;

use App\Command\BuildCommand;
use PHPUnit\Framework\TestCase;

/**
* @covers BuildCommand
*/
final class BuildCommandTest extends TestCase
{
/**
* @dataProvider provideTimeElapsed()
*/
public function testTimeElapsed(int $duration, string $expected): void
{
$buildCommand = new BuildCommand();
$this->assertSame($expected, $buildCommand->getTimeElapsed(microtime(true) - $duration));
}

/**
* @return array<array<int,string>>
*/
public function provideTimeElapsed(): array
{
return [
[45, '45 seconds'],
[125, '2 minutes, 5 seconds'],
[3601, '1 hour, 1 second'],
[5580, '1 hour, 33 minutes'],
];
}
}
Loading