kpi.class.inc.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "<h2>KPIs for $sOperation</h2>\n";
  44. $fTotalOp = 0;
  45. $iTotalOp = 0;
  46. $fMinOp = null;
  47. $fMaxOp = 0;
  48. echo "<ul>\n";
  49. foreach ($aOpStats as $sArguments => $aEvents)
  50. {
  51. $fTotalInter = 0;
  52. $fMinInter = null;
  53. $fMaxInter = 0;
  54. foreach ($aEvents as $fDuration)
  55. {
  56. $fTotalInter += $fDuration;
  57. $fMinInter = is_null($fMinInter) ? $fDuration : min($fMinInter, $fDuration);
  58. $fMaxInter = max($fMaxInter, $fDuration);
  59. $fMinOp = is_null($fMinOp) ? $fDuration : min($fMinOp, $fDuration);
  60. $fMaxOp = max($fMaxOp, $fDuration);
  61. }
  62. $fTotalOp += $fTotalInter;
  63. $iTotalOp++;
  64. $iCountInter = count($aEvents);
  65. $sTotalInter = round($fTotalInter, 3)."s";
  66. if ($iCountInter > 1)
  67. {
  68. $sMinInter = round($fMinInter, 3)."s";
  69. $sMaxInter = round($fMaxInter, 3)."s";
  70. $sTimeDesc = "$sTotalInter (from $sMinInter to $sMaxInter) in $iCountInter times";
  71. }
  72. else
  73. {
  74. $sTimeDesc = "$sTotalInter";
  75. }
  76. echo "<li>Spent $sTimeDesc, on: <span style=\"font-size:60%\">$sArguments</span></li>\n";
  77. }
  78. echo "</ul>\n";
  79. echo "<ul>Sumary for $sOperation\n";
  80. echo "<li>Total: $iTotalOp (".round($fTotalOp, 3).")</li>\n";
  81. echo "<li>Min: ".round($fMinOp, 3)."</li>\n";
  82. echo "<li>Max: ".round($fMaxOp, 3)."</li>\n";
  83. echo "<li>Avg: ".round($fTotalOp / $iTotalOp, 3)."</li>\n";
  84. echo "</ul>\n";
  85. }
  86. }
  87. public function __construct()
  88. {
  89. $this->ResetCounters();
  90. }
  91. // Get the duration since startup, and reset the counter for the next measure
  92. //
  93. public function ComputeAndReport($sOperationDesc)
  94. {
  95. if (self::$m_bEnabled_Duration)
  96. {
  97. $fStopped = MyHelpers::getmicrotime();
  98. $fDuration = $fStopped - $this->m_fStarted;
  99. $this->Report($sOperationDesc.' / duration: '.round($fDuration, 3));
  100. }
  101. if (self::$m_bEnabled_Memory)
  102. {
  103. $iMemory = self::memory_get_usage();
  104. $iMemoryUsed = $iMemory - $this->m_iInitialMemory;
  105. $this->Report($sOperationDesc.' / memory: '.self::MemStr($iMemoryUsed).' (Total: '.self::MemStr($iMemory).')');
  106. if (function_exists('memory_get_peak_usage'))
  107. {
  108. $iMemoryPeak = memory_get_peak_usage();
  109. $this->Report($sOperationDesc.' / memory peak: '.self::MemStr($iMemoryPeak));
  110. }
  111. }
  112. $this->ResetCounters();
  113. }
  114. public function ComputeStats($sOperation, $sArguments)
  115. {
  116. if (self::$m_bEnabled_Duration)
  117. {
  118. $fStopped = MyHelpers::getmicrotime();
  119. $fDuration = $fStopped - $this->m_fStarted;
  120. self::$m_aStats[$sOperation][$sArguments][] = $fDuration;
  121. }
  122. }
  123. protected function ResetCounters()
  124. {
  125. if (self::$m_bEnabled_Duration)
  126. {
  127. $this->m_fStarted = MyHelpers::getmicrotime();
  128. }
  129. if (self::$m_bEnabled_Memory)
  130. {
  131. $this->m_iInitialMemory = self::memory_get_usage();
  132. }
  133. }
  134. protected function Report($sText)
  135. {
  136. echo "$sText<br/>\n";
  137. }
  138. static protected function MemStr($iMemory)
  139. {
  140. return round($iMemory / 1024).' Kb';
  141. }
  142. static protected function memory_get_usage()
  143. {
  144. if (function_exists('memory_get_usage'))
  145. {
  146. return memory_get_usage(true);
  147. }
  148. // Copied from the PHP manual
  149. //
  150. //If its Windows
  151. //Tested on Win XP Pro SP2. Should work on Win 2003 Server too
  152. //Doesn't work for 2000
  153. //If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642
  154. if (substr(PHP_OS,0,3) == 'WIN')
  155. {
  156. $output = array();
  157. exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output);
  158. return preg_replace( '/[\D]/', '', $output[5] ) * 1024;
  159. }
  160. else
  161. {
  162. //We now assume the OS is UNIX
  163. //Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4
  164. //This should work on most UNIX systems
  165. $pid = getmypid();
  166. exec("ps -eo%mem,rss,pid | grep $pid", $output);
  167. $output = explode(" ", $output[0]);
  168. //rss is given in 1024 byte units
  169. return $output[1] * 1024;
  170. }
  171. }
  172. }
  173. class ApplicationStartupKPI extends ExecutionKPI
  174. {
  175. public function __construct()
  176. {
  177. global $fItopStarted;
  178. $this->m_fStarted = $fItopStarted;
  179. }
  180. }
  181. ?>