-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathProfile.php
178 lines (152 loc) · 3.99 KB
/
Profile.php
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
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Profiler;
/**
* @author Fabien Potencier <[email protected]>
*/
final class Profile implements \IteratorAggregate, \Serializable
{
public const ROOT = 'ROOT';
public const BLOCK = 'block';
public const TEMPLATE = 'template';
public const MACRO = 'macro';
private $starts = [];
private $ends = [];
private $profiles = [];
public function __construct(
private string $template = 'main',
private string $type = self::ROOT,
private string $name = 'main',
) {
$this->name = str_starts_with($name, '__internal_') ? 'INTERNAL' : $name;
$this->enter();
}
public function getTemplate(): string
{
return $this->template;
}
public function getType(): string
{
return $this->type;
}
public function getName(): string
{
return $this->name;
}
public function isRoot(): bool
{
return self::ROOT === $this->type;
}
public function isTemplate(): bool
{
return self::TEMPLATE === $this->type;
}
public function isBlock(): bool
{
return self::BLOCK === $this->type;
}
public function isMacro(): bool
{
return self::MACRO === $this->type;
}
/**
* @return Profile[]
*/
public function getProfiles(): array
{
return $this->profiles;
}
public function addProfile(self $profile): void
{
$this->profiles[] = $profile;
}
/**
* Returns the duration in microseconds.
*/
public function getDuration(): float
{
if ($this->isRoot() && $this->profiles) {
// for the root node with children, duration is the sum of all child durations
$duration = 0;
foreach ($this->profiles as $profile) {
$duration += $profile->getDuration();
}
return $duration;
}
return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0;
}
/**
* Returns the memory usage in bytes.
*/
public function getMemoryUsage(): int
{
return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0;
}
/**
* Returns the peak memory usage in bytes.
*/
public function getPeakMemoryUsage(): int
{
return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0;
}
/**
* Starts the profiling.
*/
public function enter(): void
{
$this->starts = [
'wt' => microtime(true),
'mu' => memory_get_usage(),
'pmu' => memory_get_peak_usage(),
];
}
/**
* Stops the profiling.
*/
public function leave(): void
{
$this->ends = [
'wt' => microtime(true),
'mu' => memory_get_usage(),
'pmu' => memory_get_peak_usage(),
];
}
public function reset(): void
{
$this->starts = $this->ends = $this->profiles = [];
$this->enter();
}
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->profiles);
}
public function serialize(): string
{
return serialize($this->__serialize());
}
public function unserialize($data): void
{
$this->__unserialize(unserialize($data));
}
/**
* @internal
*/
public function __serialize(): array
{
return [$this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles];
}
/**
* @internal
*/
public function __unserialize(array $data): void
{
[$this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles] = $data;
}
}