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
|
|
|
private const TIMES = [ |
21
|
|
|
'hour' => 3600000, |
22
|
|
|
'minute' => 60000, |
23
|
|
|
'second' => 1000, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Stores all the timers statically. |
28
|
|
|
* @var Stopwatch[] |
29
|
|
|
*/ |
30
|
|
|
static private $timers = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Start or resume the timer. |
34
|
|
|
* |
35
|
|
|
* Call this method to start the timer with a given key. The default key |
36
|
|
|
* is "default", and used in @see \Ayesh\PHP_Timer\Timer::read() and reset() |
37
|
|
|
* methods as well |
38
|
|
|
* |
39
|
|
|
* Calling this with the same $key will not restart the timer if it has already |
40
|
|
|
* started. |
41
|
|
|
* |
42
|
|
|
* @param string $key |
43
|
|
|
*/ |
44
|
|
|
public static function start(string $key = 'default'): void { |
45
|
|
|
if (isset(self::$timers[$key])) { |
46
|
|
|
self::$timers[$key]->start(); |
47
|
|
|
} |
48
|
|
|
else { |
49
|
|
|
self::$timers[$key] = new Stopwatch(); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Resets a specific timer, or default timer if not passed the $key parameter. |
55
|
|
|
* To reset all timers, call @see \Ayesh\PHP_Timer\Timer::resetAll(). |
56
|
|
|
* @param string $key |
57
|
|
|
*/ |
58
|
|
|
public static function reset(string $key = 'default'): void { |
59
|
|
|
unset(self::$timers[$key]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Resets ALL timers. |
64
|
|
|
* To reset a specific timer, @see \Ayesh\PHP_Timer\Timer::reset(). |
65
|
|
|
*/ |
66
|
|
|
public static function resetAll(): void { |
67
|
|
|
self::$timers = []; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Formats the given time the processor into the given format. |
72
|
|
|
* @param float $value |
73
|
|
|
* @param string|bool $format |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
private static function formatTime(float $value, $format): string { |
77
|
|
|
switch ($format) { |
78
|
|
|
case static::FORMAT_MILLISECONDS: |
79
|
|
|
return (string) round($value * 1000, 2); |
80
|
|
|
|
81
|
|
|
case static::FORMAT_SECONDS: |
82
|
|
|
return (string) round($value, 3); |
83
|
|
|
|
84
|
|
|
case static::FORMAT_HUMAN: |
85
|
|
|
return static::secondsToTimeString($value); |
|
|
|
|
86
|
|
|
|
87
|
|
|
case static::FORMAT_PRECISE: |
88
|
|
|
default: |
89
|
|
|
return (string) ($value * 1000); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private static function secondsToTimeString(float $time): string |
94
|
|
|
{ |
95
|
|
|
$ms = \round($time * 1000); |
96
|
|
|
|
97
|
|
|
foreach (self::TIMES as $unit => $value) { |
98
|
|
|
if ($ms >= $value) { |
99
|
|
|
$time = floor($ms / $value * 100.0) / 100.0; |
100
|
|
|
return $time . ' ' . ($time == 1 ? $unit : $unit . 's'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $ms . ' ms'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the time elapsed in the format requested in the $format parameter. |
109
|
|
|
* To access a specific timer, pass the same key that |
110
|
|
|
* @see \Ayesh\PHP_Timer\Timer::start() was called with. If the timer was not |
111
|
|
|
* started, a \LogicException will be thrown. |
112
|
|
|
* |
113
|
|
|
* The default format is milliseconds. See the class constants for additional |
114
|
|
|
* formats. |
115
|
|
|
* |
116
|
|
|
* @param string $key The key that the timer was started with. Default value is |
117
|
|
|
* "default" throughout the class. |
118
|
|
|
* @param string $format |
119
|
|
|
* @return mixed The formatted time. |
120
|
|
|
* @throws \LogicException |
121
|
|
|
*/ |
122
|
|
|
public static function read(string $key = 'default', $format = self::FORMAT_MILLISECONDS) { |
123
|
|
|
if (isset(self::$timers[$key])) { |
124
|
|
|
return self::formatTime(self::$timers[$key]->read(), $format); |
125
|
|
|
} |
126
|
|
|
throw new \LogicException('Reading timer when the given key timer was not initialized.'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Stops the timer with the given key. Default key is "default" |
131
|
|
|
* |
132
|
|
|
* @param string $key |
133
|
|
|
* |
134
|
|
|
* @throws \LogicException If the attempted timer has not started already. |
135
|
|
|
*/ |
136
|
|
|
public static function stop($key = 'default'): void { |
137
|
|
|
if (isset(self::$timers[$key])) { |
138
|
|
|
self::$timers[$key]->stop(); |
139
|
|
|
} else { |
140
|
|
|
throw new \LogicException('Stopping timer when the given key timer was not initialized.'); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return a list of timer names. Note that resetting a timer removes the timer. |
146
|
|
|
* @return string[] |
147
|
|
|
*/ |
148
|
|
|
public static function getTimers(): array { |
149
|
|
|
return array_keys(self::$timers); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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: