Stopwatch.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 Section[]
  24. */
  25. private $activeSections;
  26. public function __construct()
  27. {
  28. $this->sections = $this->activeSections = array('__root__' => new Section(null));
  29. }
  30. /**
  31. * @return Section[]
  32. */
  33. public function getSections()
  34. {
  35. return $this->sections;
  36. }
  37. /**
  38. * Creates a new section or re-opens an existing section.
  39. *
  40. * @param string|null $id The id of the session to re-open, null to create a new one
  41. *
  42. * @throws \LogicException When the section to re-open is not reachable
  43. */
  44. public function openSection($id = null)
  45. {
  46. $current = end($this->activeSections);
  47. if (null !== $id && null === $current->get($id)) {
  48. throw new \LogicException(sprintf('The section "%s" has been started at an other level and can not be opened.', $id));
  49. }
  50. $this->start('__section__.child', 'section');
  51. $this->activeSections[] = $current->open($id);
  52. $this->start('__section__');
  53. }
  54. /**
  55. * Stops the last started section.
  56. *
  57. * The id parameter is used to retrieve the events from this section.
  58. *
  59. * @see getSectionEvents()
  60. *
  61. * @param string $id The identifier of the section
  62. *
  63. * @throws \LogicException When there's no started section to be stopped
  64. */
  65. public function stopSection($id)
  66. {
  67. $this->stop('__section__');
  68. if (1 == \count($this->activeSections)) {
  69. throw new \LogicException('There is no started section to stop.');
  70. }
  71. $this->sections[$id] = array_pop($this->activeSections)->setId($id);
  72. $this->stop('__section__.child');
  73. }
  74. /**
  75. * Starts an event.
  76. *
  77. * @param string $name The event name
  78. * @param string $category The event category
  79. *
  80. * @return StopwatchEvent
  81. */
  82. public function start($name, $category = null)
  83. {
  84. return end($this->activeSections)->startEvent($name, $category);
  85. }
  86. /**
  87. * Checks if the event was started.
  88. *
  89. * @param string $name The event name
  90. *
  91. * @return bool
  92. */
  93. public function isStarted($name)
  94. {
  95. return end($this->activeSections)->isEventStarted($name);
  96. }
  97. /**
  98. * Stops an event.
  99. *
  100. * @param string $name The event name
  101. *
  102. * @return StopwatchEvent
  103. */
  104. public function stop($name)
  105. {
  106. return end($this->activeSections)->stopEvent($name);
  107. }
  108. /**
  109. * Stops then restarts an event.
  110. *
  111. * @param string $name The event name
  112. *
  113. * @return StopwatchEvent
  114. */
  115. public function lap($name)
  116. {
  117. return end($this->activeSections)->stopEvent($name)->start();
  118. }
  119. /**
  120. * Returns a specific event by name.
  121. *
  122. * @param string $name The event name
  123. *
  124. * @return StopwatchEvent
  125. */
  126. public function getEvent($name)
  127. {
  128. return end($this->activeSections)->getEvent($name);
  129. }
  130. /**
  131. * Gets all events for a given section.
  132. *
  133. * @param string $id A section identifier
  134. *
  135. * @return StopwatchEvent[]
  136. */
  137. public function getSectionEvents($id)
  138. {
  139. return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : array();
  140. }
  141. }