/lib/ezc/Debug/src/debug_timer.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 217 lines · 93 code · 28 blank · 96 comment · 11 complexity · 9ac64c54e1fe03087b88703c1338e6c7 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcDebugTimer class.
  4. *
  5. * @package Debug
  6. * @version //autogentag//
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. * @access private
  10. */
  11. /**
  12. * The ezcDebugTimer class holds several timers.
  13. *
  14. * The timers can be started and stopped individually. The timer data can be retrieved
  15. * with the getStructure method.
  16. *
  17. * @package Debug
  18. * @version //autogentag//
  19. * @access private
  20. */
  21. class ezcDebugTimer
  22. {
  23. /**
  24. * An internal structure which stores sources, groups, and timers.
  25. *
  26. * @var array(string=>ezcDebugTimerStruct)
  27. */
  28. private $timers;
  29. /**
  30. * Similar to {@link $timers} but stores those that are currently running.
  31. *
  32. * @var array(string=>ezcDebugTimerStruct)
  33. */
  34. private $runningTimers;
  35. /**
  36. * The total number of running timers.
  37. *
  38. * @var int
  39. */
  40. private $totalRunningTimers;
  41. /**
  42. * The submitted timer number.
  43. *
  44. * @var int
  45. */
  46. private $number = 0;
  47. /**
  48. * Constructs a timer object with no timers.
  49. */
  50. public function __construct()
  51. {
  52. $this->reset();
  53. }
  54. /**
  55. * Throws always an {@link ezcBasePropertyNotFoundException}.
  56. *
  57. * @throws ezcBasePropertyNotFoundException
  58. * @param string $name
  59. * @return mixed
  60. */
  61. public function __get( $name )
  62. {
  63. throw new ezcBasePropertyNotFoundException( $name );
  64. }
  65. /**
  66. * Throws always an {@link ezcBasePropertyNotFoundException}.
  67. *
  68. * @throws ezcBasePropertyNotFoundException
  69. * @param string $name
  70. * @param string $value
  71. * @return mixed
  72. */
  73. public function __set( $name, $value )
  74. {
  75. throw new ezcBasePropertyNotFoundException( $name );
  76. }
  77. /**
  78. * Resets the timer object to its initial state with no timers.
  79. *
  80. * @return void
  81. */
  82. public function reset()
  83. {
  84. $this->timers = array();
  85. $this->runningTimers = array();
  86. $this->totalRunningTimers = 0;
  87. }
  88. /**
  89. * Starts the timer identified by $name with the group $group and returns true on success.
  90. *
  91. * If the timer was already started false is returned.
  92. *
  93. * @param string $name
  94. * @param string $group
  95. * @return bool
  96. */
  97. public function startTimer( $name, $group )
  98. {
  99. if ( !isset( $this->runningTimers[ $name ] ) )
  100. {
  101. $this->totalRunningTimers++;
  102. $this->runningTimers[$name] = new ezcDebugTimerStruct();
  103. $this->runningTimers[$name]->name = $name;
  104. $this->runningTimers[$name]->group = $group;
  105. $this->runningTimers[$name]->switchTime = array();
  106. $this->runningTimers[$name]->startTime = microtime( true );
  107. $this->runningTimers[$name]->startNumber = $this->number++;
  108. return true;
  109. }
  110. return false;
  111. }
  112. /**
  113. * Stops the timer $oldName and starts the timer $newName and return true on success.
  114. *
  115. * If the timer $oldName does not exist or if it was omitted with several timers running
  116. * false is returned.
  117. *
  118. * @param string $newName
  119. * @param string|bool $oldName
  120. * @return bool
  121. */
  122. public function switchTimer( $newName, $oldName = false )
  123. {
  124. if ( $this->totalRunningTimers < 1 )
  125. {
  126. return false;
  127. }
  128. if ( $oldName === false )
  129. {
  130. if ( $this->totalRunningTimers > 1 )
  131. {
  132. return false;
  133. }
  134. $oldName = key( $this->runningTimers );
  135. }
  136. if ( isset( $this->runningTimers[ $oldName ] ) )
  137. {
  138. if ( $newName != $oldName )
  139. {
  140. $this->runningTimers[$newName] = $this->runningTimers[$oldName];
  141. unset( $this->runningTimers[$oldName] );
  142. }
  143. $switchStruct = new ezcDebugSwitchTimerStruct();
  144. $switchStruct->name = $newName;
  145. $switchStruct->time = microtime( true );
  146. $this->runningTimers[$newName]->switchTime[] = $switchStruct;
  147. return true;
  148. }
  149. return false;
  150. }
  151. /**
  152. * Stop the timer identified by $name and return true on success.
  153. *
  154. * If the timer $name does not exist or if it was omitted with several timers running
  155. * false is returned. $name can be omitted (false) if only 1 timer is running.
  156. *
  157. * @param string|bool $name
  158. * @return bool
  159. * @todo Error handling if multiple timers are running.
  160. */
  161. public function stopTimer( $name = false )
  162. {
  163. if ( $name === false && $this->totalRunningTimers == 1 )
  164. {
  165. $name = key( $this->runningTimers );
  166. }
  167. if ( isset( $this->runningTimers[ $name ] ) )
  168. {
  169. $this->runningTimers[$name]->stopTime = microtime( true );
  170. $this->runningTimers[$name]->elapsedTime = $this->runningTimers[$name]->stopTime - $this->runningTimers[$name]->startTime;
  171. $this->runningTimers[$name]->stopNumber = $this->number++;
  172. $this->timers[] = $this->runningTimers[$name];
  173. unset( $this->runningTimers[$name] );
  174. $this->totalRunningTimers--;
  175. return true;
  176. }
  177. return false;
  178. }
  179. /**
  180. * Returns an array with the timer data.
  181. *
  182. * @return array(ezcDebugTimerStruct)
  183. */
  184. public function getTimeData()
  185. {
  186. return $this->timers;
  187. }
  188. }
  189. ?>