Base for a static organization website

DebugTimer.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since DebugKit 0.1
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. App::uses('Debugger', 'Utility');
  15. /**
  16. * Contains methods for Profiling and creating timers.
  17. *
  18. */
  19. class DebugTimer {
  20. /**
  21. * Internal timers array
  22. *
  23. * @var array
  24. */
  25. protected static $_timers = array();
  26. /**
  27. * Start an benchmarking timer.
  28. *
  29. * @param string $name The name of the timer to start.
  30. * @param string $message A message for your timer
  31. * @return boolean Always true
  32. */
  33. public static function start($name = null, $message = null) {
  34. $start = microtime(true);
  35. if (!$name) {
  36. $named = false;
  37. $calledFrom = debug_backtrace();
  38. $_name = $name = Debugger::trimpath($calledFrom[0]['file']) . ' line ' . $calledFrom[0]['line'];
  39. } else {
  40. $named = true;
  41. }
  42. if (!$message) {
  43. $message = $name;
  44. }
  45. $_name = $name;
  46. $i = 1;
  47. while (isset(self::$_timers[$name])) {
  48. $i++;
  49. $name = $_name . ' #' . $i;
  50. }
  51. if ($i > 1) {
  52. $message .= ' #' . $i;
  53. }
  54. self::$_timers[$name] = array(
  55. 'start' => $start,
  56. 'message' => $message,
  57. 'named' => $named
  58. );
  59. return true;
  60. }
  61. /**
  62. * Stop a benchmarking timer.
  63. *
  64. * $name should be the same as the $name used in startTimer().
  65. *
  66. * @param string $name The name of the timer to end.
  67. * @return boolean true if timer was ended, false if timer was not started.
  68. */
  69. public static function stop($name = null) {
  70. $end = microtime(true);
  71. if (!$name) {
  72. $names = array_reverse(array_keys(self::$_timers));
  73. foreach ($names as $name) {
  74. if (!empty(self::$_timers[$name]['end'])) {
  75. continue;
  76. }
  77. if (empty(self::$_timers[$name]['named'])) {
  78. break;
  79. }
  80. }
  81. } else {
  82. $i = 1;
  83. $_name = $name;
  84. while (isset(self::$_timers[$name])) {
  85. if (empty(self::$_timers[$name]['end'])) {
  86. break;
  87. }
  88. $i++;
  89. $name = $_name . ' #' . $i;
  90. }
  91. }
  92. if (!isset(self::$_timers[$name])) {
  93. return false;
  94. }
  95. self::$_timers[$name]['end'] = $end;
  96. return true;
  97. }
  98. /**
  99. * Get all timers that have been started and stopped.
  100. * Calculates elapsed time for each timer. If clear is true, will delete existing timers
  101. *
  102. * @param boolean $clear false
  103. * @return array
  104. */
  105. public static function getAll($clear = false) {
  106. $start = self::requestStartTime();
  107. $now = microtime(true);
  108. $times = array();
  109. if (!empty(self::$_timers)) {
  110. $firstTimer = reset(self::$_timers);
  111. $_end = $firstTimer['start'];
  112. } else {
  113. $_end = $now;
  114. }
  115. $times['Core Processing (Derived from $_SERVER["REQUEST_TIME"])'] = array(
  116. 'message' => __d('debug_kit', 'Core Processing (Derived from $_SERVER["REQUEST_TIME"])'),
  117. 'start' => 0,
  118. 'end' => $_end - $start,
  119. 'time' => round($_end - $start, 6),
  120. 'named' => null
  121. );
  122. foreach (self::$_timers as $name => $timer) {
  123. if (!isset($timer['end'])) {
  124. $timer['end'] = $now;
  125. }
  126. $times[$name] = array_merge($timer, array(
  127. 'start' => $timer['start'] - $start,
  128. 'end' => $timer['end'] - $start,
  129. 'time' => self::elapsedTime($name)
  130. ));
  131. }
  132. if ($clear) {
  133. self::$_timers = array();
  134. }
  135. return $times;
  136. }
  137. /**
  138. * Clear all existing timers
  139. *
  140. * @return boolean true
  141. */
  142. public static function clear() {
  143. self::$_timers = array();
  144. return true;
  145. }
  146. /**
  147. * Get the difference in time between the timer start and timer end.
  148. *
  149. * @param $name string the name of the timer you want elapsed time for.
  150. * @param $precision int the number of decimal places to return, defaults to 5.
  151. * @return float number of seconds elapsed for timer name, 0 on missing key
  152. */
  153. public static function elapsedTime($name = 'default', $precision = 5) {
  154. if (!isset(self::$_timers[$name]['start']) || !isset(self::$_timers[$name]['end'])) {
  155. return 0;
  156. }
  157. return round(self::$_timers[$name]['end'] - self::$_timers[$name]['start'], $precision);
  158. }
  159. /**
  160. * Get the total execution time until this point
  161. *
  162. * @return float elapsed time in seconds since script start.
  163. */
  164. public static function requestTime() {
  165. $start = self::requestStartTime();
  166. $now = microtime(true);
  167. return ($now - $start);
  168. }
  169. /**
  170. * get the time the current request started.
  171. *
  172. * @return float time of request start
  173. */
  174. public static function requestStartTime() {
  175. if (defined('TIME_START')) {
  176. $startTime = TIME_START;
  177. } elseif (isset($GLOBALS['TIME_START'])) {
  178. $startTime = $GLOBALS['TIME_START'];
  179. } else {
  180. $startTime = env('REQUEST_TIME');
  181. }
  182. return $startTime;
  183. }
  184. }