Created
August 7, 2019 09:58
-
-
Save delboy1978uk/4b9bc5ff3e322b35f95fa17649133c5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Meta\Library\Development; | |
use Meta\Common\Collections\Collection; | |
/** | |
* Class Timer | |
* | |
* Inspired by javscripts console.time/console.timeEnd | |
* | |
* Timer::start('foobar') - sets a label and stashes the microtime(true) | |
* Timer::stop('foobar') - sets a stop label and returns the time in microseconds | |
* | |
* Note: successive calls to stop will overrite previous labels and return the time | |
* | |
* So you can do | |
* | |
* Timer::start('foobar') | |
* | |
* sleep(1) | |
* | |
* Time::stop('foobar') - time from start at this point | |
* | |
* sleep(1); | |
* | |
* Time::stop('foobar') - time from start at *this* point | |
* | |
* @package Meta\Library\Development | |
*/ | |
class ProfileTimer | |
{ | |
/** | |
* Timer constructor. | |
*/ | |
public function __construct() | |
{ | |
$this->startTimes = new Collection(); | |
$this->stopTimes = new Collection(); | |
} | |
/** | |
* @var ProfileTimer $instance | |
*/ | |
protected static $instance; | |
/** | |
* @var Collection of start times, $label => microtime(true) | |
*/ | |
private $startTimes; | |
/** | |
* @var Collection $stopTimes - array of $label => microtime(true) | |
*/ | |
private $stopTimes; | |
/** | |
* Set start time for label | |
* | |
* @param string $label | |
* | |
* @return ProfileTimer | |
*/ | |
public static function start($label) | |
{ | |
self::init(); | |
// Set a start time label with current microtime | |
self::$instance->startTimes->set($label, microtime(true)); | |
return self::$instance; | |
} | |
/** | |
* Set a stop time for a label | |
* | |
* @param string $label | |
* | |
* @return string|null | |
*/ | |
public static function stop($label) | |
{ | |
self::init(); | |
self::$instance->stopTimes->set($label, microtime(true)); | |
if (self::$instance->startTimes->has($label)) { | |
$startTime = self::$instance->startTimes->get($label); | |
$stopTime = self::$instance->stopTimes->get($label); | |
return ($stopTime - $startTime) * 1000 .'ms'; | |
} | |
return null; | |
} | |
/** | |
* Initialise the instance property with an instance of Timer | |
* | |
* Will only initialise once so can be safely called multiple times | |
* | |
* @return ProfileTimer | |
*/ | |
private static function init() | |
{ | |
if (!self::$instance) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
/** | |
* Return an array consisting of labels and times in milliseconds. | |
* | |
* Only complete pairs will be returned (where we have both a start/stop label) | |
* | |
* In the case where multiple stops where used it will return the difference of the *last* stop | |
* | |
* @return array | |
*/ | |
public static function allMetrics() | |
{ | |
self::init(); | |
$results = []; | |
foreach (self::$instance->startTimes as $key => $value) { | |
$startTime = self::$instance->startTimes->get($key, false); | |
$stopTime = self::$instance->stopTimes->get($key, false); | |
if ($startTime !== false && $stopTime !== false) { | |
$startTime = self::$instance->startTimes->get($key); | |
$stopTime = self::$instance->stopTimes->get($key); | |
$results[$key] = ($stopTime - $startTime) * 1000; | |
} | |
} | |
return $results; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment