Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Component/Utility/Timer.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Component\Utility; | |
4 | |
5 /** | |
6 * Provides helpers to use timers throughout a request. | |
7 * | |
8 * @ingroup utility | |
9 */ | |
10 class Timer { | |
11 | |
12 static protected $timers = []; | |
13 | |
14 /** | |
15 * Starts the timer with the specified name. | |
16 * | |
17 * If you start and stop the same timer multiple times, the measured intervals | |
18 * will be accumulated. | |
19 * | |
20 * @param $name | |
21 * The name of the timer. | |
22 */ | |
23 public static function start($name) { | |
24 static::$timers[$name]['start'] = microtime(TRUE); | |
25 static::$timers[$name]['count'] = isset(static::$timers[$name]['count']) ? ++static::$timers[$name]['count'] : 1; | |
26 } | |
27 | |
28 /** | |
29 * Reads the current timer value without stopping the timer. | |
30 * | |
31 * @param string $name | |
32 * The name of the timer. | |
33 * | |
34 * @return int | |
35 * The current timer value in ms. | |
36 */ | |
37 public static function read($name) { | |
38 if (isset(static::$timers[$name]['start'])) { | |
39 $stop = microtime(TRUE); | |
40 $diff = round(($stop - static::$timers[$name]['start']) * 1000, 2); | |
41 | |
42 if (isset(static::$timers[$name]['time'])) { | |
43 $diff += static::$timers[$name]['time']; | |
44 } | |
45 return $diff; | |
46 } | |
47 return static::$timers[$name]['time']; | |
48 } | |
49 | |
50 /** | |
51 * Stops the timer with the specified name. | |
52 * | |
53 * @param string $name | |
54 * The name of the timer. | |
55 * | |
56 * @return array | |
57 * A timer array. The array contains the number of times the timer has been | |
58 * started and stopped (count) and the accumulated timer value in ms (time). | |
59 */ | |
60 public static function stop($name) { | |
61 if (isset(static::$timers[$name]['start'])) { | |
62 $stop = microtime(TRUE); | |
63 $diff = round(($stop - static::$timers[$name]['start']) * 1000, 2); | |
64 if (isset(static::$timers[$name]['time'])) { | |
65 static::$timers[$name]['time'] += $diff; | |
66 } | |
67 else { | |
68 static::$timers[$name]['time'] = $diff; | |
69 } | |
70 unset(static::$timers[$name]['start']); | |
71 } | |
72 | |
73 return static::$timers[$name]; | |
74 } | |
75 | |
76 } |