Stopwatch.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Stopwatch;
  11. /**
  12. * Stopwatch provides a way to profile code.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Stopwatch
  17. {
  18. /**
  19. * @var Section[]
  20. */
  21. private $sections;
  22. /**
  23. * @var array
  24. */
  25. private $activeSections;
  26. public function __construct()
  27. {
  28. $this->sections = $this->activeSections = array('__root__' => new Section('__root__'));
  29. }
  30. /**
  31. * Creates a new section or re-opens an existing section.
  32. *
  33. * @param string|null $id The id of the session to re-open, null to create a new one
  34. *
  35. * @throws \LogicException When the section to re-open is not reachable
  36. */
  37. public function openSection($id = null)
  38. {
  39. $current = end($this->activeSections);
  40. if (null !== $id && null === $current->get($id)) {
  41. throw new \LogicException(sprintf('The section "%s" has been started at an other level and can not be opened.', $id));
  42. }
  43. $this->start('__section__.child', 'section');
  44. $this->activeSections[] = $current->open($id);
  45. $this->start('__section__');
  46. }
  47. /**
  48. * Stops the last started section.
  49. *
  50. * The id parameter is used to retrieve the events from this section.
  51. *
  52. * @see getSectionEvents
  53. *
  54. * @param string $id The identifier of the section
  55. *
  56. * @throws \LogicException When there's no started section to be stopped
  57. */
  58. public function stopSection($id)
  59. {
  60. $this->stop('__section__');
  61. if (1 == count($this->activeSections)) {
  62. throw new \LogicException('There is no started section to stop.');
  63. }
  64. $this->sections[$id] = array_pop($this->activeSections)->setId($id);
  65. $this->stop('__section__.child');
  66. }
  67. /**
  68. * Starts an event.
  69. *
  70. * @param string $name The event name
  71. * @param string $category The event category
  72. *
  73. * @return StopwatchEvent A StopwatchEvent instance
  74. */
  75. public function start($name, $category = null)
  76. {
  77. return end($this->activeSections)->startEvent($name, $category);
  78. }
  79. /**
  80. * Checks if the event was started
  81. *
  82. * @param string $name The event name
  83. *
  84. * @return bool
  85. */
  86. public function isStarted($name)
  87. {
  88. return end($this->activeSections)->isEventStarted($name);
  89. }
  90. /**
  91. * Stops an event.
  92. *
  93. * @param string $name The event name
  94. *
  95. * @return StopwatchEvent A StopwatchEvent instance
  96. */
  97. public function stop($name)
  98. {
  99. return end($this->activeSections)->stopEvent($name);
  100. }
  101. /**
  102. * Stops then restarts an event.
  103. *
  104. * @param string $name The event name
  105. *
  106. * @return StopwatchEvent A StopwatchEvent instance
  107. */
  108. public function lap($name)
  109. {
  110. return end($this->activeSections)->stopEvent($name)->start();
  111. }
  112. /**
  113. * Gets all events for a given section.
  114. *
  115. * @param string $id A section identifier
  116. *
  117. * @return StopwatchEvent[] An array of StopwatchEvent instances
  118. */
  119. public function getSectionEvents($id)
  120. {
  121. return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : array();
  122. }
  123. }
  124. /**
  125. * @internal This class is for internal usage only
  126. *
  127. * @author Fabien Potencier <fabien@symfony.com>
  128. */
  129. class Section
  130. {
  131. /**
  132. * @var StopwatchEvent[]
  133. */
  134. private $events = array();
  135. /**
  136. * @var null|float
  137. */
  138. private $origin;
  139. /**
  140. * @var string
  141. */
  142. private $id;
  143. /**
  144. * @var Section[]
  145. */
  146. private $children = array();
  147. /**
  148. * Constructor.
  149. *
  150. * @param float|null $origin Set the origin of the events in this section, use null to set their origin to their start time
  151. */
  152. public function __construct($origin = null)
  153. {
  154. $this->origin = is_numeric($origin) ? $origin : null;
  155. }
  156. /**
  157. * Returns the child section.
  158. *
  159. * @param string $id The child section identifier
  160. *
  161. * @return Section|null The child section or null when none found
  162. */
  163. public function get($id)
  164. {
  165. foreach ($this->children as $child) {
  166. if ($id === $child->getId()) {
  167. return $child;
  168. }
  169. }
  170. return null;
  171. }
  172. /**
  173. * Creates or re-opens a child section.
  174. *
  175. * @param string|null $id null to create a new section, the identifier to re-open an existing one.
  176. *
  177. * @return Section A child section
  178. */
  179. public function open($id)
  180. {
  181. if (null === $session = $this->get($id)) {
  182. $session = $this->children[] = new self(microtime(true) * 1000);
  183. }
  184. return $session;
  185. }
  186. /**
  187. * @return string The identifier of the section
  188. */
  189. public function getId()
  190. {
  191. return $this->id;
  192. }
  193. /**
  194. * Sets the session identifier.
  195. *
  196. * @param string $id The session identifier
  197. *
  198. * @return Section The current section
  199. */
  200. public function setId($id)
  201. {
  202. $this->id = $id;
  203. return $this;
  204. }
  205. /**
  206. * Starts an event.
  207. *
  208. * @param string $name The event name
  209. * @param string $category The event category
  210. *
  211. * @return StopwatchEvent The event
  212. */
  213. public function startEvent($name, $category)
  214. {
  215. if (!isset($this->events[$name])) {
  216. $this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category);
  217. }
  218. return $this->events[$name]->start();
  219. }
  220. /**
  221. * Checks if the event was started
  222. *
  223. * @param string $name The event name
  224. *
  225. * @return bool
  226. */
  227. public function isEventStarted($name)
  228. {
  229. return isset($this->events[$name]) && $this->events[$name]->isStarted();
  230. }
  231. /**
  232. * Stops an event.
  233. *
  234. * @param string $name The event name
  235. *
  236. * @return StopwatchEvent The event
  237. *
  238. * @throws \LogicException When the event has not been started
  239. */
  240. public function stopEvent($name)
  241. {
  242. if (!isset($this->events[$name])) {
  243. throw new \LogicException(sprintf('Event "%s" is not started.', $name));
  244. }
  245. return $this->events[$name]->stop();
  246. }
  247. /**
  248. * Stops then restarts an event.
  249. *
  250. * @param string $name The event name
  251. *
  252. * @return StopwatchEvent The event
  253. *
  254. * @throws \LogicException When the event has not been started
  255. */
  256. public function lap($name)
  257. {
  258. return $this->stopEvent($name)->start();
  259. }
  260. /**
  261. * Returns the events from this section.
  262. *
  263. * @return StopwatchEvent[] An array of StopwatchEvent instances
  264. */
  265. public function getEvents()
  266. {
  267. return $this->events;
  268. }
  269. }