-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTimer.php
More file actions
147 lines (127 loc) · 3.79 KB
/
Timer.php
File metadata and controls
147 lines (127 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace Ayesh\PHP_Timer;
use LogicException;
use function round;
/**
* Class Timer
*
* Helper class to measure the execution time between two points in a single
* request.
*
* @package Ayesh\PHP_Timer
*/
class Timer {
public const FORMAT_PRECISE = false;
public const FORMAT_MILLISECONDS = 'ms';
public const FORMAT_SECONDS = 's';
public const FORMAT_HUMAN = 'h';
private const TIMES = [
'hour' => 3600000,
'minute' => 60000,
'second' => 1000,
];
/**
* Stores all the timers statically.
*
* @var Stopwatch[]
*/
static private array $timers = [];
/**
* Start or resume the timer.
*
* Call this method to start the timer with a given key. The default key
* is "default", and used in @param string $key
*
* @see Timer::read and reset()
* methods as well
*
* Calling this with the same $key will not restart the timer if it has already
* started.
*
*/
public static function start(string $key = 'default'): void {
if (isset(self::$timers[$key])) {
self::$timers[$key]->start();
return;
}
self::$timers[$key] = new Stopwatch();
}
/**
* Resets a specific timer, or default timer if not passed the $key parameter.
* To reset all timers, call @param string $key
*
* @see Timer::resetAll
*/
public static function reset(string $key = 'default'): void {
unset(self::$timers[$key]);
}
/**
* Resets ALL timers.
* To reset a specific timer, @see Timer::reset
*/
public static function resetAll(): void {
self::$timers = [];
}
/**
* Returns the time elapsed in the format requested in the $format parameter.
* To access a specific timer, pass the same key that
*
* @param string $key The key that the timer was started with. Default value is
* "default" throughout the class.
* @param string $format The default format is milliseconds. See the class constants for additional
* formats.
*
* @return mixed The formatted time, formatted by the formatter string passed for $format.
* @throws LogicException
* If the timer was not started, a \LogicException will be thrown. Use @see Timer::start
* to start a timer.
*/
public static function read(string $key = 'default', string $format = self::FORMAT_MILLISECONDS): string {
if (!isset(self::$timers[$key])) {
throw new LogicException('Reading timer when the given key timer was not initialized.');
}
return self::formatTime(self::$timers[$key]->read(), $format);
}
/**
* Formats the given time the processor into the given format.
*
* @param float $value
* @param string $format
*
* @return string
*/
private static function formatTime(float $value, string $format): string {
return match ($format) {
static::FORMAT_MILLISECONDS => (string) round($value * 1000, 2),
static::FORMAT_SECONDS => (string) round($value, 3),
static::FORMAT_HUMAN => static::secondsToTimeString($value),
default => (string) ($value * 1000),
};
}
private static function secondsToTimeString(float $time): string {
$ms = (int) round($time * 1000);
return Formatter::formatTime($ms);
}
/**
* Stops the timer with the given key. Default key is "default"
*
* @param string $key
*
* @throws LogicException If the attempted timer has not started already.
*/
public static function stop(string $key = 'default'): void {
if (!isset(self::$timers[$key])) {
throw new LogicException('Stopping timer when the given key timer was not initialized.');
}
self::$timers[$key]->stop();
}
/**
* Return a list of timer names. Note that resetting a timer removes the timer.
*
* @return string[]
*/
public static function getTimers(): array {
return array_keys(self::$timers);
}
}