PageRenderTime 28ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/nacridan/lib/Profiler.inc.php

https://gitlab.com/nacridan/Nacridan
PHP | 244 lines | 144 code | 35 blank | 65 comment | 11 complexity | 18e8c81c625a632015eef5c8a399cdca MD5 | raw file
  1. <?php
  2. /********************************************************************************\
  3. * Copyright (C) Carl Taylor (cjtaylor@adepteo.com) *
  4. * Copyright (C) Torben Nehmer (torben@nehmer.net) for Code Cleanup *
  5. * *
  6. * This program is free software; you can redistribute it and/or *
  7. * modify it under the terms of the GNU General Public License *
  8. * as published by the Free Software Foundation; either version 2 *
  9. * of the License, or (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the Free Software *
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. \********************************************************************************/
  20. // / Enable multiple timers to aid profiling of performance over sections of code
  21. class Profiler
  22. {
  23. var $description;
  24. var $startTime;
  25. var $endTime;
  26. var $initTime;
  27. var $cur_timer;
  28. var $stack;
  29. var $trail;
  30. var $trace;
  31. var $count;
  32. var $running;
  33. /**
  34. * Initialise the timer.
  35. * with the current micro time
  36. */
  37. function Profiler($output_enabled = false, $trace_enabled = false)
  38. {
  39. $this->description = array();
  40. $this->startTime = array();
  41. $this->endTime = array();
  42. $this->initTime = 0;
  43. $this->cur_timer = "";
  44. $this->stack = array();
  45. $this->trail = "";
  46. $this->trace = "";
  47. $this->count = array();
  48. $this->running = array();
  49. $this->initTime = $this->getMicroTime();
  50. $this->output_enabled = $output_enabled;
  51. $this->trace_enabled = $trace_enabled;
  52. $this->startTimer('unprofiled');
  53. }
  54. // Public Methods
  55. /**
  56. * Start an individual timer
  57. * This will pause the running timer and place it on a stack.
  58. *
  59. * @param string $name
  60. * name of the timer
  61. * @param
  62. * string optional $desc description of the timer
  63. */
  64. function startTimer($name, $desc = "")
  65. {
  66. $this->trace .= "start $name\n";
  67. $n = array_push($this->stack, $this->cur_timer);
  68. $this->__suspendTimer($this->stack[$n - 1]);
  69. $this->startTime[$name] = $this->getMicroTime();
  70. $this->cur_timer = $name;
  71. $this->description[$name] = $desc;
  72. if (! array_key_exists($name, $this->count))
  73. $this->count[$name] = 1;
  74. else
  75. $this->count[$name] ++;
  76. }
  77. /**
  78. * Stop an individual timer
  79. * Restart the timer that was running before this one
  80. *
  81. * @param string $name
  82. * name of the timer
  83. */
  84. function stopTimer($name)
  85. {
  86. $this->trace .= "stop $name\n";
  87. $this->endTime[$name] = $this->getMicroTime();
  88. if (! array_key_exists($name, $this->running))
  89. $this->running[$name] = $this->elapsedTime($name);
  90. else
  91. $this->running[$name] += $this->elapsedTime($name);
  92. $this->cur_timer = array_pop($this->stack);
  93. $this->__resumeTimer($this->cur_timer);
  94. }
  95. /**
  96. * measure the elapsed time of a timer without stoping the timer if
  97. * it is still running
  98. */
  99. function elapsedTime($name)
  100. {
  101. // This shouldn't happen, but it does once.
  102. if (! array_key_exists($name, $this->startTime))
  103. return 0;
  104. if (array_key_exists($name, $this->endTime)) {
  105. return ($this->endTime[$name] - $this->startTime[$name]);
  106. } else {
  107. $now = $this->getMicroTime();
  108. return ($now - $this->startTime[$name]);
  109. }
  110. }
  111. // end start_time
  112. /**
  113. * Measure the elapsed time since the profile class was initialised
  114. */
  115. function elapsedOverall()
  116. {
  117. $oaTime = $this->getMicroTime() - $this->initTime;
  118. return ($oaTime);
  119. }
  120. // end start_time
  121. /**
  122. * print out a log of all the timers that were registered
  123. */
  124. function printTimers($enabled = false)
  125. {
  126. if ($this->output_enabled || $enabled) {
  127. $TimedTotal = 0;
  128. $tot_perc = 0;
  129. ksort($this->description);
  130. print("<pre style='position: absolute; top: 580px; color: #FFFFFF;'>\n");
  131. $oaTime = $this->getMicroTime() - $this->initTime;
  132. echo "============================================================================\n";
  133. echo " PROFILER OUTPUT\n";
  134. echo "============================================================================\n";
  135. print("Calls Time Routine\n");
  136. echo "-----------------------------------------------------------------------------\n";
  137. while (list ($key, $val) = each($this->description)) {
  138. $t = $this->elapsedTime($key);
  139. $total = $this->running[$key];
  140. $count = $this->count[$key];
  141. $TimedTotal += $total;
  142. $perc = ($total / $oaTime) * 100;
  143. $tot_perc += $perc;
  144. // $perc=sprintf("%3.2f", $perc );
  145. printf("%3d %3.4f ms (%3.2f %%) %s\n", $count, $total * 1000, $perc, $key);
  146. }
  147. echo "\n";
  148. $missed = $oaTime - $TimedTotal;
  149. $perc = ($missed / $oaTime) * 100;
  150. $tot_perc += $perc;
  151. // $perc=sprintf("%3.2f", $perc );
  152. printf(" %3.4f ms (%3.2f %%) %s\n", $missed * 1000, $perc, "Missed");
  153. echo "============================================================================\n";
  154. printf(" %3.4f ms (%3.2f %%) %s\n", $oaTime * 1000, $tot_perc, "OVERALL TIME");
  155. echo "============================================================================\n";
  156. print("</pre>");
  157. }
  158. }
  159. function printTrace($enabled = false)
  160. {
  161. if ($this->trace_enabled || $enabled) {
  162. print("<pre>");
  163. print("Trace\n$this->trace\n\n");
  164. print("</pre>");
  165. }
  166. }
  167. // / Internal Use Only Functions
  168. /**
  169. * Get the current time as accuratly as possible
  170. */
  171. function getMicroTime()
  172. {
  173. $tmp = explode(" ", microtime());
  174. $rt = $tmp[0] + $tmp[1];
  175. return $rt;
  176. }
  177. /**
  178. * resume an individual timer
  179. */
  180. function __resumeTimer($name)
  181. {
  182. $this->trace .= "resume $name\n";
  183. $this->startTime[$name] = $this->getMicroTime();
  184. }
  185. /**
  186. * suspend an individual timer
  187. */
  188. function __suspendTimer($name)
  189. {
  190. $this->trace .= "suspend $name\n";
  191. $this->endTime[$name] = $this->getMicroTime();
  192. if (! array_key_exists($name, $this->running))
  193. $this->running[$name] = $this->elapsedTime($name);
  194. else
  195. $this->running[$name] += $this->elapsedTime($name);
  196. }
  197. }
  198. $prof = new Profiler(false, false);
  199. function profiler_start($name)
  200. {
  201. global $prof;
  202. $prof->startTimer($name);
  203. }
  204. function profiler_stop($name)
  205. {
  206. global $prof;
  207. $prof->stopTimer($name);
  208. }
  209. ?>