| 1 |
<?php |
| 2 |
/** |
| 3 |
* Timing functions for the run. |
| 4 |
* |
| 5 |
* @author Greg Sherwood <[email protected]> |
| 6 |
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
| 7 |
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace PHP_CodeSniffer\Util; |
| 11 |
|
| 12 |
class Timing |
| 13 |
{ |
| 14 |
|
| 15 |
/** |
| 16 |
* The start time of the run. |
| 17 |
* |
| 18 |
* @var float |
| 19 |
*/ |
| 20 |
private static $startTime; |
| 21 |
|
| 22 |
/** |
| 23 |
* Used to make sure we only print the run time once per run. |
| 24 |
* |
| 25 |
* @var boolean |
| 26 |
*/ |
| 27 |
private static $printed = false; |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Start recording time for the run. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public static function startTiming() |
| 36 |
{ |
| 37 |
|
| 38 |
self::$startTime = microtime(true); |
| 39 |
|
| 40 |
}//end startTiming() |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Print information about the run. |
| 45 |
* |
| 46 |
* @param boolean $force If TRUE, prints the output even if it has |
| 47 |
* already been printed during the run. |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public static function printRunTime($force=false) |
| 52 |
{ |
| 53 |
if ($force === false && self::$printed === true) { |
| 54 |
// A double call. |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if (self::$startTime === null) { |
| 59 |
// Timing was never started. |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$time = ((microtime(true) - self::$startTime) * 1000); |
| 64 |
|
| 65 |
if ($time > 60000) { |
| 66 |
$mins = floor($time / 60000); |
| 67 |
$secs = round((($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 |
} |
| 77 |
|
| 78 |
$mem = round((memory_get_peak_usage(true) / (1024 * 1024)), 2).'MB'; |
| 79 |
echo "Time: $time; Memory: $mem".PHP_EOL.PHP_EOL; |
| 80 |
|
| 81 |
self::$printed = true; |
| 82 |
|
| 83 |
}//end printRunTime() |
| 84 |
|
| 85 |
|
| 86 |
}//end class |
| 87 |
|