activate();
$dbgExecTime->log('start');
...
$dbgExecTime->log('end');
$dbgExecTime->printDebug();
*/
/* usage 2 - log to file:
Lib::loadClass('DebugExecutionTime');
$dbgExecTime = new DebugExecutionTime();
$dbgExecTime->setLogFile('UNIQ_FILE_NAME', $format = 'csv'); // $format: 'csv', 'json'
$dbgExecTime->logToFile('start');
...
$dbgExecTime->logToFile('end');
*/
class DebugExecutionTime {
private $_log = array();
private $_logByGroups = array();
private $_isActive = false;
private $_lastTime = 0;
private $_logFile = null;
private $_logFormat = null;
public function __construct() {
$this->_log = array();
}
public function activate() {
$this->_isActive = true;
}
public function setLogFile($fileName, $format = 'csv') { // @param $format = 'csv', 'json'
if (!file_exists($fileName)) @touch($fileName);
if (!file_exists($fileName)) return; // log error?
$this->_logFile = $fileName;
switch ($format) {
case 'json'; $this->_logFormat = 'json'; break;
default: $this->_logFormat = 'csv';
}
}
public function logToFile($msg) {
if (!$this->_logFile) return;
static $_idRequest = null;
static $_lastMicroTime = null;
// $timeDiff = (!$lastTime)
// ? ''
// : V::milisecondsStringDiff($dbg['date'], $lastTime); // TODO: $dbg['date'] - $lastTime;
$microTime = date("Y-m-d H:i:s") . substr((string)microtime(), 1, 6);
$info = [
'date' => $microTime,
'diff' => ($_lastMicroTime) ? V::milisecondsStringDiff($microTime, $_lastMicroTime) : '',
'req' => $_idRequest,
'mem' => memory_get_usage(true),
'msg' => $msg,
];
$_lastMicroTime = $microTime;
if (null === $_idRequest && 'csv' === $this->_logFormat) {
error_log(
implode("\t", array_keys($info)) . "\n"
, 3
, $this->_logFile
);
}
if (null === $_idRequest) {
$_idRequest = substr(md5(date("Y-m-d") . Request::getUserIp() . V::get('REQUEST_TIME', '', $_SERVER)), 0, 6);
$info['req'] = $_idRequest;
}
switch ($this->_logFormat) {
case 'csv': {
error_log(
implode("\t", array_values($info)) . "\n"
, 3
, $this->_logFile
);
} break;
case 'json': {
error_log(
json_encode($info) . "\n"
, 3
, $this->_logFile
);
} break;
}
}
/*
* @param $msg string or NULL (NULL to log only group stats)
*/
public function log($msg, $groups = array()) {
if (!$this->_isActive) return;
$curTime = microtime(true);
$execTime = (!$this->_lastTime)? 0 : $curTime - $this->_lastTime;
$this->_lastTime = $curTime;
$this->_log[] = array($msg, $execTime);
foreach ($groups as $group) {
if (!array_key_exists($group, $this->_logByGroups)) $this->_logByGroups[$group] = 0;
$this->_logByGroups[$group] += $execTime;
}
}
public function getLastExecTime() {
if (!$this->_isActive) return null;
$logTotal = count($this->_log);
if ($logTotal < 2) return null;
return $this->_log[$logTotal - 1][1];
}
public function getTotalExecTime() {
if (!$this->_isActive) return null;
$logTotal = count($this->_log);
if ($logTotal < 2) return null;
$sumExecTime = 0;
foreach ($this->_log as $log) $sumExecTime += $log[1];
return $sumExecTime;
}
public function printDebug() {
if (!$this->_isActive) return;
$sumExecTime = 0;
foreach ($this->_log as $log) $sumExecTime += $log[1];
?>
| msg |
time |
total: |
|
_log as $log) : ?>
|
|
|
_logByGroups)) : ?>
| group |
time |
time left |
time % |
_logByGroups as $group => $execTime) : ?>
|
|
|
% |
|