kpi.class.inc.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Measures operations duration, memory usage, etc. (and some other KPIs)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. class ExecutionKPI
  25. {
  26. static protected $m_bEnabled_Duration = false;
  27. static protected $m_bEnabled_Memory = false;
  28. static protected $m_aStats = array();
  29. protected $m_fStarted = null;
  30. protected $m_iInitialMemory = null;
  31. static public function EnableDuration()
  32. {
  33. self::$m_bEnabled_Duration = true;
  34. }
  35. static public function EnableMemory()
  36. {
  37. self::$m_bEnabled_Memory = true;
  38. }
  39. static public function ReportStats()
  40. {
  41. foreach (self::$m_aStats as $sOperation => $aOpStats)
  42. {
  43. echo "====================<br/>\n";
  44. echo "KPIs for $sOperation<br/>\n";
  45. echo "====================<br/>\n";
  46. $fTotalOp = 0;
  47. $iTotalOp = 0;
  48. foreach ($aOpStats as $sArguments => $aEvents)
  49. {
  50. $fTotalInter = 0;
  51. $iTotalInter = 0;
  52. foreach ($aEvents as $fDuration)
  53. {
  54. $fTotalInter += $fDuration;
  55. $iTotalInter++;
  56. }
  57. $fTotalOp += $fTotalInter;
  58. $iTotalOp++;
  59. echo "$sArguments: $iTotalInter (".round($fTotalInter, 3).")<br/>\n";
  60. }
  61. echo "Total: $iTotalOp (".round($fTotalOp, 3).")<br/>\n";
  62. echo "====================<br/>\n";
  63. }
  64. }
  65. public function __construct()
  66. {
  67. $this->ResetCounters();
  68. }
  69. // Get the duration since startup, and reset the counter for the next measure
  70. //
  71. public function ComputeAndReport($sOperationDesc)
  72. {
  73. if (self::$m_bEnabled_Duration)
  74. {
  75. $fStopped = MyHelpers::getmicrotime();
  76. $fDuration = $fStopped - $this->m_fStarted;
  77. $this->Report($sOperationDesc.' / duration: '.round($fDuration, 3));
  78. }
  79. if (self::$m_bEnabled_Memory)
  80. {
  81. $iMemory = self::memory_get_usage();
  82. $iMemoryUsed = $iMemory - $this->m_iInitialMemory;
  83. $this->Report($sOperationDesc.' / memory: '.self::MemStr($iMemoryUsed).' (Total: '.self::MemStr($iMemory).')');
  84. }
  85. $this->ResetCounters();
  86. }
  87. public function ComputeStats($sOperation, $sArguments)
  88. {
  89. if (self::$m_bEnabled_Duration)
  90. {
  91. $fStopped = MyHelpers::getmicrotime();
  92. $fDuration = $fStopped - $this->m_fStarted;
  93. self::$m_aStats[$sOperation][$sArguments][] = $fDuration;
  94. }
  95. }
  96. protected function ResetCounters()
  97. {
  98. if (self::$m_bEnabled_Duration)
  99. {
  100. $this->m_fStarted = MyHelpers::getmicrotime();
  101. }
  102. if (self::$m_bEnabled_Memory)
  103. {
  104. $this->m_iInitialMemory = self::memory_get_usage();
  105. }
  106. }
  107. protected function Report($sText)
  108. {
  109. echo "$sText<br/>\n";
  110. }
  111. static protected function MemStr($iMemory)
  112. {
  113. return round($iMemory / 1024).' Kb';
  114. }
  115. static protected function memory_get_usage()
  116. {
  117. if (function_exists('memory_get_usage'))
  118. {
  119. return memory_get_usage(true);
  120. }
  121. // Copied from the PHP manual
  122. //
  123. //If its Windows
  124. //Tested on Win XP Pro SP2. Should work on Win 2003 Server too
  125. //Doesn't work for 2000
  126. //If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642
  127. if (substr(PHP_OS,0,3) == 'WIN')
  128. {
  129. $output = array();
  130. exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output);
  131. return preg_replace( '/[\D]/', '', $output[5] ) * 1024;
  132. }
  133. else
  134. {
  135. //We now assume the OS is UNIX
  136. //Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4
  137. //This should work on most UNIX systems
  138. $pid = getmypid();
  139. exec("ps -eo%mem,rss,pid | grep $pid", $output);
  140. $output = explode(" ", $output[0]);
  141. //rss is given in 1024 byte units
  142. return $output[1] * 1024;
  143. }
  144. }
  145. }
  146. ?>