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
|
|
|
/** @var string */ |
32
|
|
|
private $template; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
private $name; |
36
|
|
|
|
37
|
|
|
/** @var array<string,int|float> */ |
38
|
|
|
private $starts = []; |
39
|
|
|
|
40
|
|
|
/** @var array<string,int|float> */ |
41
|
|
|
private $ends = []; |
42
|
|
|
|
43
|
|
|
/** @var Profile[] */ |
44
|
|
|
private $profiles = []; |
45
|
|
|
|
46
|
|
|
public function __construct(string $template = 'main', string $name = 'main') |
47
|
|
|
{ |
48
|
|
|
$this->template = $template; |
49
|
|
|
$this->name = $name; |
50
|
|
|
$this->enter(); |
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->template; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Profile[] |
65
|
|
|
*/ |
66
|
|
|
public function getProfiles(): array |
67
|
|
|
{ |
68
|
|
|
return $this->profiles; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Profile $profile |
73
|
|
|
*/ |
74
|
|
|
public function addProfile(self $profile): void |
75
|
|
|
{ |
76
|
|
|
$this->profiles[] = $profile; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the duration in microseconds. |
81
|
|
|
* |
82
|
|
|
* @return float |
83
|
|
|
*/ |
84
|
|
|
public function getDuration(): float |
85
|
|
|
{ |
86
|
|
|
if ($this->profiles) { |
|
|
|
|
87
|
|
|
// for the root node with children, duration is the sum of all child durations |
88
|
|
|
$duration = 0; |
89
|
|
|
|
90
|
|
|
foreach ($this->profiles as $profile) { |
91
|
|
|
$duration += $profile->getDuration(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $duration; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the memory usage in bytes. |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function getMemoryUsage(): int |
106
|
|
|
{ |
107
|
|
|
return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the peak memory usage in bytes. |
112
|
|
|
* |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
public function getPeakMemoryUsage(): int |
116
|
|
|
{ |
117
|
|
|
return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Starts the profiling. |
122
|
|
|
*/ |
123
|
|
|
public function enter(): void |
124
|
|
|
{ |
125
|
|
|
$this->starts = [ |
126
|
|
|
'wt' => \microtime(true), |
127
|
|
|
'mu' => \memory_get_usage(), |
128
|
|
|
'pmu' => \memory_get_peak_usage(), |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Stops the profiling. |
134
|
|
|
* |
135
|
|
|
* @return static |
136
|
|
|
*/ |
137
|
|
|
public function leave(): self |
138
|
|
|
{ |
139
|
|
|
$this->ends = [ |
140
|
|
|
'wt' => \microtime(true), |
141
|
|
|
'mu' => \memory_get_usage(), |
142
|
|
|
'pmu' => \memory_get_peak_usage(), |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function reset(): void |
149
|
|
|
{ |
150
|
|
|
$this->starts = $this->ends = $this->profiles = []; |
151
|
|
|
$this->enter(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function getIterator(): Traversable |
155
|
|
|
{ |
156
|
|
|
return new ArrayIterator($this->profiles); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.