1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Flight Routing. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.1 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Flight\Routing; |
19
|
|
|
|
20
|
|
|
use ArrayIterator; |
21
|
|
|
use Flight\Routing\Interfaces\RouteInterface; |
22
|
|
|
use IteratorAggregate; |
23
|
|
|
use Traversable; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class ProfileRoute implements IteratorAggregate |
29
|
|
|
{ |
30
|
|
|
/** @var null|RouteInterface */ |
31
|
|
|
private $route; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
private $name; |
35
|
|
|
|
36
|
|
|
/** @var bool */ |
37
|
|
|
private $matched; |
38
|
|
|
|
39
|
|
|
/** @var array<string,float|int> */ |
40
|
|
|
private $starts = []; |
41
|
|
|
|
42
|
|
|
/** @var array<string,float|int> */ |
43
|
|
|
private $ends = []; |
44
|
|
|
|
45
|
|
|
/** @var ProfileRoute[] */ |
46
|
|
|
private $profiles = []; |
47
|
|
|
|
48
|
|
|
public function __construct(string $name = 'main', ?RouteInterface $route = null) |
49
|
|
|
{ |
50
|
|
|
$this->route = $route; |
51
|
|
|
$this->name = $name; |
52
|
|
|
$this->enter(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Add Matched info of route |
57
|
|
|
* |
58
|
|
|
* @param string $name |
59
|
|
|
* @param bool $matched |
60
|
|
|
*/ |
61
|
|
|
public function setMatched(string $name, bool $matched = true): void |
62
|
|
|
{ |
63
|
|
|
foreach ($this->profiles as $profile) { |
64
|
|
|
if ($name === $profile->getName()) { |
65
|
|
|
$profile->matched = $matched; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return null|RouteInterface |
72
|
|
|
*/ |
73
|
|
|
public function getRoute(): ?RouteInterface |
74
|
|
|
{ |
75
|
|
|
return $this->route; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getName(): string |
82
|
|
|
{ |
83
|
|
|
return $this->name; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function isRoute(): bool |
90
|
|
|
{ |
91
|
|
|
return $this->route instanceof RouteInterface; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return ProfileRoute[] |
96
|
|
|
*/ |
97
|
|
|
public function getProfiles(): array |
98
|
|
|
{ |
99
|
|
|
return $this->profiles; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add a new profiled route |
104
|
|
|
* |
105
|
|
|
* @param ProfileRoute $profile |
106
|
|
|
*/ |
107
|
|
|
public function addProfile(self $profile): void |
108
|
|
|
{ |
109
|
|
|
$this->profiles[] = $profile; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns the duration in microseconds. |
114
|
|
|
* |
115
|
|
|
* @return float |
116
|
|
|
*/ |
117
|
|
|
public function getDuration(): float |
118
|
|
|
{ |
119
|
|
|
if (!empty($this->profiles)) { |
120
|
|
|
// for the root node with children, duration is the sum of all child durations |
121
|
|
|
$duration = 0; |
122
|
|
|
|
123
|
|
|
foreach ($this->profiles as $profile) { |
124
|
|
|
$duration += $profile->getDuration(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $duration; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns the memory usage in bytes. |
135
|
|
|
* |
136
|
|
|
* @return int|float |
137
|
|
|
*/ |
138
|
|
|
public function getMemoryUsage() |
139
|
|
|
{ |
140
|
|
|
return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns the peak memory usage in bytes. |
145
|
|
|
* |
146
|
|
|
* @return int|float |
147
|
|
|
*/ |
148
|
|
|
public function getPeakMemoryUsage() |
149
|
|
|
{ |
150
|
|
|
return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Starts the profiling. |
155
|
|
|
*/ |
156
|
|
|
public function enter(): void |
157
|
|
|
{ |
158
|
|
|
$this->starts = [ |
159
|
|
|
'wt' => \microtime(true), |
160
|
|
|
'mu' => \memory_get_usage(), |
161
|
|
|
'pmu' => \memory_get_peak_usage(), |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Stops the profiling. |
167
|
|
|
*/ |
168
|
|
|
public function leave(): void |
169
|
|
|
{ |
170
|
|
|
$this->ends = [ |
171
|
|
|
'wt' => \microtime(true), |
172
|
|
|
'mu' => \memory_get_usage(), |
173
|
|
|
'pmu' => \memory_get_peak_usage(), |
174
|
|
|
]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function reset(): void |
178
|
|
|
{ |
179
|
|
|
$this->starts = $this->ends = $this->profiles = []; |
180
|
|
|
$this->enter(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function getIterator(): Traversable |
187
|
|
|
{ |
188
|
|
|
return new ArrayIterator($this->profiles); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|