3 namespace Drupal\webprofiler {
8 class Stopwatch extends \Symfony\Component\Stopwatch\Stopwatch {
14 namespace Symfony\Component\Stopwatch {
28 private $activeSections;
33 public function __construct() {
34 $this->sections = $this->activeSections = ['__root__' => new Section('__root__')];
38 * Creates a new section or re-opens an existing section.
40 * @param string|null $id The id of the session to re-open, null to create a new one
42 * @throws \LogicException When the section to re-open is not reachable
44 public function openSection($id = NULL) {
45 $current = end($this->activeSections);
47 if (NULL !== $id && NULL === $current->get($id)) {
48 throw new \LogicException(sprintf('The section "%s" has been started at an other level and can not be opened.', $id));
51 $this->start('__section__.child', 'section');
52 $this->activeSections[] = $current->open($id);
53 $this->start('__section__');
57 * Stops the last started section.
59 * The id parameter is used to retrieve the events from this section.
61 * @see getSectionEvents
63 * @param string $id The identifier of the section
65 * @throws \LogicException When there's no started section to be stopped
67 public function stopSection($id) {
68 $this->stop('__section__');
70 if (1 == count($this->activeSections)) {
71 throw new \LogicException('There is no started section to stop.');
74 $this->sections[$id] = array_pop($this->activeSections)->setId($id);
75 $this->stop('__section__.child');
81 * @param string $name The event name
82 * @param string $category The event category
84 * @return StopwatchEvent A StopwatchEvent instance
86 public function start($name, $category = NULL) {
87 return end($this->activeSections)->startEvent($name, $category);
91 * Checks if the event was started
93 * @param string $name The event name
97 public function isStarted($name) {
98 return end($this->activeSections)->isEventStarted($name);
104 * @param string $name The event name
106 * @return StopwatchEvent A StopwatchEvent instance
108 public function stop($name) {
109 return end($this->activeSections)->stopEvent($name);
113 * Stops then restarts an event.
115 * @param string $name The event name
117 * @return StopwatchEvent A StopwatchEvent instance
119 public function lap($name) {
120 return end($this->activeSections)->stopEvent($name)->start();
124 * Gets all events for a given section.
126 * @param string $id A section identifier
128 * @return StopwatchEvent[] An array of StopwatchEvent instances
130 public function getSectionEvents($id) {
131 return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : [];
137 * @internal This class is for internal usage only
139 * @author Fabien Potencier <fabien@symfony.com>
143 * @var StopwatchEvent[]
145 private $events = [];
160 private $children = [];
165 * @param float|null $origin Set the origin of the events in this section, use null to set their origin to their start time
167 public function __construct($origin = NULL) {
168 $this->origin = is_numeric($origin) ? $origin : NULL;
172 * Returns the child section.
174 * @param string $id The child section identifier
176 * @return Section|null The child section or null when none found
178 public function get($id) {
179 foreach ($this->children as $child) {
180 if ($id === $child->getId()) {
189 * Creates or re-opens a child section.
191 * @param string|null $id null to create a new section, the identifier to re-open an existing one.
193 * @return Section A child section
195 public function open($id) {
196 if (NULL === $session = $this->get($id)) {
197 $session = $this->children[] = new self(microtime(TRUE) * 1000);
204 * @return string The identifier of the section
206 public function getId() {
211 * Sets the session identifier.
213 * @param string $id The session identifier
215 * @return Section The current section
217 public function setId($id) {
226 * @param string $name The event name
227 * @param string $category The event category
229 * @return StopwatchEvent The event
231 public function startEvent($name, $category) {
232 if (!isset($this->events[$name])) {
233 $this->events[$name] = new StopwatchEvent($this->origin ?: microtime(TRUE) * 1000, $category);
236 return $this->events[$name]->start();
240 * Checks if the event was started
242 * @param string $name The event name
246 public function isEventStarted($name) {
247 return isset($this->events[$name]) && $this->events[$name]->isStarted();
253 * @param string $name The event name
255 * @return StopwatchEvent The event
257 * @throws \LogicException When the event has not been started
259 public function stopEvent($name) {
260 if (!isset($this->events[$name])) {
261 throw new \LogicException(sprintf('Event "%s" is not started.', $name));
264 return $this->events[$name]->stop();
268 * Stops then restarts an event.
270 * @param string $name The event name
272 * @return StopwatchEvent The event
274 * @throws \LogicException When the event has not been started
276 public function lap($name) {
277 return $this->stopEvent($name)->start();
281 * Returns the events from this section.
283 * @return StopwatchEvent[] An array of StopwatchEvent instances
285 public function getEvents() {
286 return $this->events;
291 * Class StopwatchEvent
293 class StopwatchEvent {
295 * @var StopwatchPeriod[]
297 private $periods = [];
312 private $started = [];
317 * @param float $origin The origin time in milliseconds
318 * @param string|null $category The event category or null to use the default
320 * @throws \InvalidArgumentException When the raw time is not valid
322 public function __construct($origin, $category = NULL) {
323 $this->origin = $this->formatTime($origin);
324 $this->category = is_string($category) ? $category : 'default';
330 * @return string The category
332 public function getCategory() {
333 return $this->category;
339 * @return float The origin in milliseconds
341 public function getOrigin() {
342 return $this->origin;
346 * Starts a new event period.
348 * @return StopwatchEvent The event
350 public function start() {
351 $this->started[] = $this->getNow();
357 * Stops the last started event period.
359 * @throws \LogicException When start wasn't called before stopping
361 * @return StopwatchEvent The event
363 * @throws \LogicException When stop() is called without a matching call to start()
365 public function stop() {
366 if (!count($this->started)) {
367 throw new \LogicException('stop() called but start() has not been called before.');
370 $this->periods[] = new StopwatchPeriod(array_pop($this->started), $this->getNow());
376 * Checks if the event was started
380 public function isStarted() {
381 return !empty($this->started);
385 * Stops the current period and then starts a new one.
387 * @return StopwatchEvent The event
389 public function lap() {
390 return $this->stop()->start();
394 * Stops all non already stopped periods.
396 public function ensureStopped() {
397 while (count($this->started)) {
403 * Gets all event periods.
405 * @return StopwatchPeriod[] An array of StopwatchPeriod instances
407 public function getPeriods() {
408 return $this->periods;
412 * Gets the relative time of the start of the first period.
414 * @return integer The time (in milliseconds)
416 public function getStartTime() {
417 return isset($this->periods[0]) ? $this->periods[0]->getStartTime() : 0;
421 * Gets the relative time of the end of the last period.
423 * @return integer The time (in milliseconds)
425 public function getEndTime() {
426 return ($count = count($this->periods)) ? $this->periods[$count - 1]->getEndTime() : 0;
430 * Gets the duration of the events (including all periods).
432 * @return integer The duration (in milliseconds)
434 public function getDuration() {
436 foreach ($this->periods as $period) {
437 $total += $period->getDuration();
444 * Gets the max memory usage of all periods.
446 * @return integer The memory usage (in bytes)
448 public function getMemory() {
450 foreach ($this->periods as $period) {
451 if ($period->getMemory() > $memory) {
452 $memory = $period->getMemory();
460 * Return the current time relative to origin.
462 * @return float Time in ms
464 protected function getNow() {
465 return $this->formatTime(microtime(TRUE) * 1000 - $this->origin);
471 * @param integer|float $time A raw time
473 * @return float The formatted time
475 * @throws \InvalidArgumentException When the raw time is not valid
477 private function formatTime($time) {
478 if (!is_numeric($time)) {
479 throw new \InvalidArgumentException('The time must be a numerical value');
482 return round($time, 1);
487 * Class StopwatchPeriod
489 class StopwatchPeriod {
497 * @param integer $start The relative time of the start of the period (in milliseconds)
498 * @param integer $end The relative time of the end of the period (in milliseconds)
500 public function __construct($start, $end) {
501 $this->start = (integer) $start;
502 $this->end = (integer) $end;
503 $this->memory = memory_get_usage(TRUE);
507 * Gets the relative time of the start of the period.
509 * @return integer The time (in milliseconds)
511 public function getStartTime() {
516 * Gets the relative time of the end of the period.
518 * @return integer The time (in milliseconds)
520 public function getEndTime() {
525 * Gets the time spent in this period.
527 * @return integer The period duration (in milliseconds)
529 public function getDuration() {
530 return $this->end - $this->start;
534 * Gets the memory usage.
536 * @return integer The memory usage (in bytes)
538 public function getMemory() {
539 return $this->memory;