Stopwatch.php 3.9 KB

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