1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Ayesh\PHP_Timer; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Timer |
8
|
|
|
* |
9
|
|
|
* Helper class to measure the execution time between two points in a single |
10
|
|
|
* request. |
11
|
|
|
* |
12
|
|
|
* @package Ayesh\PHP_Timer |
13
|
|
|
*/ |
14
|
|
|
class Timer { |
15
|
|
|
public const FORMAT_PRECISE = FALSE; |
16
|
|
|
public const FORMAT_MILLISECONDS = 'ms'; |
17
|
|
|
public const FORMAT_SECONDS = 's'; |
18
|
|
|
public const FORMAT_HUMAN = 'h'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Stores all the timers statically. |
22
|
|
|
* @var Stopwatch[] |
23
|
|
|
*/ |
24
|
|
|
static private $timers = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Start or resume the timer. |
28
|
|
|
* |
29
|
|
|
* Call this method to start the timer with a given key. The default key |
30
|
|
|
* is "default", and used in @see \Ayesh\PHP_Timer\Timer::read() and reset() |
31
|
|
|
* methods as well |
32
|
|
|
* |
33
|
|
|
* Calling this with the same $key will not restart the timer if it has already |
34
|
|
|
* started. |
35
|
|
|
* |
36
|
|
|
* @param string $key |
37
|
|
|
*/ |
38
|
|
|
public static function start(string $key = 'default'): void { |
39
|
|
|
if (isset(self::$timers[$key])) { |
40
|
|
|
self::$timers[$key]->start(); |
41
|
|
|
} |
42
|
|
|
else { |
43
|
|
|
self::$timers[$key] = new Stopwatch(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Resets a specific timer, or default timer if not passed the $key parameter. |
49
|
|
|
* To reset all timers, call @see \Ayesh\PHP_Timer\Timer::resetAll(). |
50
|
|
|
* @param string $key |
51
|
|
|
*/ |
52
|
|
|
public static function reset(string $key = 'default'): void { |
53
|
|
|
unset(self::$timers[$key]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Resets ALL timers. |
58
|
|
|
* To reset a specific timer, @see \Ayesh\PHP_Timer\Timer::reset(). |
59
|
|
|
*/ |
60
|
|
|
public static function resetAll(): void { |
61
|
|
|
self::$timers = []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Formats the given time the processor into the given format. |
66
|
|
|
* @param $value |
67
|
|
|
* @param $format |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
private static function formatTime($value, $format): string { |
71
|
|
|
switch ($format) { |
72
|
|
|
|
73
|
|
|
case static::FORMAT_PRECISE: |
74
|
|
|
return (string) ($value * 1000); |
75
|
|
|
|
76
|
|
|
case static::FORMAT_MILLISECONDS: |
77
|
|
|
return (string) round($value * 1000, 2); |
78
|
|
|
|
79
|
|
|
case static::FORMAT_SECONDS: |
80
|
|
|
return (string) round($value, 3); |
81
|
|
|
|
82
|
|
|
default: |
83
|
|
|
return (string) ($value * 1000); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the time elapsed in the format requested in the $format parameter. |
89
|
|
|
* To access a specific timer, pass the same key that |
90
|
|
|
* @see \Ayesh\PHP_Timer\Timer::start() was called with. If the timer was not |
91
|
|
|
* started, a \LogicException will be thrown. |
92
|
|
|
* |
93
|
|
|
* The default format is milliseconds. See the class constants for additional |
94
|
|
|
* formats. |
95
|
|
|
* |
96
|
|
|
* @param string $key The key that the timer was started with. Default value is |
97
|
|
|
* "default" throughout the class. |
98
|
|
|
* @param string $format |
99
|
|
|
* @return mixed The formatted time. |
100
|
|
|
* @throws \LogicException |
101
|
|
|
*/ |
102
|
|
|
public static function read(string $key = 'default', $format = self::FORMAT_MILLISECONDS) { |
103
|
|
|
if (isset(self::$timers[$key])) { |
104
|
|
|
return self::formatTime(self::$timers[$key]->read(), $format); |
105
|
|
|
} |
106
|
|
|
throw new \LogicException('Reading timer when the given key timer was not initialized.'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Stops the timer with the given key. Default key is "default" |
111
|
|
|
* |
112
|
|
|
* @param string $key |
113
|
|
|
* |
114
|
|
|
* @throws \LogicException If the attempted timer has not started already. |
115
|
|
|
*/ |
116
|
|
|
public static function stop($key = 'default'): void { |
117
|
|
|
if (isset(self::$timers[$key])) { |
118
|
|
|
self::$timers[$key]->stop(); |
119
|
|
|
} else { |
120
|
|
|
throw new \LogicException('Stopping timer when the given key timer was not initialized.'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return a list of timer names. Note that resetting a timer removes the timer. |
126
|
|
|
* @return string[] |
127
|
|
|
*/ |
128
|
|
|
public static function getTimers(): array { |
129
|
|
|
return array_keys(static::$timers); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: