StopwatchEvent.php 5.1 KB

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