mirror of
https://github.com/processwire/processwire.git
synced 2025-08-10 00:37:02 +02:00
Update core Debug class to use PHP 7.3 hrtime() function for timers (rather than microtime), when available. Also add support for configuring timer precision via Debug::timerSetting('precision', $n) where $n is number of decimals. Default is 4.
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
* This file is licensed under the MIT license
|
* This file is licensed under the MIT license
|
||||||
* https://processwire.com/about/license/mit/
|
* https://processwire.com/about/license/mit/
|
||||||
*
|
*
|
||||||
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer
|
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer
|
||||||
* https://processwire.com
|
* https://processwire.com
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@@ -41,6 +41,25 @@ class Debug {
|
|||||||
*/
|
*/
|
||||||
static protected $savedTimerNotes = array();
|
static protected $savedTimerNotes = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use hrtime()?
|
||||||
|
*
|
||||||
|
* @var null|bool
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static protected $useHrtime = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Timer precision (digits after decimal)
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static protected $timerSettings = array(
|
||||||
|
'precision' => 4,
|
||||||
|
'useHrtime' => null,
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Measure time between two events
|
* Measure time between two events
|
||||||
*
|
*
|
||||||
@@ -58,26 +77,60 @@ class Debug {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static public function timer($key = '', $reset = false) {
|
static public function timer($key = '', $reset = false) {
|
||||||
|
|
||||||
|
if(self::$timerSettings['useHrtime'] === null) {
|
||||||
|
self::$timerSettings['useHrtime'] = function_exists("\\hrtime");
|
||||||
|
}
|
||||||
|
|
||||||
// returns number of seconds elapsed since first call
|
// returns number of seconds elapsed since first call
|
||||||
if($reset && $key) self::removeTimer($key);
|
if($reset && $key) self::removeTimer($key);
|
||||||
|
|
||||||
if(!$key || !isset(self::$timers[$key])) {
|
if(!$key || !isset(self::$timers[$key])) {
|
||||||
// start new timer
|
// start new timer
|
||||||
$startTime = -microtime(true);
|
$startTime = self::$timerSettings['useHrtime'] ? hrtime(true) : -microtime(true);
|
||||||
if(!$key) {
|
if(!$key) {
|
||||||
$key = (string) $startTime;
|
$key = (string) $startTime;
|
||||||
while(isset(self::$timers[$key])) $key .= "0";
|
while(isset(self::$timers[$key])) $key .= "0";
|
||||||
}
|
}
|
||||||
self::$timers[(string) $key] = $startTime;
|
self::$timers[(string) $key] = $startTime;
|
||||||
$value = $key;
|
$value = $key;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// return existing timer
|
// finish a timer
|
||||||
$value = number_format(self::$timers[$key] + microtime(true), 4);
|
if(self::$timerSettings['useHrtime']) {
|
||||||
|
// return existing hrtime timer
|
||||||
|
$value = ((hrtime(true) - self::$timers[$key]) / 1e+6) / 1000;
|
||||||
|
} else {
|
||||||
|
// return existing microtime timer
|
||||||
|
$value = self::$timers[$key] + microtime(true);
|
||||||
|
}
|
||||||
|
if(self::$timerSettings['precision']) {
|
||||||
|
$value = number_format($value, self::$timerSettings['precision']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get or set timer setting
|
||||||
|
*
|
||||||
|
* ~~~~~~
|
||||||
|
* // Example of changing precision to 2
|
||||||
|
* Debug::timerSetting('precision', 2);
|
||||||
|
* ~~~~~~
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed|null $value
|
||||||
|
* @return mixed
|
||||||
|
* @since 3.0.154
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static public function timerSetting($key, $value = null) {
|
||||||
|
if($value !== null) self::$timerSettings[$key] = $value;
|
||||||
|
return self::$timerSettings[$key];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save the current time of the given timer which can be later retrieved with getSavedTimer($key)
|
* Save the current time of the given timer which can be later retrieved with getSavedTimer($key)
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user