StopwatchEvent.php 5.6 KB

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