activate(); $dbgExecTime->log('start'); ... $dbgExecTime->log('end'); $dbgExecTime->printDebug(); */ class DebugExecutionTime { private $_log = array(); private $_logByGroups = array(); private $_isActive = false; private $_lastTime = 0; public function __construct() { $this->_log = array(); } public function activate() { $this->_isActive = true; } /* * @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]; ?>
_log as $log) : ?>
msg time
total:
_logByGroups)) : ?> _logByGroups as $group => $execTime) : ?>
group time time left time %
%