Repository navigation
Expand file tree
/
Copy pathProfiler.php
More file actions
382 lines (349 loc) · 9.39 KB
/
Copy pathProfiler.php
File metadata and controls
382 lines (349 loc) · 9.39 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
declare(strict_types=1);
namespace ArrayIterator\Profiler;
use ArrayIterator;
use ArrayIterator\Profiler\Aggregate\Interfaces\AggregatorInterface;
use ArrayIterator\Profiler\Interfaces\CollectionInterface;
use ArrayIterator\Profiler\Interfaces\GroupInterface;
use ArrayIterator\Profiler\Interfaces\ProfilerInterface;
use ArrayIterator\Profiler\Interfaces\RecordInterface;
use ArrayIterator\Profiler\Traits\DurationTimeTrait;
use ArrayIterator\Profiler\Traits\MemoryTrait;
use ArrayIterator\Profiler\Traits\SeverityTrait;
use SplObjectStorage;
use Traversable;
use function count;
use function debug_backtrace;
use function is_subclass_of;
use function iterator_to_array;
use function max;
use function memory_get_usage;
use function microtime;
use const DEBUG_BACKTRACE_IGNORE_ARGS;
/**
* The Profiler
*/
class Profiler implements ProfilerInterface
{
use DurationTimeTrait,
SeverityTrait,
MemoryTrait;
/**
* @var CollectionInterface<string, GroupInterface> $groups
*/
protected CollectionInterface $groups;
/**
* @var SplObjectStorage<AggregatorInterface, mixed>
*/
private SplObjectStorage $aggregators;
/**
* @var bool true if enable
*/
private bool $enable = true;
/**
* @var Waterfall Waterfall instance
*/
private Waterfall $waterfall;
/**
* The profiler constructor
*
* @param array<scalar, AggregatorInterface> $initialAggregators initial provider aggregators
*/
public function __construct(bool $enable = true, iterable $initialAggregators = [])
{
$this->startMemory = memory_get_usage();
$this->startRealMemory = memory_get_usage(true);
$this->startTime = $this->convertMicrotime();
$this->aggregators = new SplObjectStorage();
$this->groups = new Collection();
$this->setEnable($enable);
foreach ($initialAggregators as $aggregator) {
if ($aggregator instanceof AggregatorInterface) {
$this->addAggregator($aggregator);
}
}
}
/***
* @inheritDoc
*/
public function recordState(RecordInterface $record) : void
{
if (!$record->isStopped()) {
return;
}
$class = (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]??[])['class']??null;
if (!$class || !is_subclass_of($class, GroupInterface::class)) {
return;
}
$endTime = $record->getEndTime();
if ($this->endTime && $this->endTime > $endTime) {
return;
}
$this->endMemory = memory_get_usage();
$this->endRealMemory = memory_get_usage(true);
$this->endTime = $endTime;
}
/**
* @inheritDoc
*/
public function setEnable(bool $enable)
{
$this->enable = $enable;
}
/**
* @inheritDoc
*/
public function isEnable(): bool
{
return $this->enable;
}
/**
* @inheritDoc
*/
public function addAggregator(AggregatorInterface $aggregator)
{
$this->aggregators->attach($aggregator);
}
/**
* @inheritDoc
*/
public function removeAggregator(AggregatorInterface $aggregator)
{
$this->aggregators->detach($aggregator);
}
/**
* @inheritDoc
*/
public function hasAggregator(AggregatorInterface $aggregator): bool
{
return $this->aggregators->contains($aggregator);
}
/**
* @inheritDoc
* @return array<AggregatorInterface>
*/
public function getAggregators(): array
{
return iterator_to_array($this->aggregators);
}
/**
* @inheritDoc
*/
public function aggregate(RecordInterface $record) : int
{
$aggregated = 0;
foreach ($this->getAggregators() as $aggregator) {
$aggregated += $aggregator->aggregate($record) ? 1 : 0;
}
return $aggregated;
}
/**
* @inheritDoc
*/
public function convertMicrotime(?float $microtime = null): float
{
return (float) (($microtime ?? microtime(true)) * self::EXPONENT);
}
/**
* @inheritDoc
*/
public function hasGroup(string $name): bool
{
return isset($this->groups[$name]);
}
/**
* @inheritDoc
* @param GroupInterface<string, array<integer, RecordInterface>> $group
*/
public function addGroup(GroupInterface $group)
{
$this->groups[$group->getName()] ??= $group;
}
/**
* @inheritDoc
*/
public function getGroup(string $groupName): ?GroupInterface
{
return $this->groups[$groupName]??null;
}
/**
* @inheritDoc
*/
public function group(string $groupName) : GroupInterface
{
if (isset($this->groups[$groupName])) {
return $this->groups[$groupName];
}
$group = new Group($this, $groupName);
/**
* @var GroupInterface<string, array<integer, RecordInterface> $group
*/
return $this->groups[$groupName] = $group;
}
/**
* @inheritDoc
*/
public function getGroups(): array
{
return $this->groups->getData();
}
/**
* @inheritdoc
*/
public function getAllRecords(): iterable
{
foreach ($this->getGroups() as $group) {
foreach ($group->getAllRecords() as $id => $record) {
yield $id => $record;
}
}
}
/**
* @inheritDoc
*/
public function start(
string $recordName,
string $groupName = self::DEFAULT_GROUP,
iterable $initialData = []
): RecordInterface {
return $this->group($groupName)->start($recordName, $initialData);
}
/**
* @inheritDoc
*/
public function stop(
?string $recordName = null,
string $groupName = self::DEFAULT_GROUP,
iterable $additionalData = []
): ?RecordInterface {
$group = $this->getGroup($groupName);
return $group ? $group->stop($recordName, $additionalData) : null;
}
/**
* @inheritDoc
*/
public function stopAll(
?string $recordName = null,
string $groupName = self::DEFAULT_GROUP,
iterable $additionalData = []
): array {
$group = $this->getGroup($groupName);
if (!$group) {
return [];
}
$result = [];
foreach (($recordName !== null ? $group->getRecord($recordName) : $group->getAllRecords()) as $key => $record) {
if ($record->isStopped()) {
continue;
}
$record = $group->stop($record, $additionalData);
if ($record) {
$result[$key] = $record;
}
}
return $result;
}
/**
* Get waterfall instance
*
* @return Waterfall
*/
public function getWaterfall(): Waterfall
{
return $this->waterfall ??= new Waterfall($this);
}
/**
* @inheritDoc
*/
public function clear(): void
{
$this->groups->clear();
}
/**
* @inheritDoc
*/
public function count(): int
{
return count($this->groups);
}
/**
* @inheritDoc
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->getGroups());
}
/**
* @inheritDoc
* @return array{
* timing: array{
* start: float,
* end: float,
* duration: float,
* },
* memory: array{
* normal:array{
* start: int,
* end: int,
* },
* real:array{
* start: int,
* end: int,
* }
* },
* groups: array<string, GroupInterface>,
* }
*/
public function toArray(): array
{
$startTime = $this->getStartTime();
$duration = $this->getDuration();
$startMemory = $this->getStartMemory();
$usedMemory = $this->getUsedMemory();
$startRealMemory = $this->getStartRealMemory();
$usedRealMemory = $this->getUsedRealMemory();
return [
'timing' => [
'start' => $startTime,
'end' => max($duration - $startTime, 0),
'duration' => $duration
],
'memory' => [
'normal' => [
'start' => $startMemory,
'end' => max($usedMemory - $startMemory, 0),
],
'real' => [
'start' => $startRealMemory,
'end' => max($usedRealMemory - $startRealMemory, 0),
],
],
'groups' => $this->getGroups(),
];
}
/**
* @inheritDoc
* @return array{
* timing: array{
* start: float,
* end: float,
* duration: float,
* },
* memory: array{
* normal:array{
* start: int,
* end: int,
* },
* real:array{
* start: int,
* end: int,
* }
* },
* groups: array<string, GroupInterface>,
* }
*/
public function jsonSerialize() : array
{
return $this->toArray();
}
}