|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DJStarCOM\ServerTiming; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
|
6
|
|
|
|
|
7
|
|
|
class ServerTiming |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var Stopwatch */ |
|
10
|
|
|
protected $stopwatch; |
|
11
|
|
|
|
|
12
|
|
|
/** @var array */ |
|
13
|
|
|
protected $finishedEvents = []; |
|
14
|
|
|
|
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
protected $startedEvents = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* ServerTiming constructor. |
|
20
|
|
|
* @param Stopwatch $stopwatch |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(Stopwatch $stopwatch) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->stopwatch = $stopwatch; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Add metric |
|
29
|
|
|
* @param string $metric |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
|
|
public function addMetric(string $metric) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->finishedEvents[$metric] = null; |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Has started event |
|
41
|
|
|
* @param string $key |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
public function hasStartedEvent(string $key): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return array_key_exists($key, $this->startedEvents); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Measure |
|
51
|
|
|
* @param string $key |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
|
|
public function measure(string $key) |
|
55
|
|
|
{ |
|
56
|
|
|
if (! $this->hasStartedEvent($key)) { |
|
57
|
|
|
return $this->start($key); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->stop($key); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Start watching |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
|
|
public function start(string $key) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->stopwatch->start($key); |
|
71
|
|
|
|
|
72
|
|
|
$this->startedEvents[$key] = true; |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Stop watching |
|
79
|
|
|
* @param string $key |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function stop(string $key) |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->stopwatch->isStarted($key)) { |
|
85
|
|
|
$event = $this->stopwatch->stop($key); |
|
86
|
|
|
|
|
87
|
|
|
$this->setDuration($key, $event->getDuration()); |
|
88
|
|
|
|
|
89
|
|
|
unset($this->startedEvents[$key]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Stop all unfinished events |
|
97
|
|
|
*/ |
|
98
|
|
|
public function stopAllUnfinishedEvents() |
|
99
|
|
|
{ |
|
100
|
|
|
foreach (array_keys($this->startedEvents) as $startedEventName) { |
|
101
|
|
|
$this->stop($startedEventName); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Set event duration |
|
107
|
|
|
* @param string $key |
|
108
|
|
|
* @param $duration |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setDuration(string $key, $duration) |
|
112
|
|
|
{ |
|
113
|
|
|
if (is_callable($duration)) { |
|
114
|
|
|
$this->start($key); |
|
115
|
|
|
|
|
116
|
|
|
call_user_func($duration); |
|
117
|
|
|
|
|
118
|
|
|
$this->stop($key); |
|
119
|
|
|
} else { |
|
120
|
|
|
$this->finishedEvents[$key] = $duration; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get event duration |
|
128
|
|
|
* @param string $key |
|
129
|
|
|
* @return mixed|null |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getDuration(string $key) |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->finishedEvents[$key] ?? null; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get all events |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
public function events(): array |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->finishedEvents; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|