|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\BlackBar\Modules; |
|
4
|
|
|
|
|
5
|
|
|
class Profiler extends Module |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var int |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $memory_start = 0; |
|
11
|
|
|
/** |
|
12
|
|
|
* @var int |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $memory_stop = 0; |
|
15
|
|
|
/** |
|
16
|
|
|
* The profiler noise to remove from the timer (in nanoseconds). |
|
17
|
|
|
* @var int |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $noise = 0; |
|
20
|
|
|
/** |
|
21
|
|
|
* The hrtime the profiler started measuring (in nanoseconds). |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $start = 0; |
|
25
|
|
|
/** |
|
26
|
|
|
* The hrtime the profiler stopped measuring (in nanoseconds). |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $stop = 0; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $timer = []; |
|
34
|
|
|
|
|
35
|
|
|
public function entries(): array |
|
36
|
|
|
{ |
|
37
|
|
|
$entries = []; |
|
38
|
|
|
foreach ($this->entries as $entry) { |
|
39
|
|
|
$entry['time'] = $this->formatTime($entry['time']); |
|
40
|
|
|
$entries[] = $entry; |
|
41
|
|
|
} |
|
42
|
|
|
return $entries; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function isVisible(): bool |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->hasEntries(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function label(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return __('Profiler', 'blackbar'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function setNoise(int $nanoseconds): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->noise = $nanoseconds - $this->start; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function setStart(int $nanoseconds): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->start = $nanoseconds; |
|
63
|
|
|
$this->memory_start = memory_get_peak_usage(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function setStop(int $nanoseconds): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->stop = $nanoseconds; |
|
69
|
|
|
$this->memory_stop = memory_get_peak_usage(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function start(string $name): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->timer = [ |
|
75
|
|
|
'memory' => memory_get_peak_usage(), |
|
76
|
|
|
'name' => $name, |
|
77
|
|
|
'start' => (int) hrtime(true), |
|
78
|
|
|
'stop' => 0, |
|
79
|
|
|
'time' => 0, |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function stop(): void |
|
84
|
|
|
{ |
|
85
|
|
|
if (!empty($this->timer)) { |
|
86
|
|
|
$nanoseconds = (int) hrtime(true); |
|
87
|
|
|
$this->timer['memory'] = max(0, memory_get_peak_usage() - $this->timer['memory']); |
|
88
|
|
|
$this->timer['stop'] = $nanoseconds; |
|
89
|
|
|
$this->timer['time'] = max(0, $nanoseconds - $this->timer['start'] - $this->noise); |
|
90
|
|
|
$this->entries[] = $this->timer; |
|
91
|
|
|
$this->timer = []; // reset timer |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|