Improved the internal KPI reporting tools

SVN:trunk[722]
This commit is contained in:
Romain Quetiez
2010-08-30 22:14:04 +00:00
parent 8a788acb9a
commit b7131ba4db
13 changed files with 213 additions and 110 deletions

View File

@@ -452,7 +452,7 @@ class UserRightsProfile extends UserRightsAddOnAPI
{
if (!is_null($this->m_aClassActionGrants)) return;
$oDuration = new Duration();
$oKPI = new ExecutionKPI();
$oFilter = DBObjectSearch::FromOQL_AllData("SELECT URP_ActionGrant AS p WHERE p.permission = 'yes'");
$aGrants = $oFilter->ToDataArray();
@@ -461,7 +461,7 @@ class UserRightsProfile extends UserRightsAddOnAPI
$this->m_aClassActionGrants[$aGrant['profileid']][$aGrant['class']][strtolower($aGrant['action'])] = $aGrant['id'];
}
$oDuration->Scratch('Load of action grants');
$oKPI->ComputeAndReport('Load of action grants');
}
public function LoadCache()
@@ -469,7 +469,7 @@ class UserRightsProfile extends UserRightsAddOnAPI
if (!is_null($this->m_aProfiles)) return;
// Could be loaded in a shared memory (?)
$oDuration = new Duration();
$oKPI = new ExecutionKPI();
$oProfileSet = new DBObjectSet(DBObjectSearch::FromOQL_AllData("SELECT URP_Profiles"));
$this->m_aProfiles = array();
@@ -505,7 +505,7 @@ class UserRightsProfile extends UserRightsAddOnAPI
$this->m_aClassStimulusGrants[$oStimGrant->Get('profileid')][$oStimGrant->Get('class')][$oStimGrant->Get('stimulus')] = $oStimGrant;
}
$oDuration->Scratch('Load of user management cache (excepted Action Grants)');
$oKPI->ComputeAndReport('Load of user management cache (excepted Action Grants)');
/*
echo "<pre>\n";

View File

@@ -35,7 +35,7 @@ require_once('coreexception.class.inc.php');
require_once('config.class.inc.php');
require_once('log.class.inc.php');
require_once('duration.class.inc.php');
require_once('kpi.class.inc.php');
require_once('dict.class.inc.php');

View File

@@ -195,14 +195,13 @@ class CMDBSource
//$aTraceInf['file'] = __FILE__;
// $sSQLQuery .= MyHelpers::MakeSQLComment($aTraceInf);
$mu_t1 = MyHelpers::getmicrotime();
$oKPI = new ExecutionKPI();
$result = mysql_query($sSQLQuery, self::$m_resDBLink);
if (!$result)
{
throw new MySQLException('Failed to issue SQL query', array('query' => $sSQLQuery));
}
$mu_t2 = MyHelpers::getmicrotime();
// #@# todo - query_trace($sSQLQuery, $mu_t2 - $mu_t1);
$oKPI->ComputeStats('Query exec (mySQL)', $sSQLQuery);
return $result;
}

View File

@@ -37,7 +37,8 @@ define ('DEFAULT_LOG_GLOBAL', true);
define ('DEFAULT_LOG_NOTIFICATION', true);
define ('DEFAULT_LOG_ISSUE', true);
define ('DEFAULT_LOG_WEB_SERVICE', true);
define ('DEFAULT_LOG_DURATION', false);
define ('DEFAULT_LOG_KPI_DURATION', false);
define ('DEFAULT_LOG_KPI_MEMORY', false);
define ('DEFAULT_MIN_DISPLAY_LIMIT', 10);
define ('DEFAULT_MAX_DISPLAY_LIMIT', 15);
@@ -80,7 +81,8 @@ class Config
protected $m_bLogNotification;
protected $m_bLogIssue;
protected $m_bLogWebService;
protected $m_bLogDuration; // private setting
protected $m_bLogKpiDuration; // private setting
protected $m_bLogKpiMemory; // private setting
/**
* @var integer Number of elements to be displayed when there are more than m_iMaxDisplayLimit elements
@@ -178,7 +180,8 @@ class Config
$this->m_bLogNotification = DEFAULT_LOG_NOTIFICATION;
$this->m_bLogIssue = DEFAULT_LOG_ISSUE;
$this->m_bLogWebService = DEFAULT_LOG_WEB_SERVICE;
$this->m_bLogDuration = DEFAULT_LOG_DURATION;
$this->m_bLogKPIDuration = DEFAULT_LOG_KPI_DURATION;
$this->m_bLogKPIDuration = DEFAULT_LOG_KPI_DURATION;
$this->m_iMinDisplayLimit = DEFAULT_MIN_DISPLAY_LIMIT;
$this->m_iMaxDisplayLimit = DEFAULT_MAX_DISPLAY_LIMIT;
$this->m_iStandardReloadInterval = DEFAULT_STANDARD_RELOAD_INTERVAL;
@@ -278,7 +281,8 @@ class Config
$this->m_bLogNotification = isset($MySettings['log_notification']) ? (bool) trim($MySettings['log_notification']) : DEFAULT_LOG_NOTIFICATION;
$this->m_bLogIssue = isset($MySettings['log_issue']) ? (bool) trim($MySettings['log_issue']) : DEFAULT_LOG_ISSUE;
$this->m_bLogWebService = isset($MySettings['log_web_service']) ? (bool) trim($MySettings['log_web_service']) : DEFAULT_LOG_WEB_SERVICE;
$this->m_bLogDuration = isset($MySettings['log_duration']) ? (bool) trim($MySettings['log_duration']) : DEFAULT_LOG_DURATION;
$this->m_bLogKPIDuration = isset($MySettings['log_kpi_duration']) ? (bool) trim($MySettings['log_kpi_duration']) : DEFAULT_LOG_KPI_DURATION;
$this->m_bLogKPIMemory = isset($MySettings['log_kpi_memory']) ? (bool) trim($MySettings['log_kpi_memory']) : DEFAULT_LOG_KPI_MEMORY;
$this->m_iMinDisplayLimit = isset($MySettings['min_display_limit']) ? trim($MySettings['min_display_limit']) : DEFAULT_MIN_DISPLAY_LIMIT;
$this->m_iMaxDisplayLimit = isset($MySettings['max_display_limit']) ? trim($MySettings['max_display_limit']) : DEFAULT_MAX_DISPLAY_LIMIT;
$this->m_iStandardReloadInterval = isset($MySettings['standard_reload_interval']) ? trim($MySettings['standard_reload_interval']) : DEFAULT_STANDARD_RELOAD_INTERVAL;
@@ -409,9 +413,14 @@ class Config
return $this->m_bLogWebService;
}
public function GetLogDuration()
public function GetLogKPIDuration()
{
return $this->m_bLogDuration;
return $this->m_bLogKPIDuration;
}
public function GetLogKPIMemory()
{
return $this->m_bLogKPIMemory;
}
public function GetMinDisplayLimit()

View File

@@ -1,82 +0,0 @@
<?php
// Copyright (C) 2010 Combodo SARL
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/**
* Mesures operations duration
*
* @author Erwan Taloc <erwan.taloc@combodo.com>
* @author Romain Quetiez <romain.quetiez@combodo.com>
* @author Denis Flaven <denis.flaven@combodo.com>
* @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
*/
class Duration
{
static $m_bEnabled = false;
static public function Enable()
{
self::$m_bEnabled = true;
}
protected $m_fStarted = null;
public function __construct()
{
if (!self::$m_bEnabled) return;
$this->m_fStarted = MyHelpers::getmicrotime();
}
// Get the duration since startup, and reset the counter for the next measure
//
public function Scratch($sMeasure)
{
if (!self::$m_bEnabled) return;
$fStopped = MyHelpers::getmicrotime();
$fDuration = $fStopped - $this->m_fStarted;
$this->Report($sMeasure.': '.round($fDuration, 3));
$this->m_fStarted = MyHelpers::getmicrotime();
}
protected function Report($sText)
{
echo "DURATION... $sText<br/>\n";
}
}
// Prototype, to be finalized later
// Reports the function duration
// One single thing to do: construct it
class FunctionDuration
{
protected $m_sFunction = null;
public function __construct()
{
$this->m_sFunction = 'my_function_name_in_call_stack';
$this->m_fStarted = MyHelpers::getmicrotime();
}
public function __destruct()
{
$this->Scratch('Exiting ');
}
}
?>

