diff --git a/src/Command/BuildCommand.php b/src/Command/BuildCommand.php index 46e3f0b..bdc7379 100644 --- a/src/Command/BuildCommand.php +++ b/src/Command/BuildCommand.php @@ -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(); @@ -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('OK (' . $seconds . ' ' . ($seconds > 1 ? 'seconds' : 'second') . ')'); + self::$io->writeln('OK (' . $this->getTimeElapsed($timeStartProcessing) . ')'); } // Render all pages. @@ -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; diff --git a/tests/Command/BuildCommandTest.php b/tests/Command/BuildCommandTest.php new file mode 100644 index 0000000..17d8de1 --- /dev/null +++ b/tests/Command/BuildCommandTest.php @@ -0,0 +1,36 @@ +assertSame($expected, $buildCommand->getTimeElapsed(microtime(true) - $duration)); + } + + /** + * @return array> + */ + public function provideTimeElapsed(): array + { + return [ + [45, '45 seconds'], + [125, '2 minutes, 5 seconds'], + [3601, '1 hour, 1 second'], + [5580, '1 hour, 33 minutes'], + ]; + } +}