3 * This file is part of the PHP_Timer package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Utility class for timing.
19 private static $times = array(
28 private static $startTimes = array();
33 public static $requestTime;
38 public static function start()
40 array_push(self::$startTimes, microtime(true));
44 * Stops the timer and returns the elapsed time.
48 public static function stop()
50 return microtime(true) - array_pop(self::$startTimes);
54 * Formats the elapsed time as a string.
59 public static function secondsToTimeString($time)
61 $ms = round($time * 1000);
63 foreach (self::$times as $unit => $value) {
65 $time = floor($ms / $value * 100.0) / 100.0;
67 return $time . ' ' . ($time == 1 ? $unit : $unit . 's');
75 * Formats the elapsed time since the start of the request as a string.
79 public static function timeSinceStartOfRequest()
81 return self::secondsToTimeString(microtime(true) - self::$requestTime);
85 * Returns the resources (time, memory) of the request as a string.
89 public static function resourceUsage()
92 'Time: %s, Memory: %4.2fMB',
93 self::timeSinceStartOfRequest(),
94 memory_get_peak_usage(true) / 1048576
99 if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
100 PHP_Timer::$requestTime = $_SERVER['REQUEST_TIME_FLOAT'];
101 } elseif (isset($_SERVER['REQUEST_TIME'])) {
102 PHP_Timer::$requestTime = $_SERVER['REQUEST_TIME'];
104 PHP_Timer::$requestTime = microtime(true);