Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Stopwatch.php
1 <?php
2
3 namespace Drupal\webprofiler {
4
5   /**
6    * Class Stopwatch
7    */
8   class Stopwatch extends \Symfony\Component\Stopwatch\Stopwatch {
9
10   }
11
12 }
13
14 namespace Symfony\Component\Stopwatch {
15
16   /**
17    * Class Stopwatch
18    */
19   class Stopwatch {
20     /**
21      * @var Section[]
22      */
23     private $sections;
24
25     /**
26      * @var array
27      */
28     private $activeSections;
29
30     /**
31      *
32      */
33     public function __construct() {
34       $this->sections = $this->activeSections = ['__root__' => new Section('__root__')];
35     }
36
37     /**
38      * Creates a new section or re-opens an existing section.
39      *
40      * @param string|null $id The id of the session to re-open, null to create a new one
41      *
42      * @throws \LogicException When the section to re-open is not reachable
43      */
44     public function openSection($id = NULL) {
45       $current = end($this->activeSections);
46
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));
49       }
50
51       $this->start('__section__.child', 'section');
52       $this->activeSections[] = $current->open($id);
53       $this->start('__section__');
54     }
55
56     /**
57      * Stops the last started section.
58      *
59      * The id parameter is used to retrieve the events from this section.
60      *
61      * @see getSectionEvents
62      *
63      * @param string $id The identifier of the section
64      *
65      * @throws \LogicException When there's no started section to be stopped
66      */
67     public function stopSection($id) {
68       $this->stop('__section__');
69
70       if (1 == count($this->activeSections)) {
71         throw new \LogicException('There is no started section to stop.');
72       }
73
74       $this->sections[$id] = array_pop($this->activeSections)->setId($id);
75       $this->stop('__section__.child');
76     }
77
78     /**
79      * Starts an event.
80      *
81      * @param string $name The event name
82      * @param string $category The event category
83      *
84      * @return StopwatchEvent A StopwatchEvent instance
85      */
86     public function start($name, $category = NULL) {
87       return end($this->activeSections)->startEvent($name, $category);
88     }
89
90     /**
91      * Checks if the event was started
92      *
93      * @param string $name The event name
94      *
95      * @return bool
96      */
97     public function isStarted($name) {
98       return end($this->activeSections)->isEventStarted($name);
99     }
100
101     /**
102      * Stops an event.
103      *
104      * @param string $name The event name
105      *
106      * @return StopwatchEvent A StopwatchEvent instance
107      */
108     public function stop($name) {
109       return end($this->activeSections)->stopEvent($name);
110     }
111
112     /**
113      * Stops then restarts an event.
114      *
115      * @param string $name The event name
116      *
117      * @return StopwatchEvent A StopwatchEvent instance
118      */
119     public function lap($name) {
120       return end($this->activeSections)->stopEvent($name)->start();
121     }
122
123     /**
124      * Gets all events for a given section.
125      *
126      * @param string $id A section identifier
127      *
128      * @return StopwatchEvent[] An array of StopwatchEvent instances
129      */
130     public function getSectionEvents($id) {
131       return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : [];
132     }
133   }
134
135
136   /**
137    * @internal This class is for internal usage only
138    *
139    * @author Fabien Potencier <fabien@symfony.com>
140    */
141   class Section {
142     /**
143      * @var StopwatchEvent[]
144      */
145     private $events = [];
146
147     /**
148      * @var null|float
149      */
150     private $origin;
151
152     /**
153      * @var string
154      */
155     private $id;
156
157     /**
158      * @var Section[]
159      */
160     private $children = [];
161
162     /**
163      * Constructor.
164      *
165      * @param float|null $origin Set the origin of the events in this section, use null to set their origin to their start time
166      */
167     public function __construct($origin = NULL) {
168       $this->origin = is_numeric($origin) ? $origin : NULL;
169     }
170
171     /**
172      * Returns the child section.
173      *
174      * @param string $id The child section identifier
175      *
176      * @return Section|null The child section or null when none found
177      */
178     public function get($id) {
179       foreach ($this->children as $child) {
180         if ($id === $child->getId()) {
181           return $child;
182         }
183       }
184
185       return NULL;
186     }
187
188     /**
189      * Creates or re-opens a child section.
190      *
191      * @param string|null $id null to create a new section, the identifier to re-open an existing one.
192      *
193      * @return Section A child section
194      */
195     public function open($id) {
196       if (NULL === $session = $this->get($id)) {
197         $session = $this->children[] = new self(microtime(TRUE) * 1000);
198       }
199
200       return $session;
201     }
202
203     /**
204      * @return string The identifier of the section
205      */
206     public function getId() {
207       return $this->id;
208     }
209
210     /**
211      * Sets the session identifier.
212      *
213      * @param string $id The session identifier
214      *
215      * @return Section The current section
216      */
217     public function setId($id) {
218       $this->id = $id;
219
220       return $this;
221     }
222
223     /**
224      * Starts an event.
225      *
226      * @param string $name The event name
227      * @param string $category The event category
228      *
229      * @return StopwatchEvent The event
230      */
231     public function startEvent($name, $category) {
232       if (!isset($this->events[$name])) {
233         $this->events[$name] = new StopwatchEvent($this->origin ?: microtime(TRUE) * 1000, $category);
234       }
235
236       return $this->events[$name]->start();
237     }
238
239     /**
240      * Checks if the event was started
241      *
242      * @param string $name The event name
243      *
244      * @return bool
245      */
246     public function isEventStarted($name) {
247       return isset($this->events[$name]) && $this->events[$name]->isStarted();
248     }
249
250     /**
251      * Stops an event.
252      *
253      * @param string $name The event name
254      *
255      * @return StopwatchEvent The event
256      *
257      * @throws \LogicException When the event has not been started
258      */
259     public function stopEvent($name) {
260       if (!isset($this->events[$name])) {
261         throw new \LogicException(sprintf('Event "%s" is not started.', $name));
262       }
263
264       return $this->events[$name]->stop();
265     }
266
267     /**
268      * Stops then restarts an event.
269      *
270      * @param string $name The event name
271      *
272      * @return StopwatchEvent The event
273      *
274      * @throws \LogicException When the event has not been started
275      */
276     public function lap($name) {
277       return $this->stopEvent($name)->start();
278     }
279
280     /**
281      * Returns the events from this section.
282      *
283      * @return StopwatchEvent[] An array of StopwatchEvent instances
284      */
285     public function getEvents() {
286       return $this->events;
287     }
288   }
289
290   /**
291    * Class StopwatchEvent
292    */
293   class StopwatchEvent {
294     /**
295      * @var StopwatchPeriod[]
296      */
297     private $periods = [];
298
299     /**
300      * @var float
301      */
302     private $origin;
303
304     /**
305      * @var string
306      */
307     private $category;
308
309     /**
310      * @var float[]
311      */
312     private $started = [];
313
314     /**
315      * Constructor.
316      *
317      * @param float $origin The origin time in milliseconds
318      * @param string|null $category The event category or null to use the default
319      *
320      * @throws \InvalidArgumentException When the raw time is not valid
321      */
322     public function __construct($origin, $category = NULL) {
323       $this->origin = $this->formatTime($origin);
324       $this->category = is_string($category) ? $category : 'default';
325     }
326
327     /**
328      * Gets the category.
329      *
330      * @return string The category
331      */
332     public function getCategory() {
333       return $this->category;
334     }
335
336     /**
337      * Gets the origin.
338      *
339      * @return float The origin in milliseconds
340      */
341     public function getOrigin() {
342       return $this->origin;
343     }
344
345     /**
346      * Starts a new event period.
347      *
348      * @return StopwatchEvent The event
349      */
350     public function start() {
351       $this->started[] = $this->getNow();
352
353       return $this;
354     }
355
356     /**
357      * Stops the last started event period.
358      *
359      * @throws \LogicException When start wasn't called before stopping
360      *
361      * @return StopwatchEvent The event
362      *
363      * @throws \LogicException When stop() is called without a matching call to start()
364      */
365     public function stop() {
366       if (!count($this->started)) {
367         throw new \LogicException('stop() called but start() has not been called before.');
368       }
369
370       $this->periods[] = new StopwatchPeriod(array_pop($this->started), $this->getNow());
371
372       return $this;
373     }
374
375     /**
376      * Checks if the event was started
377      *
378      * @return bool
379      */
380     public function isStarted() {
381       return !empty($this->started);
382     }
383
384     /**
385      * Stops the current period and then starts a new one.
386      *
387      * @return StopwatchEvent The event
388      */
389     public function lap() {
390       return $this->stop()->start();
391     }
392
393     /**
394      * Stops all non already stopped periods.
395      */
396     public function ensureStopped() {
397       while (count($this->started)) {
398         $this->stop();
399       }
400     }
401
402     /**
403      * Gets all event periods.
404      *
405      * @return StopwatchPeriod[] An array of StopwatchPeriod instances
406      */
407     public function getPeriods() {
408       return $this->periods;
409     }
410
411     /**
412      * Gets the relative time of the start of the first period.
413      *
414      * @return integer The time (in milliseconds)
415      */
416     public function getStartTime() {
417       return isset($this->periods[0]) ? $this->periods[0]->getStartTime() : 0;
418     }
419
420     /**
421      * Gets the relative time of the end of the last period.
422      *
423      * @return integer The time (in milliseconds)
424      */
425     public function getEndTime() {
426       return ($count = count($this->periods)) ? $this->periods[$count - 1]->getEndTime() : 0;
427     }
428
429     /**
430      * Gets the duration of the events (including all periods).
431      *
432      * @return integer The duration (in milliseconds)
433      */
434     public function getDuration() {
435       $total = 0;
436       foreach ($this->periods as $period) {
437         $total += $period->getDuration();
438       }
439
440       return $total;
441     }
442
443     /**
444      * Gets the max memory usage of all periods.
445      *
446      * @return integer The memory usage (in bytes)
447      */
448     public function getMemory() {
449       $memory = 0;
450       foreach ($this->periods as $period) {
451         if ($period->getMemory() > $memory) {
452           $memory = $period->getMemory();
453         }
454       }
455
456       return $memory;
457     }
458
459     /**
460      * Return the current time relative to origin.
461      *
462      * @return float Time in ms
463      */
464     protected function getNow() {
465       return $this->formatTime(microtime(TRUE) * 1000 - $this->origin);
466     }
467
468     /**
469      * Formats a time.
470      *
471      * @param integer|float $time A raw time
472      *
473      * @return float The formatted time
474      *
475      * @throws \InvalidArgumentException When the raw time is not valid
476      */
477     private function formatTime($time) {
478       if (!is_numeric($time)) {
479         throw new \InvalidArgumentException('The time must be a numerical value');
480       }
481
482       return round($time, 1);
483     }
484   }
485
486   /**
487    * Class StopwatchPeriod
488    */
489   class StopwatchPeriod {
490     private $start;
491     private $end;
492     private $memory;
493
494     /**
495      * Constructor.
496      *
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)
499      */
500     public function __construct($start, $end) {
501       $this->start = (integer) $start;
502       $this->end = (integer) $end;
503       $this->memory = memory_get_usage(TRUE);
504     }
505
506     /**
507      * Gets the relative time of the start of the period.
508      *
509      * @return integer The time (in milliseconds)
510      */
511     public function getStartTime() {
512       return $this->start;
513     }
514
515     /**
516      * Gets the relative time of the end of the period.
517      *
518      * @return integer The time (in milliseconds)
519      */
520     public function getEndTime() {
521       return $this->end;
522     }
523
524     /**
525      * Gets the time spent in this period.
526      *
527      * @return integer The period duration (in milliseconds)
528      */
529     public function getDuration() {
530       return $this->end - $this->start;
531     }
532
533     /**
534      * Gets the memory usage.
535      *
536      * @return integer The memory usage (in bytes)
537      */
538     public function getMemory() {
539       return $this->memory;
540     }
541   }
542
543 }