/vendor/symfony/symfony/src/Symfony/Component/Stopwatch/StopwatchEvent.php

https://github.com/nattaphat/hgis · PHP · 218 lines · 88 code · 26 blank · 104 comment · 4 complexity · a4a14f91a9feae9f35f429f58378675c MD5 · raw file

  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. * Stops the current period and then starts a new one.
  96. *
  97. * @return StopwatchEvent The event
  98. */
  99. public function lap()
  100. {
  101. return $this->stop()->start();
  102. }
  103. /**
  104. * Stops all non already stopped periods.
  105. */
  106. public function ensureStopped()
  107. {
  108. while (count($this->started)) {
  109. $this->stop();
  110. }
  111. }
  112. /**
  113. * Gets all event periods.
  114. *
  115. * @return StopwatchPeriod[] An array of StopwatchPeriod instances
  116. */
  117. public function getPeriods()
  118. {
  119. return $this->periods;
  120. }
  121. /**
  122. * Gets the relative time of the start of the first period.
  123. *
  124. * @return integer The time (in milliseconds)
  125. */
  126. public function getStartTime()
  127. {
  128. return isset($this->periods[0]) ? $this->periods[0]->getStartTime() : 0;
  129. }
  130. /**
  131. * Gets the relative time of the end of the last period.
  132. *
  133. * @return integer The time (in milliseconds)
  134. */
  135. public function getEndTime()
  136. {
  137. return ($count = count($this->periods)) ? $this->periods[$count - 1]->getEndTime() : 0;
  138. }
  139. /**
  140. * Gets the duration of the events (including all periods).
  141. *
  142. * @return integer The duration (in milliseconds)
  143. */
  144. public function getDuration()
  145. {
  146. $total = 0;
  147. foreach ($this->periods as $period) {
  148. $total += $period->getDuration();
  149. }
  150. return $this->formatTime($total);
  151. }
  152. /**
  153. * Gets the max memory usage of all periods.
  154. *
  155. * @return integer The memory usage (in bytes)
  156. */
  157. public function getMemory()
  158. {
  159. $memory = 0;
  160. foreach ($this->periods as $period) {
  161. if ($period->getMemory() > $memory) {
  162. $memory = $period->getMemory();
  163. }
  164. }
  165. return $memory;
  166. }
  167. /**
  168. * Return the current time relative to origin.
  169. *
  170. * @return float Time in ms
  171. */
  172. protected function getNow()
  173. {
  174. return $this->formatTime(microtime(true) * 1000 - $this->origin);
  175. }
  176. /**
  177. * Formats a time.
  178. *
  179. * @param integer|float $time A raw time
  180. *
  181. * @return float The formatted time
  182. *
  183. * @throws \InvalidArgumentException When the raw time is not valid
  184. */
  185. private function formatTime($time)
  186. {
  187. if (!is_numeric($time)) {
  188. throw new \InvalidArgumentException('The time must be a numerical value');
  189. }
  190. return round($time, 1);
  191. }
  192. }