| 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 |
* The start time of the run. |
| 16 |
* |
| 17 |
* @var float |
| 18 |
*/ |
| 19 |
private static $startTime; |
| 20 |
|
| 21 |
/** |
| 22 |
* Used to make sure we only print the run time once per run. |
| 23 |
* |
| 24 |
* @var boolean |
| 25 |
*/ |
| 26 |
private static $printed = false; |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Start recording time for the run. |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public static function startTiming() { |
| 35 |
|
| 36 |
self::$startTime = microtime( true ); |
| 37 |
|
| 38 |
}//end startTiming() |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Print information about the run. |
| 43 |
* |
| 44 |
* @param boolean $force If TRUE, prints the output even if it has |
| 45 |
* already been printed during the run. |
| 46 |
* |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public static function printRunTime( $force = false ) { |
| 50 |
if ( $force === false && self::$printed === true ) { |
| 51 |
// A double call. |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if ( self::$startTime === null ) { |
| 56 |
// Timing was never started. |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$time = ( ( microtime( true ) - self::$startTime ) * 1000 ); |
| 61 |
|
| 62 |
if ( $time > 60000 ) { |
| 63 |
$mins = floor( $time / 60000 ); |
| 64 |
$secs = round( ( ( $time % 60000 ) / 1000 ), 2 ); |
| 65 |
$time = $mins . ' mins'; |
| 66 |
if ( $secs !== 0 ) { |
| 67 |
$time .= ", $secs secs"; |
| 68 |
} |
| 69 |
} else if ( $time > 1000 ) { |
| 70 |
$time = round( ( $time / 1000 ), 2 ) . ' secs'; |
| 71 |
} else { |
| 72 |
$time = round( $time ) . 'ms'; |
| 73 |
} |
| 74 |
|
| 75 |
$mem = round( ( memory_get_peak_usage( true ) / ( 1024 * 1024 ) ), 2 ) . 'MB'; |
| 76 |
echo "Time: $time; Memory: $mem" . PHP_EOL . PHP_EOL; |
| 77 |
|
| 78 |
self::$printed = true; |
| 79 |
|
| 80 |
}//end printRunTime() |
| 81 |
|
| 82 |
|
| 83 |
}//end class |
| 84 |
|