|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\BlackBar\Modules; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\BlackBar\Application; |
|
6
|
|
|
|
|
7
|
|
|
class Profiler implements Module |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Application |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $app; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var float |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $noise; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var float |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $start; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var float |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $stop; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $timer; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $timers; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(Application $app) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->app = $app; |
|
37
|
|
|
$this->noise = floatval(0); |
|
38
|
|
|
$this->start = floatval(0); |
|
39
|
|
|
$this->stop = floatval(0); |
|
40
|
|
|
$this->timer = []; |
|
41
|
|
|
$this->timers = []; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function entries(): array |
|
45
|
|
|
{ |
|
46
|
|
|
$entries = []; |
|
47
|
|
|
foreach ($this->timers as $timer) { |
|
48
|
|
|
$timer['memory'] = round($timer['memory'] / 1000); // convert bytes to KB |
|
49
|
|
|
$timer['time'] = $this->formatTime($timer['time']); |
|
50
|
|
|
$entries[] = $timer; |
|
51
|
|
|
} |
|
52
|
|
|
return $entries; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function hasEntries(): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return !empty($this->timers); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function id(): string |
|
61
|
|
|
{ |
|
62
|
|
|
return 'glbb-profiler'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function isVisible(): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return !empty($this->timers); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function label(): string |
|
71
|
|
|
{ |
|
72
|
|
|
$label = __('Profiler', 'blackbar'); |
|
73
|
|
|
$time = $this->totalTime(); |
|
74
|
|
|
if ($time > 0) { |
|
75
|
|
|
$info = sprintf('<span class="glbb-link-info">(%s)</span>', $this->formatTime($time)); |
|
76
|
|
|
$label = sprintf('%s %s', $label, $info); |
|
77
|
|
|
} |
|
78
|
|
|
return $label; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function render(): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->app->render('panels/profiler', ['profiler' => $this]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function setNoise(float $microtime): void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->noise = $this->start - $microtime; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function setStart(float $microtime): void |
|
92
|
|
|
{ |
|
93
|
|
|
$this->start = $microtime; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function setStop(float $microtime): void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->stop = $microtime; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function start(string $name): void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->timer = [ |
|
104
|
|
|
'memory' => memory_get_peak_usage(), |
|
105
|
|
|
'name' => $name, |
|
106
|
|
|
'start' => microtime(true), |
|
107
|
|
|
'stop' => floatval(0), |
|
108
|
|
|
'time' => floatval(0), |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function stop(string $name): void |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
if (empty($this->timer)) { |
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
$this->timer['stop'] = microtime(true) - $this->noise; |
|
118
|
|
|
$this->timer['time'] = $this->timer['stop'] - $this->timer['start']; |
|
119
|
|
|
$this->timers[] = $this->timer; |
|
120
|
|
|
$this->timer = []; // reset timer |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function totalTime(): float |
|
124
|
|
|
{ |
|
125
|
|
|
return array_sum(wp_list_pluck($this->timers, 'time')); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function formatTime(float $time): string |
|
129
|
|
|
{ |
|
130
|
|
|
if ($time < 100) { |
|
131
|
|
|
return sprintf('%.3f µs', $time); |
|
132
|
|
|
} |
|
133
|
|
|
return sprintf('%.3f ms', $time * 1000); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.