Skip to content

Commit

Permalink
Better readability of elapsed build time
Browse files Browse the repository at this point in the history
Fixes #84
  • Loading branch information
samwilson authored Sep 6, 2024
1 parent 18c7888 commit d3ffe2e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
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'],
];
}
}

0 comments on commit d3ffe2e

Please sign in to comment.