167
core/kpi.class.inc.php Normal file
View File

@@ -0,0 +1,167 @@
<?php
// Copyright (C) 2010 Combodo SARL
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/**
* Measures operations duration, memory usage, etc. (and some other KPIs)
*
* @author Erwan Taloc <erwan.taloc@combodo.com>
* @author Romain Quetiez <romain.quetiez@combodo.com>
* @author Denis Flaven <denis.flaven@combodo.com>
* @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
*/
class ExecutionKPI
{
static protected $m_bEnabled_Duration = false;
static protected $m_bEnabled_Memory = false;
static protected $m_aStats = array();
protected $m_fStarted = null;
protected $m_iInitialMemory = null;
static public function EnableDuration()
{
self::$m_bEnabled_Duration = true;
}
static public function EnableMemory()
{
self::$m_bEnabled_Memory = true;
}
static public function ReportStats()
{
foreach (self::$m_aStats as $sOperation => $aOpStats)
{
echo "====================<br/>\n";
echo "KPIs for $sOperation<br/>\n";
echo "====================<br/>\n";
$fTotalOp = 0;
$iTotalOp = 0;
foreach ($aOpStats as $sArguments => $aEvents)
{
$fTotalInter = 0;
$iTotalInter = 0;
foreach ($aEvents as $fDuration)
{
$fTotalInter += $fDuration;
$iTotalInter++;
}
$fTotalOp += $fTotalInter;
$iTotalOp++;
echo "$sArguments: $iTotalInter (".round($fTotalInter, 3).")<br/>\n";
}
echo "Total: $iTotalOp (".round($fTotalOp, 3).")<br/>\n";
echo "====================<br/>\n";
}
}
public function __construct()
{
$this->ResetCounters();
}
// Get the duration since startup, and reset the counter for the next measure
//
public function ComputeAndReport($sOperationDesc)
{
if (self::$m_bEnabled_Duration)
{
$fStopped = MyHelpers::getmicrotime();
$fDuration = $fStopped - $this->m_fStarted;
$this->Report($sOperationDesc.' / duration: '.round($fDuration, 3));
}
if (self::$m_bEnabled_Memory)
{
$iMemory = self::memory_get_usage();
$iMemoryUsed = $iMemory - $this->m_iInitialMemory;
$this->Report($sOperationDesc.' / memory: '.self::MemStr($iMemoryUsed).' (Total: '.self::MemStr($iMemory).')');
}
$this->ResetCounters();
}
public function ComputeStats($sOperation, $sArguments)
{
if (self::$m_bEnabled_Duration)
{
$fStopped = MyHelpers::getmicrotime();
$fDuration = $fStopped - $this->m_fStarted;
self::$m_aStats[$sOperation][$sArguments][] = $fDuration;
}
}
protected function ResetCounters()
{
if (self::$m_bEnabled_Duration)
{
$this->m_fStarted = MyHelpers::getmicrotime();
}
if (self::$m_bEnabled_Memory)
{
$this->m_iInitialMemory = self::memory_get_usage();
}
}
protected function Report($sText)
{
echo "$sText<br/>\n";
}
static protected function MemStr($iMemory)
{
return round($iMemory / 1024).' Kb';
}
static protected function memory_get_usage()
{
if (function_exists('memory_get_usage'))
{
return memory_get_usage(true);
}
// Copied from the PHP manual
//
//If its Windows
//Tested on Win XP Pro SP2. Should work on Win 2003 Server too
//Doesn't work for 2000
//If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642
if (substr(PHP_OS,0,3) == 'WIN')
{
$output = array();
exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output);
return preg_replace( '/[\D]/', '', $output[5] ) * 1024;
}
else
{
//We now assume the OS is UNIX
//Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4
//This should work on most UNIX systems
$pid = getmypid();
exec("ps -eo%mem,rss,pid | grep $pid", $output);
$output = explode(" ", $output[0]);
//rss is given in 1024 byte units
return $output[1] * 1024;
}
}
}
?>

