StopwatchEvent.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * Represents an Event managed by Stopwatch.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class StopwatchEvent
  17. {
  18. /**
  19. * @var StopwatchPeriod[]
  20. */
  21. private array $periods = [];
  22. private float $origin;
  23. private string $category;
  24. private bool $morePrecision;
  25. /**
  26. * @var float[]
  27. */
  28. private array $started = [];
  29. private string $name;
  30. /**
  31. * @param float $origin The origin time in milliseconds
  32. * @param string|null $category The event category or null to use the default
  33. * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision
  34. * @param string|null $name The event name or null to define the name as default
  35. */
  36. public function __construct(float $origin, ?string $category = null, bool $morePrecision = false, ?string $name = null)
  37. {
  38. $this->origin = $this->formatTime($origin);
  39. $this->category = \is_string($category) ? $category : 'default';
  40. $this->morePrecision = $morePrecision;
  41. $this->name = $name ?? 'default';
  42. }
  43. /**
  44. * Gets the category.
  45. */
  46. public function getCategory(): string
  47. {
  48. return $this->category;
  49. }
  50. /**
  51. * Gets the origin in milliseconds.
  52. */
  53. public function getOrigin(): float
  54. {
  55. return $this->origin;
  56. }
  57. /**
  58. * Starts a new event period.
  59. *
  60. * @return $this
  61. */
  62. public function start(): static
  63. {
  64. $this->started[] = $this->getNow();
  65. return $this;
  66. }
  67. /**
  68. * Stops the last started event period.
  69. *
  70. * @return $this
  71. *
  72. * @throws \LogicException When stop() is called without a matching call to start()
  73. */
  74. public function stop(): static
  75. {
  76. if (!\count($this->started)) {
  77. throw new \LogicException('stop() called but start() has not been called before.');
  78. }
  79. $this->periods[] = new StopwatchPeriod(array_pop($this->started), $this->getNow(), $this->morePrecision);
  80. return $this;
  81. }
  82. /**
  83. * Checks if the event was started.
  84. */
  85. public function isStarted(): bool
  86. {
  87. return !empty($this->started);
  88. }
  89. /**
  90. * Stops the current period and then starts a new one.
  91. *
  92. * @return $this
  93. */
  94. public function lap(): static
  95. {
  96. return $this->stop()->start();
  97. }
  98. /**
  99. * Stops all non already stopped periods.
  100. *
  101. * @return void
  102. */
  103. public function ensureStopped()
  104. {
  105. while (\count($this->started)) {
  106. $this->stop();
  107. }
  108. }
  109. /**
  110. * Gets all event periods.
  111. *
  112. * @return StopwatchPeriod[]
  113. */
  114. public function getPeriods(): array
  115. {
  116. return $this->periods;
  117. }
  118. /**
  119. * Gets the relative time of the start of the first period in milliseconds.
  120. */
  121. public function getStartTime(): int|float
  122. {
  123. if (isset($this->periods[0])) {
  124. return $this->periods[0]->getStartTime();
  125. }
  126. if ($this->started) {
  127. return $this->started[0];
  128. }
  129. return 0;
  130. }
  131. /**
  132. * Gets the relative time of the end of the last period in milliseconds.
  133. */
  134. public function getEndTime(): int|float
  135. {
  136. $count = \count($this->periods);
  137. return $count ? $this->periods[$count - 1]->getEndTime() : 0;
  138. }
  139. /**
  140. * Gets the duration of the events in milliseconds (including all periods).
  141. */
  142. public function getDuration(): int|float
  143. {
  144. $periods = $this->periods;
  145. $left = \count($this->started);
  146. for ($i = $left - 1; $i >= 0; --$i) {
  147. $periods[] = new StopwatchPeriod($this->started[$i], $this->getNow(), $this->morePrecision);
  148. }
  149. $total = 0;
  150. foreach ($periods as $period) {
  151. $total += $period->getDuration();
  152. }
  153. return $total;
  154. }
  155. /**
  156. * Gets the max memory usage of all periods in bytes.
  157. */
  158. public function getMemory(): int
  159. {
  160. $memory = 0;
  161. foreach ($this->periods as $period) {
  162. if ($period->getMemory() > $memory) {
  163. $memory = $period->getMemory();
  164. }
  165. }
  166. return $memory;
  167. }
  168. /**
  169. * Return the current time relative to origin in milliseconds.
  170. */
  171. protected function getNow(): float
  172. {
  173. return $this->formatTime(microtime(true) * 1000 - $this->origin);
  174. }
  175. /**
  176. * Formats a time.
  177. */
  178. private function formatTime(float $time): float
  179. {
  180. return round($time, 1);
  181. }
  182. /**
  183. * Gets the event name.
  184. */
  185. public function getName(): string
  186. {
  187. return $this->name;
  188. }
  189. public function __toString(): string
  190. {
  191. return sprintf('%s/%s: %.2F MiB - %d ms', $this->getCategory(), $this->getName(), $this->getMemory() / 1024 / 1024, $this->getDuration());
  192. }
  193. }