blob: 8d5ec9577dd648ed705d7ca7541e9d9e01849670 (
plain)
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
|
<?php
final class Timer
{
private static ?Timer $instance = null;
private array $timers = [];
/**
* gets the instance via lazy initialization (created on first usage)
*/
public static function getInstance(): Timer
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function start_timer(string $timer_name, $instance): void
{
$this->timers[$timer_name][$instance]['start'] = microtime(true);
}
public function stop_timer(string $timer_name, $instance): void
{
if (isset($this->timers[$timer_name][$instance])) {
$this->timers[$timer_name][$instance]['end'] = microtime(true);
} else {
throw new InvalidArgumentException('Timer not found: ' . $timer_name);
}
}
public function get_time_deltas(): array
{
$deltas = [];
foreach ($this->timers as $timer_name => $timer) {
foreach ($timer as $instance) {
$delta = $this->compute_delta($instance);
if (is_float($delta)) {
$deltas[$timer_name][] = $delta * 1000;
}
}
}
$res = [];
foreach ($deltas as $timer_name => $data) {
$res[$timer_name] = array_sum($data)/count($data);
}
return $res;
}
private function compute_delta($data)
{
if (isset($data['start']) && isset($data['end'])) {
return $data['end'] - $data['start'];
}
return false;
}
/**
* is not allowed to call from outside to prevent from creating multiple instances,
* to use the singleton, you have to obtain the instance from Singleton::getInstance() instead
*/
private function __construct()
{
}
/**
* prevent the instance from being cloned (which would create a second instance of it)
*/
private function __clone()
{
}
/**
* prevent from being unserialized (which would create a second instance of it)
*/
public function __wakeup()
{
throw new Exception("Cannot unserialize singleton");
}
}
|