StopwatchEvent.php 5.3 KB

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