View File

@@ -1534,9 +1534,9 @@ abstract class MetaModel
// Query caching
//
$bQueryCacheEnabled = true;
$sOqlQuery = $oFilter->ToOql();
if ($bQueryCacheEnabled)
{
$sOqlQuery = $oFilter->ToOql();
// Warning: using directly the query string as the key to the hash array can FAIL if the string
// is long and the differences are only near the end... so it's safer (but not bullet proof?)
// to use a hash (like md5) of the string as the key !
@@ -1565,7 +1565,9 @@ abstract class MetaModel
$aTableAliases = array();
$oConditionTree = $oFilter->GetCriteria();
$oKPI = new ExecutionKPI();
$oSelect = self::MakeQuery($oFilter->GetSelectedClasses(), $oConditionTree, $aClassAliases, $aTableAliases, $aTranslation, $oFilter);
$oKPI->ComputeStats('MakeQuery (select)', $sOqlQuery);
self::$m_aQueryStructCache[$sOqlQuery] = clone $oSelect;
}
@@ -3169,9 +3171,13 @@ abstract class MetaModel
self::$m_bLogWebService = false;
}
if (self::$m_oConfig->GetLogDuration())
if (self::$m_oConfig->GetLogKPIDuration())
{
Duration::Enable();
ExecutionKPI::EnableDuration();
}
if (self::$m_oConfig->GetLogKPIMemory())
{
ExecutionKPI::EnableMemory();
}
// Note: load the dictionary as soon as possible, because it might be
@@ -3206,13 +3212,13 @@ abstract class MetaModel
$sSource = self::$m_oConfig->GetDBName();
$sTablePrefix = self::$m_oConfig->GetDBSubname();
$oDuration = new Duration();
$oKPI = new ExecutionKPI();
// The include have been included, let's browse the existing classes and
// develop some data based on the proposed model
self::InitClasses($sTablePrefix);
$oDuration->Scratch('Initialization of Data model structures');
$oKPI->ComputeAndReport('Initialization of Data model structures');
self::$m_sDBName = $sSource;
self::$m_sTablePrefix = $sTablePrefix;

