1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.2 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 Biurad\UI; |
19
|
|
|
|
20
|
|
|
use ArrayIterator; |
21
|
|
|
use IteratorAggregate; |
22
|
|
|
use Traversable; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Fabien Potencier <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class Profile implements IteratorAggregate |
28
|
|
|
{ |
29
|
|
|
public const TEMPLATE = 'template'; |
30
|
|
|
|
31
|
|
|
private $template; |
32
|
|
|
|
33
|
|
|
private $name; |
34
|
|
|
|
35
|
|
|
private $starts = []; |
36
|
|
|
|
37
|
|
|
private $ends = []; |
38
|
|
|
|
39
|
|
|
private $profiles = []; |
40
|
|
|
|
41
|
|
|
public function __construct(string $template = 'main', string $name = 'main') |
42
|
|
|
{ |
43
|
|
|
$this->template = $template; |
44
|
|
|
$this->name = $name; |
45
|
|
|
$this->enter(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getTemplate(): string |
49
|
|
|
{ |
50
|
|
|
return $this->template; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getName(): string |
54
|
|
|
{ |
55
|
|
|
return $this->name; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function isTemplate(): bool |
59
|
|
|
{ |
60
|
|
|
return self::TEMPLATE === $this->type; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Profile[] |
65
|
|
|
*/ |
66
|
|
|
public function getProfiles(): array |
67
|
|
|
{ |
68
|
|
|
return $this->profiles; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function addProfile(self $profile): void |
72
|
|
|
{ |
73
|
|
|
$this->profiles[] = $profile; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the duration in microseconds. |
78
|
|
|
* |
79
|
|
|
* @return float |
80
|
|
|
*/ |
81
|
|
|
public function getDuration(): float |
82
|
|
|
{ |
83
|
|
|
if ($this->profiles) { |
|
|
|
|
84
|
|
|
// for the root node with children, duration is the sum of all child durations |
85
|
|
|
$duration = 0; |
86
|
|
|
|
87
|
|
|
foreach ($this->profiles as $profile) { |
88
|
|
|
$duration += $profile->getDuration(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $duration; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the memory usage in bytes. |
99
|
|
|
* |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
public function getMemoryUsage(): int |
103
|
|
|
{ |
104
|
|
|
return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the peak memory usage in bytes. |
109
|
|
|
* |
110
|
|
|
* @return int |
111
|
|
|
*/ |
112
|
|
|
public function getPeakMemoryUsage(): int |
113
|
|
|
{ |
114
|
|
|
return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Starts the profiling. |
119
|
|
|
*/ |
120
|
|
|
public function enter(): void |
121
|
|
|
{ |
122
|
|
|
$this->starts = [ |
123
|
|
|
'wt' => \microtime(true), |
124
|
|
|
'mu' => \memory_get_usage(), |
125
|
|
|
'pmu' => \memory_get_peak_usage(), |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Stops the profiling. |
131
|
|
|
* |
132
|
|
|
* @return static |
133
|
|
|
*/ |
134
|
|
|
public function leave(): self |
135
|
|
|
{ |
136
|
|
|
$this->ends = [ |
137
|
|
|
'wt' => \microtime(true), |
138
|
|
|
'mu' => \memory_get_usage(), |
139
|
|
|
'pmu' => \memory_get_peak_usage(), |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function reset(): void |
146
|
|
|
{ |
147
|
|
|
$this->starts = $this->ends = $this->profiles = []; |
148
|
|
|
$this->enter(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getIterator(): Traversable |
152
|
|
|
{ |
153
|
|
|
return new ArrayIterator($this->profiles); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|