StopwatchEvent.php 5.2 KB

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