Stopwatch.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. use Symfony\Contracts\Service\ResetInterface;
  12. // Help opcache.preload discover always-needed symbols
  13. class_exists(Section::class);
  14. /**
  15. * Stopwatch provides a way to profile code.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class Stopwatch implements ResetInterface
  20. {
  21. /**
  22. * @var bool
  23. */
  24. private $morePrecision;
  25. /**
  26. * @var Section[]
  27. */
  28. private $sections;
  29. /**
  30. * @var Section[]
  31. */
  32. private $activeSections;
  33. /**
  34. * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision
  35. */
  36. public function __construct(bool $morePrecision = false)
  37. {
  38. $this->morePrecision = $morePrecision;
  39. $this->reset();
  40. }
  41. /**
  42. * @return Section[]
  43. */
  44. public function getSections()
  45. {
  46. return $this->sections;
  47. }
  48. /**
  49. * Creates a new section or re-opens an existing section.
  50. *
  51. * @param string|null $id The id of the session to re-open, null to create a new one
  52. *
  53. * @throws \LogicException When the section to re-open is not reachable
  54. */
  55. public function openSection($id = null)
  56. {
  57. $current = end($this->activeSections);
  58. if (null !== $id && null === $current->get($id)) {
  59. throw new \LogicException(sprintf('The section "%s" has been started at an other level and can not be opened.', $id));
  60. }
  61. $this->start('__section__.child', 'section');
  62. $this->activeSections[] = $current->open($id);
  63. $this->start('__section__');
  64. }
  65. /**
  66. * Stops the last started section.
  67. *
  68. * The id parameter is used to retrieve the events from this section.
  69. *
  70. * @see getSectionEvents()
  71. *
  72. * @param string $id The identifier of the section
  73. *
  74. * @throws \LogicException When there's no started section to be stopped
  75. */
  76. public function stopSection($id)
  77. {
  78. $this->stop('__section__');
  79. if (1 == \count($this->activeSections)) {
  80. throw new \LogicException('There is no started section to stop.');
  81. }
  82. $this->sections[$id] = array_pop($this->activeSections)->setId($id);
  83. $this->stop('__section__.child');
  84. }
  85. /**
  86. * Starts an event.
  87. *
  88. * @param string $name The event name
  89. * @param string|null $category The event category
  90. *
  91. * @return StopwatchEvent
  92. */
  93. public function start($name, $category = null)
  94. {
  95. return end($this->activeSections)->startEvent($name, $category);
  96. }
  97. /**
  98. * Checks if the event was started.
  99. *
  100. * @param string $name The event name
  101. *
  102. * @return bool
  103. */
  104. public function isStarted($name)
  105. {
  106. return end($this->activeSections)->isEventStarted($name);
  107. }
  108. /**
  109. * Stops an event.
  110. *
  111. * @param string $name The event name
  112. *
  113. * @return StopwatchEvent
  114. */
  115. public function stop($name)
  116. {
  117. return end($this->activeSections)->stopEvent($name);
  118. }
  119. /**
  120. * Stops then restarts an event.
  121. *
  122. * @param string $name The event name
  123. *
  124. * @return StopwatchEvent
  125. */
  126. public function lap($name)
  127. {
  128. return end($this->activeSections)->stopEvent($name)->start();
  129. }
  130. /**
  131. * Returns a specific event by name.
  132. *
  133. * @param string $name The event name
  134. *
  135. * @return StopwatchEvent
  136. */
  137. public function getEvent($name)
  138. {
  139. return end($this->activeSections)->getEvent($name);
  140. }
  141. /**
  142. * Gets all events for a given section.
  143. *
  144. * @param string $id A section identifier
  145. *
  146. * @return StopwatchEvent[]
  147. */
  148. public function getSectionEvents($id)
  149. {
  150. return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : [];
  151. }
  152. /**
  153. * Resets the stopwatch to its original state.
  154. */
  155. public function reset()
  156. {
  157. $this->sections = $this->activeSections = ['__root__' => new Section(null, $this->morePrecision)];
  158. }
  159. }