kpi.class.inc.php 4.8 KB

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