-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecord.php
More file actions
206 lines (187 loc) · 4.98 KB
/
Copy pathRecord.php
File metadata and controls
206 lines (187 loc) · 4.98 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
<?php
declare(strict_types=1);
namespace ArrayIterator\Profiler;
use ArrayIterator\Profiler\Interfaces\CollectionInterface;
use ArrayIterator\Profiler\Interfaces\GroupInterface;
use ArrayIterator\Profiler\Interfaces\RecordInterface;
use ArrayIterator\Profiler\Traits\DurationTimeTrait;
use ArrayIterator\Profiler\Traits\MemoryTrait;
use ArrayIterator\Profiler\Traits\SeverityTrait;
/**
* Object to record the benchmark data
* @template-covariant TKey of scalar
* @template-covariant TValue of mixed
*/
class Record implements RecordInterface
{
use SeverityTrait,
MemoryTrait,
DurationTimeTrait;
/**
* @var GroupInterface $group the group record
*/
protected GroupInterface $group;
/**
* @var string $name record name / identity
*/
protected string $name;
/**
* @var CollectionInterface<scalar, mixed> $data the stored object data
*/
protected CollectionInterface $data;
/**
* @var bool $stopped status of current record
*/
private bool $stopped = false;
/**
* @inheritDoc
* @param iterable<TKey, TValue> $data
*/
public function __construct(
GroupInterface $group,
string $recordName,
iterable $data = []
) {
$this->name = $recordName;
$this->group = $group;
$this->startTime = $this->convertMicrotime();
$this->startMemory = memory_get_usage();
$this->startRealMemory = memory_get_usage(true);
$this->data = new Collection($data);
}
/**
* @inheritDoc
*/
public function createGroupClone(GroupInterface $group) : RecordInterface
{
if ($group === $this->getGroup()) {
return $this;
}
$cloned = clone $this;
$cloned->group = $group;
return $cloned;
}
/**
* @inheritDoc
*/
public function getGroup(): GroupInterface
{
return $this->group;
}
/**
* @inheritDoc
*/
public function getName(): string
{
return $this->name;
}
/**
* @inheritDoc
*/
public function convertMicrotime(?float $microtime = null): float
{
return $this->group->convertMicrotime($microtime);
}
/**
* @inheritDoc
*/
public function getData(): CollectionInterface
{
return $this->data;
}
/**
* @inheritDoc
*/
public function isStopped(): bool
{
return $this->stopped;
}
/**
* @inheritDoc
*/
public function stop(iterable $data = []): RecordInterface
{
if ($this->isStopped()) {
return $this;
}
$this->stopped = true;
$this->data->merge($data); // set data first
$this->endTime = $this->convertMicrotime();
$this->endMemory = memory_get_usage();
$this->endRealMemory = memory_get_usage(true);
$this->usedMemory = $this->measureMemory(
$this->getStartMemory(),
false,
$this->endMemory
);
$this->usedRealMemory = $this->measureMemory(
$this->getStartRealMemory(),
true,
$this->endRealMemory
);
$this->group->stop($this);
return $this;
}
/**
* To Array
*
* @return array{
* "group": string,
* "name": string,
* "stopped": bool,
* "timing": array{
* "start": float,
* "end": float,
* "duration": float
* },
* "memory": array{
* "normal":array{
* start: int,
* end: int,
* },
* "real":array{
* start: int,
* end: int,
* }
* },
* "data": array<scalar, mixed>
* }
*/
public function toArray(): array
{
$startTime = $this->getStartTime();
$duration = $this->getDuration();
$startMemory = $this->getStartMemory();
$usedMemory = $this->getUsedMemory();
$startRealMemory = $this->getStartRealMemory();
$usedRealMemory = $this->getUsedRealMemory();
return [
'group' => $this->group->getName(),
'name' => $this->getName(),
'stopped' => $this->isStopped(),
'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),
],
],
'data' => $this->getData()->getData()
];
}
/**
* @inheritDoc
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
}