/** * Measures operations duration, memory usage, etc. (and some other KPIs) * * @copyright Copyright (C) 2010-2012 Combodo SARL * @license http://opensource.org/licenses/AGPL-3.0 */ class ExecutionKPI { static protected $m_bEnabled_Duration = false; static protected $m_bEnabled_Memory = false; static protected $m_sAllowedUser = '*'; static protected $m_aStats = array(); // Recurrent operations static protected $m_aExecData = array(); // One shot operations protected $m_fStarted = null; protected $m_iInitialMemory = null; static public function EnableDuration($iLevel) { if ($iLevel > 0) { self::$m_bEnabled_Duration = true; } } static public function EnableMemory($iLevel) { if ($iLevel > 0) { self::$m_bEnabled_Memory = true; } } /** * @param string sUser A user login or * for all users */ static public function SetAllowedUser($sUser) { self::$m_sAllowedUser = $sUser; } static public function ReportStats() { if (self::$m_sAllowedUser != '*') { if (UserRights::GetUser() != self::$m_sAllowedUser) { return; } } global $fItopStarted; $sExecId = microtime(); // id to differentiate the hrefs! $aBeginTimes = array(); foreach (self::$m_aExecData as $aOpStats) { $aBeginTimes[] = $aOpStats['time_begin']; } array_multisort($aBeginTimes, self::$m_aExecData); $sTableStyle = 'background-color: #ccc; margin: 10px;'; self::Report("
".date('Y-m-d H:i:s', $fItopStarted)."
"); self::Report("log_kpi_user_id: ".MetaModel::GetConfig()->Get('log_kpi_user_id')."
"); self::Report("Operation | Begin | End | Duration | Memory start | Memory end | Memory peak | "); self::Report(""); foreach (self::$m_aExecData as $aOpStats) { $sOperation = $aOpStats['op']; $sBegin = $sEnd = $sDuration = $sMemBegin = $sMemEnd = $sMemPeak = '?'; $sBegin = round($aOpStats['time_begin'], 3); $sEnd = round($aOpStats['time_end'], 3); $fDuration = $aOpStats['time_end'] - $aOpStats['time_begin']; $sDuration = round($fDuration, 3); if (isset($aOpStats['mem_begin'])) { $sMemBegin = self::MemStr($aOpStats['mem_begin']); $sMemEnd = self::MemStr($aOpStats['mem_end']); if (isset($aOpStats['mem_peak'])) { $sMemPeak = self::MemStr($aOpStats['mem_peak']); } } self::Report("
---|---|---|---|---|---|---|
$sOperation | $sBegin | $sEnd | $sDuration | $sMemBegin | $sMemEnd | $sMemPeak | "); self::Report("
Operation | Count | Duration | Min | Max | Avg | "); self::Report(""); foreach ($aConsolidatedStats as $sOperation => $aOpStats) { $sOperation = ''.$sOperation.''; $sCount = $aOpStats['count']; $sDuration = round($aOpStats['duration'], 3); $sMin = round($aOpStats['min'], 3); $sMax = ''.round($aOpStats['max'], 3).''; $sAvg = round($aOpStats['avg'], 3); self::Report("
---|---|---|---|---|---|
$sOperation | $sCount | $sDuration | $sMin | $sMax | $sAvg | "); self::Report("
Operation details | Count | Duration | Min | Max | "); self::Report(""); foreach ($aOpStats as $sArguments => $aEvents) { $sHtmlArguments = ''.$sArguments.''; if ($aConsolidatedStats[$sOperation]['max_args'] == $sArguments) { $sHtmlArguments = ''.$sHtmlArguments.''; } $fTotalInter = 0; $fMinInter = null; $fMaxInter = 0; foreach ($aEvents as $fDuration) { $fTotalInter += $fDuration; $fMinInter = is_null($fMinInter) ? $fDuration : min($fMinInter, $fDuration); $fMaxInter = max($fMaxInter, $fDuration); } $iCountInter = count($aEvents); $sTotalInter = round($fTotalInter, 3); $sMinInter = round($fMinInter, 3); $sMaxInter = round($fMaxInter, 3); self::Report("
---|---|---|---|---|
$sHtmlArguments | $iCountInter | $sTotalInter | $sMinInter | $sMaxInter | "); self::Report("