1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Profiler; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Psr\Log\LogLevel; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Profiler provides profiling support. It stores profiling messages in the memory and sends them to different targets |
12
|
|
|
* according to {@see Profiler::$targets}. |
13
|
|
|
* |
14
|
|
|
* For more details and usage information on Profiler, see the [guide article on profiling](guide:runtime-profiling) |
15
|
|
|
*/ |
16
|
|
|
class Profiler implements ProfilerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var bool whether to profiler is enabled. Defaults to true. |
20
|
|
|
* You may use this field to disable writing of the profiling messages and thus save the memory usage. |
21
|
|
|
*/ |
22
|
|
|
private bool $enabled = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array[] complete profiling messages. |
26
|
|
|
* Each message has a following keys: |
27
|
|
|
* |
28
|
|
|
* - token: string, profiling token. |
29
|
|
|
* - category: string, message category. |
30
|
|
|
* - nestedLevel: int, profiling message nested level. |
31
|
|
|
* - beginTime: float, profiling begin timestamp obtained by microtime(true). |
32
|
|
|
* - endTime: float, profiling end timestamp obtained by microtime(true). |
33
|
|
|
* - duration: float, profiling block duration in milliseconds. |
34
|
|
|
* - beginMemory: int, memory usage at the beginning of profile block in bytes, obtained by `memory_get_usage()`. |
35
|
|
|
* - endMemory: int, memory usage at the end of profile block in bytes, obtained by `memory_get_usage()`. |
36
|
|
|
* - memoryDiff: int, a diff between 'endMemory' and 'beginMemory'. |
37
|
|
|
*/ |
38
|
|
|
private array $messages = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var LoggerInterface logger to be used for message export. |
42
|
|
|
*/ |
43
|
|
|
private LoggerInterface $logger; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array pending profiling messages, e.g. the ones which have begun but not ended yet. |
47
|
|
|
*/ |
48
|
|
|
private array $pendingMessages = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int current profiling messages nested level. |
52
|
|
|
*/ |
53
|
|
|
private int $nestedLevel = 0; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array|Target[] the profiling targets. Each array element represents a single {@see Target|profiling target} |
57
|
|
|
* instance or the configuration for creating the profiling target instance. |
58
|
|
|
*/ |
59
|
|
|
private array $targets = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var bool whether {@see targets} have been initialized, e.g. ensured to be objects. |
63
|
|
|
*/ |
64
|
|
|
private bool $isTargetsInitialized = false; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initializes the profiler by registering {@see flush()} as a shutdown function. |
68
|
|
|
* |
69
|
|
|
* @param LoggerInterface $logger |
70
|
|
|
*/ |
71
|
4 |
|
public function __construct(LoggerInterface $logger) |
72
|
|
|
{ |
73
|
4 |
|
$this->logger = $logger; |
74
|
4 |
|
register_shutdown_function([$this, 'flush']); |
75
|
4 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool the profile enabled. |
79
|
|
|
* |
80
|
|
|
* {@see enabled} |
81
|
|
|
*/ |
82
|
|
|
public function getEnabled(): bool |
83
|
|
|
{ |
84
|
|
|
return $this->enabled; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array the messages profiler. |
89
|
|
|
*/ |
90
|
3 |
|
public function getMessages(): array |
91
|
|
|
{ |
92
|
3 |
|
return $this->messages; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return Target[] the profiling targets. Each array element represents a single {@see Target|profiling target} |
97
|
|
|
* instance. |
98
|
|
|
*/ |
99
|
2 |
|
public function getTargets(): array |
100
|
|
|
{ |
101
|
2 |
|
if (!$this->isTargetsInitialized) { |
102
|
2 |
|
foreach ($this->targets as $name => $target) { |
103
|
2 |
|
if (!$target instanceof Target) { |
104
|
1 |
|
$this->targets[$name] = new $target['__class']($target['logger'], $target['level']); |
105
|
|
|
} |
106
|
|
|
} |
107
|
2 |
|
$this->isTargetsInitialized = true; |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
return $this->targets; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the profiler enabled or disabled. |
115
|
|
|
* |
116
|
|
|
* @param bool $value |
117
|
|
|
*/ |
118
|
1 |
|
public function setEnabled(bool $value): void |
119
|
|
|
{ |
120
|
1 |
|
$this->enabled = $value; |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set messages profiler. |
125
|
|
|
* |
126
|
|
|
* @param array $value |
127
|
|
|
*/ |
128
|
|
|
public function setMessages(array $value): void |
129
|
|
|
{ |
130
|
|
|
$this->messages = $value; |
131
|
|
|
|
132
|
|
|
$this->dispatch($this->messages); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param array|Target[] $targets the profiling targets. Each array element represents a single |
137
|
|
|
* {@see Target|profiling target} instance or the configuration for creating the profiling target instance. |
138
|
|
|
*/ |
139
|
1 |
|
public function setTargets(array $targets): void |
140
|
|
|
{ |
141
|
1 |
|
$this->targets = $targets; |
142
|
1 |
|
$this->isTargetsInitialized = false; |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Adds extra target to {@see targets}. |
147
|
|
|
* |
148
|
|
|
* @param array|Target $target the log target instance or its DI compatible configuration. |
149
|
|
|
* @param string|null $name array key to be used to store target, if `null` is given target will be append |
150
|
|
|
* to the end of the array by natural integer key. |
151
|
|
|
*/ |
152
|
2 |
|
public function addTarget($target, ?string $name = null): void |
153
|
|
|
{ |
154
|
2 |
|
if (!$target instanceof Target) { |
155
|
|
|
$this->isTargetsInitialized = false; |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
if ($name === null) { |
159
|
2 |
|
$this->targets[] = $target; |
160
|
|
|
} else { |
161
|
1 |
|
$this->targets[$name] = $target; |
162
|
|
|
} |
163
|
2 |
|
} |
164
|
|
|
|
165
|
4 |
|
public function begin(string $token, array $context = []): void |
166
|
|
|
{ |
167
|
4 |
|
if (!$this->enabled) { |
168
|
1 |
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
4 |
|
$category = $context['category'] ?? 'application'; |
172
|
|
|
|
173
|
4 |
|
$message = array_merge( |
174
|
4 |
|
$context, |
175
|
|
|
[ |
176
|
4 |
|
'token' => $token, |
177
|
4 |
|
'category' => $category, |
178
|
4 |
|
'nestedLevel' => $this->nestedLevel, |
179
|
4 |
|
'beginTime' => microtime(true), |
180
|
4 |
|
'beginMemory' => memory_get_usage(), |
181
|
|
|
] |
182
|
|
|
); |
183
|
|
|
|
184
|
4 |
|
$this->pendingMessages[$category][$token][] = $message; |
185
|
4 |
|
$this->nestedLevel++; |
186
|
4 |
|
} |
187
|
|
|
|
188
|
4 |
|
public function end(string $token, array $context = []): void |
189
|
|
|
{ |
190
|
4 |
|
if (!$this->enabled) { |
191
|
1 |
|
return; |
192
|
|
|
} |
193
|
|
|
|
194
|
4 |
|
$category = $context['category'] ?? 'application'; |
195
|
|
|
|
196
|
4 |
|
if (empty($this->pendingMessages[$category][$token])) { |
197
|
|
|
throw new \InvalidArgumentException( |
198
|
|
|
sprintf( |
199
|
|
|
'Unexpected %s::end() call for category "%s" token "%s". A matching begin() is not found.', |
200
|
|
|
static::class, |
201
|
|
|
$category, |
202
|
|
|
$token |
203
|
|
|
) |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
4 |
|
$message = array_pop($this->pendingMessages[$category][$token]); |
208
|
4 |
|
if (empty($this->pendingMessages[$category][$token])) { |
209
|
4 |
|
unset($this->pendingMessages[$category][$token]); |
210
|
|
|
|
211
|
4 |
|
if (empty($this->pendingMessages[$category])) { |
212
|
4 |
|
unset($this->pendingMessages[$category]); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
4 |
|
$message = array_merge( |
217
|
4 |
|
$message, |
218
|
|
|
$context, |
219
|
|
|
[ |
220
|
4 |
|
'endTime' => microtime(true), |
221
|
4 |
|
'endMemory' => memory_get_usage(), |
222
|
|
|
] |
223
|
|
|
); |
224
|
|
|
|
225
|
4 |
|
$message['duration'] = $message['endTime'] - $message['beginTime']; |
226
|
4 |
|
$message['memoryDiff'] = $message['endMemory'] - $message['beginMemory']; |
227
|
|
|
|
228
|
4 |
|
$this->messages[] = $message; |
229
|
4 |
|
$this->nestedLevel--; |
230
|
4 |
|
} |
231
|
|
|
|
232
|
2 |
|
public function flush(): void |
233
|
|
|
{ |
234
|
2 |
|
foreach ($this->pendingMessages as $category => $categoryMessages) { |
235
|
|
|
foreach ($categoryMessages as $token => $messages) { |
236
|
|
|
if (!empty($messages)) { |
237
|
|
|
$this->logger->log( |
238
|
|
|
LogLevel::WARNING, |
239
|
|
|
'Unclosed profiling entry detected: category "' . $category . '" token "' . $token . '"' . ' ' . |
240
|
|
|
__METHOD__ |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
2 |
|
$this->pendingMessages = []; |
247
|
2 |
|
$this->nestedLevel = 0; |
248
|
|
|
|
249
|
2 |
|
if (empty($this->messages)) { |
250
|
|
|
return; |
251
|
|
|
} |
252
|
|
|
|
253
|
2 |
|
$messages = $this->messages; |
254
|
|
|
|
255
|
|
|
// new messages could appear while the existing ones are being handled by targets |
256
|
2 |
|
$this->messages = []; |
257
|
|
|
|
258
|
2 |
|
$this->dispatch($messages); |
259
|
2 |
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Dispatches the profiling messages to {@see targets}. |
263
|
|
|
* |
264
|
|
|
* @param array $messages the profiling messages. |
265
|
|
|
*/ |
266
|
1 |
|
protected function dispatch(array $messages): void |
267
|
|
|
{ |
268
|
1 |
|
foreach ($this->getTargets() as $target) { |
269
|
1 |
|
$target->collect($messages); |
270
|
|
|
} |
271
|
1 |
|
} |
272
|
|
|
} |
273
|
|
|
|