1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Gear\Profiler\DataStructures; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Metric |
20
|
|
|
* |
21
|
|
|
* @package O2System\Gear\Profiler\DataStructures |
22
|
|
|
*/ |
23
|
|
|
class Metric |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Metric::$marker |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $marker; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Metric::$startTime |
34
|
|
|
* |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
public $startTime; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Metric::$endTime |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
public $endTime; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Metric::$startMemory |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
public $startMemory; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Metric::$endMemory |
55
|
|
|
* |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
public $endMemory; |
59
|
|
|
|
60
|
|
|
// ------------------------------------------------------------------------ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Metric::__construct |
64
|
|
|
* |
65
|
|
|
* @param string $marker |
66
|
|
|
*/ |
67
|
|
|
public function __construct($marker) |
68
|
|
|
{ |
69
|
|
|
$this->marker = $marker; |
70
|
|
|
|
71
|
|
|
$this->start(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// ------------------------------------------------------------------------ |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Metric::start |
78
|
|
|
* |
79
|
|
|
* @param int|null $startTime |
80
|
|
|
* @param int|null $startMemory |
81
|
|
|
*/ |
82
|
|
|
public function start($startTime = null, $startMemory = null) |
83
|
|
|
{ |
84
|
|
|
$this->startTime = isset($startTime) ? $startTime : microtime(true); |
85
|
|
|
$this->startMemory = isset($startMemory) ? $startMemory : memory_get_usage(true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// ------------------------------------------------------------------------ |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Metric::getStartTime |
92
|
|
|
* |
93
|
|
|
* @param int $precision |
94
|
|
|
* @param int $floatingPrecision |
95
|
|
|
* @param bool $showUnit |
96
|
|
|
* |
97
|
|
|
* @return float|string |
98
|
|
|
*/ |
99
|
|
|
public function getStartTime($precision = 0, $floatingPrecision = 3, $showUnit = true) |
100
|
|
|
{ |
101
|
|
|
return $this->getFormattedTime($this->startTime, $precision, $floatingPrecision, $showUnit); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// ------------------------------------------------------------------------ |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Metric::getFormattedTime |
108
|
|
|
* |
109
|
|
|
* @param int $time |
110
|
|
|
* @param int $precision |
111
|
|
|
* @param int $floatingPrecision |
112
|
|
|
* @param bool $showUnit |
113
|
|
|
* |
114
|
|
|
* @return float|string |
115
|
|
|
*/ |
116
|
|
|
public function getFormattedTime( |
117
|
|
|
$time, |
118
|
|
|
$precision = 0, |
119
|
|
|
$floatingPrecision = 3, |
120
|
|
|
$showUnit = true |
121
|
|
|
) { |
122
|
|
|
|
123
|
|
|
$test = is_int( |
124
|
|
|
$precision |
125
|
|
|
) && $precision >= 0 && $precision <= 2 && |
126
|
|
|
is_float($time) && |
|
|
|
|
127
|
|
|
is_int($floatingPrecision) && $floatingPrecision >= 0 && |
128
|
|
|
is_bool($showUnit); |
129
|
|
|
|
130
|
|
|
if ($test) { |
|
|
|
|
131
|
|
|
$duration = round($time * 10 * ($precision * 3), $floatingPrecision); |
132
|
|
|
|
133
|
|
|
if ($showUnit) { |
134
|
|
|
switch ($precision) { |
135
|
|
|
case 0 : |
136
|
|
|
return $duration . ' s'; |
137
|
|
|
case 1 : |
138
|
|
|
return $duration . ' ms'; |
139
|
|
|
case 2 : |
140
|
|
|
return $duration . ' µs'; |
141
|
|
|
default : |
142
|
|
|
return $duration . ' (no unit)'; |
143
|
|
|
} |
144
|
|
|
} else { |
145
|
|
|
return $duration; |
146
|
|
|
} |
147
|
|
|
} else { |
148
|
|
|
return 'Can\'t return the render time'; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// ------------------------------------------------------------------------ |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Metric::getEndTime |
156
|
|
|
* |
157
|
|
|
* @param int $precision |
158
|
|
|
* @param int $floatingPrecision |
159
|
|
|
* @param bool $showUnit |
160
|
|
|
* |
161
|
|
|
* @return float|string |
162
|
|
|
*/ |
163
|
|
|
public function getEndTime($precision = 0, $floatingPrecision = 3, $showUnit = true) |
164
|
|
|
{ |
165
|
|
|
if (empty($this->endTime)) { |
166
|
|
|
$this->stop(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->getFormattedTime($this->endTime, $precision, $floatingPrecision, $showUnit); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// ------------------------------------------------------------------------ |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Metric::stop |
176
|
|
|
* |
177
|
|
|
* @return static |
178
|
|
|
*/ |
179
|
|
|
public function stop() |
180
|
|
|
{ |
181
|
|
|
$this->endTime = microtime(true); |
182
|
|
|
$this->endMemory = memory_get_peak_usage(true); |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// ------------------------------------------------------------------------ |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Metric::getDuration |
191
|
|
|
* |
192
|
|
|
* @param int $precision |
193
|
|
|
* @param int $floatingPrecision |
194
|
|
|
* @param bool $showUnit |
195
|
|
|
* |
196
|
|
|
* @return float|string |
197
|
|
|
*/ |
198
|
|
|
public function getDuration($precision = 0, $floatingPrecision = 3, $showUnit = true) |
199
|
|
|
{ |
200
|
|
|
if (empty($this->endTime)) { |
201
|
|
|
$this->stop(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this->getFormattedTime($this->endTime - $this->startTime, $precision, $floatingPrecision, $showUnit); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// ------------------------------------------------------------------------ |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Metric::getStartMemory |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getStartMemory() |
215
|
|
|
{ |
216
|
|
|
return $this->getFormattedMemorySize($this->startMemory); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// ------------------------------------------------------------------------ |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Metric::getFormattedMemorySize |
223
|
|
|
* |
224
|
|
|
* @param int $size |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
public function getFormattedMemorySize($size) |
229
|
|
|
{ |
230
|
|
|
if ($size < 1024) { |
231
|
|
|
return $size . " bytes"; |
232
|
|
|
} elseif ($size < 1048576) { |
233
|
|
|
return round($size / 1024, 2) . " kb"; |
234
|
|
|
} else { |
235
|
|
|
return round($size / 1048576, 2) . " mb"; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
// ------------------------------------------------------------------------ |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Metric::getMemoryUsage |
243
|
|
|
* |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public function getMemoryUsage() |
247
|
|
|
{ |
248
|
|
|
if (empty($this->endMemory)) { |
249
|
|
|
$this->stop(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $this->getFormattedMemorySize($this->endMemory - $this->startMemory); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// ------------------------------------------------------------------------ |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Metric::getMemory |
259
|
|
|
* |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
public function getMemory() |
263
|
|
|
{ |
264
|
|
|
return $this->getEndMemory(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// ------------------------------------------------------------------------ |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Metric::getEndMemory |
271
|
|
|
* |
272
|
|
|
* @return string |
273
|
|
|
*/ |
274
|
|
|
public function getEndMemory() |
275
|
|
|
{ |
276
|
|
|
return $this->getFormattedMemorySize($this->endMemory); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
// ------------------------------------------------------------------------ |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Metric::getPeakMemoryUsage |
283
|
|
|
* |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
public function getPeakMemoryUsage() |
287
|
|
|
{ |
288
|
|
|
return $this->getFormattedMemorySize(memory_get_peak_usage(true)); |
289
|
|
|
} |
290
|
|
|
} |