diff --git a/src/Util/Timing.php b/src/Util/Timing.php index f5bfda611b..95f6810b3b 100644 --- a/src/Util/Timing.php +++ b/src/Util/Timing.php @@ -12,6 +12,20 @@ class Timing { + /** + * Number of milliseconds in a minute. + * + * @var int + */ + const MINUTE_IN_MS = 60000; + + /** + * Number of milliseconds in a second. + * + * @var int + */ + const SECOND_IN_MS = 1000; + /** * The start time of the run in microseconds. * @@ -67,15 +81,15 @@ public static function getDuration() public static function getHumanReadableDuration($duration) { $timeString = ''; - if ($duration > 60000) { - $mins = floor($duration / 60000); - $secs = round((fmod($duration, 60000) / 1000), 2); + if ($duration >= self::MINUTE_IN_MS) { + $mins = floor($duration / self::MINUTE_IN_MS); + $secs = round((fmod($duration, self::MINUTE_IN_MS) / self::SECOND_IN_MS), 2); $timeString = $mins.' mins'; - if ($secs !== 0) { + if ($secs >= 0.01) { $timeString .= ", $secs secs"; } - } else if ($duration > 1000) { - $timeString = round(($duration / 1000), 2).' secs'; + } else if ($duration >= self::SECOND_IN_MS) { + $timeString = round(($duration / self::SECOND_IN_MS), 2).' secs'; } else { $timeString = round($duration).'ms'; } diff --git a/tests/Core/Util/Timing/GetHumanReadableDurationTest.php b/tests/Core/Util/Timing/GetHumanReadableDurationTest.php index 113c93e5fb..93b3d05e1e 100644 --- a/tests/Core/Util/Timing/GetHumanReadableDurationTest.php +++ b/tests/Core/Util/Timing/GetHumanReadableDurationTest.php @@ -68,7 +68,7 @@ public static function dataGetHumanReadableDuration() ], 'Duration: 1 second' => [ 'duration' => 1000, - 'expected' => '1000ms', + 'expected' => '1 secs', ], 'Duration: slightly more than 1 second' => [ 'duration' => 1001.178215, @@ -80,11 +80,11 @@ public static function dataGetHumanReadableDuration() ], 'Duration: exactly 1 minute' => [ 'duration' => 60000, - 'expected' => '60 secs', + 'expected' => '1 mins', ], 'Duration: slightly more than 1 minute' => [ 'duration' => 60001.7581235, - 'expected' => '1 mins, 0 secs', + 'expected' => '1 mins', ], 'Duration: 1 minute, just under half a second' => [ 'duration' => 60499.83639, @@ -100,7 +100,7 @@ public static function dataGetHumanReadableDuration() ], 'Duration: exactly 1 hour' => [ 'duration' => 3600000, - 'expected' => '60 mins, 0 secs', + 'expected' => '60 mins', ], 'Duration: 89.4 mins' => [ 'duration' => 5364000,