View File

@@ -35,7 +35,7 @@ require_once('cmdbsource.class.inc.php');
require_once('sqlquery.class.inc.php');
require_once('log.class.inc.php');
require_once('duration.class.inc.php');
require_once('kpi.class.inc.php');
require_once('dbobject.class.php');
require_once('dbobjectsearch.class.php');

View File

@@ -131,7 +131,7 @@ abstract class User extends cmdbAbstractObject
return;
}
$oDuration = new Duration();
$oKPI = new ExecutionKPI();
$aDisplayData = array();
foreach (MetaModel::GetClasses($sClassCategory) as $sClass)
@@ -167,7 +167,7 @@ abstract class User extends cmdbAbstractObject
);
}
$oDuration->Scratch('Computation of user rights');
$oKPI->ComputeAndReport('Computation of user rights');
$aDisplayConfig = array();
$aDisplayConfig['class'] = array('label' => Dict::S('UI:UserManagement:Class'), 'description' => Dict::S('UI:UserManagement:Class+'));

View File

@@ -455,15 +455,18 @@ try
require_once('../application/itopwebpage.class.inc.php');
require_once('../application/wizardhelper.class.inc.php');
$oDuration = new Duration();
require_once('../application/startup.inc.php');
$oAppContext = new ApplicationContext();
$currentOrganization = utils::ReadParam('org_id', '');
$operation = utils::ReadParam('operation', '');
$oKPI = new ExecutionKPI();
require_once('../application/loginwebpage.class.inc.php');
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
$oKPI->ComputeAndReport('User login');
$oP = new iTopWebPage(Dict::S('UI:WelcomeToITop'), $currentOrganization);
switch($operation)
@@ -1390,7 +1393,8 @@ EOF
$oP->set_title($oMenuNode->GetLabel());
}
}
$oDuration->Scratch('Total page execution time');
$oKPI->ComputeAndReport('GUI creation before output');
ExecutionKPI::ReportStats();
////MetaModel::ShowQueryTrace();
$oP->output();
}

View File

@@ -89,7 +89,7 @@ ob_start('FatalErrorCatcher'); // Start capturing the output, and pass it throug
require_once('../core/config.class.inc.php');
require_once('../core/log.class.inc.php');
require_once('../core/duration.class.inc.php');
require_once('../core/kpi.class.inc.php');
require_once('../core/cmdbsource.class.inc.php');
require_once('./xmldataloader.class.inc.php');

View File

@@ -26,7 +26,7 @@
require_once('../application/utils.inc.php');
require_once('../core/config.class.inc.php');
require_once('../core/log.class.inc.php');
require_once('../core/duration.class.inc.php');
require_once('../core/kpi.class.inc.php');
require_once('../core/cmdbsource.class.inc.php');
require_once('./setuppage.class.inc.php');
@@ -376,7 +376,7 @@ function CheckServerConnection(SetupWebPage $oP, $sDBServer, $sDBUser, $sDBPwd)
function InitDataModel(SetupWebPage $oP, $sConfigFileName, $bModelOnly = true)
{
require_once('../core/log.class.inc.php');
require_once('../core/duration.class.inc.php');
require_once('../core/kpi.class.inc.php');
require_once('../core/coreexception.class.inc.php');
require_once('../core/dict.class.inc.php');
require_once('../core/attributedef.class.inc.php');

View File

@@ -88,7 +88,7 @@ class XMLDataLoader
protected function InitDataModel($sConfigFileName)
{
require_once('../core/log.class.inc.php');
require_once('../core/duration.class.inc.php');
require_once('../core/kpi.class.inc.php');
require_once('../core/coreexception.class.inc.php');
require_once('../core/dict.class.inc.php');
require_once('../core/attributedef.class.inc.php');