Skip to content

Commit b5b07c6

Browse files
committed
Util\Timing: split printRunTime() method
... into logical parts to allow the Performance report access to the total run time information (at the point of report creation) as well.
1 parent 3c80ec8 commit b5b07c6

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

src/Util/Timing.php

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,51 @@ public static function startTiming()
4040
}//end startTiming()
4141

4242

43+
/**
44+
* Get the duration of the run up to "now".
45+
*
46+
* @return int Duration in microseconds.
47+
*/
48+
public static function getDuration()
49+
{
50+
if (self::$startTime === null) {
51+
// Timing was never started.
52+
return 0;
53+
}
54+
55+
return ((microtime(true) - self::$startTime) * 1000);
56+
57+
}//end getDuration()
58+
59+
60+
/**
61+
* Convert a duration in microseconds to a human readable duration string.
62+
*
63+
* @param int $duration Duration in microseconds.
64+
*
65+
* @return string
66+
*/
67+
public static function getHumanReadableDuration($duration)
68+
{
69+
$timeString = '';
70+
if ($duration > 60000) {
71+
$mins = floor($duration / 60000);
72+
$secs = round((fmod($duration, 60000) / 1000), 2);
73+
$timeString = $mins.' mins';
74+
if ($secs !== 0) {
75+
$timeString .= ", $secs secs";
76+
}
77+
} else if ($duration > 1000) {
78+
$timeString = round(($duration / 1000), 2).' secs';
79+
} else {
80+
$timeString = round($duration).'ms';
81+
}
82+
83+
return $timeString;
84+
85+
}//end getHumanReadableDuration()
86+
87+
4388
/**
4489
* Print information about the run.
4590
*
@@ -60,23 +105,11 @@ public static function printRunTime($force=false)
60105
return;
61106
}
62107

63-
$time = ((microtime(true) - self::$startTime) * 1000);
64-
65-
if ($time > 60000) {
66-
$mins = floor($time / 60000);
67-
$secs = round((fmod($time, 60000) / 1000), 2);
68-
$time = $mins.' mins';
69-
if ($secs !== 0) {
70-
$time .= ", $secs secs";
71-
}
72-
} else if ($time > 1000) {
73-
$time = round(($time / 1000), 2).' secs';
74-
} else {
75-
$time = round($time).'ms';
76-
}
108+
$duration = self::getDuration();
109+
$duration = self::getHumanReadableDuration($duration);
77110

78111
$mem = round((memory_get_peak_usage(true) / (1024 * 1024)), 2).'MB';
79-
echo "Time: $time; Memory: $mem".PHP_EOL.PHP_EOL;
112+
echo "Time: $duration; Memory: $mem".PHP_EOL.PHP_EOL;
80113

81114
self::$printed = true;
82115

0 commit comments

Comments
 (0)