Skip to content

Util\Timing::getHumanReadableDuration: improve time display #658

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

Merged
merged 2 commits into from
Nov 2, 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
26 changes: 20 additions & 6 deletions src/Util/Timing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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';
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Core/Util/Timing/GetHumanReadableDurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down