StopwatchEvent.php 4.8 KB

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