-
Notifications
You must be signed in to change notification settings - Fork 6
/
Profiler.php
253 lines (215 loc) · 7.71 KB
/
Profiler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace TestTools\Profiler;
/**
* Lightweight profiler featuring low performance overhead and runtime aggregation
*
* @author Michael Mayer <michael@lastzero.net>
* @package TestTools
* @license MIT
*/
class Profiler
{
protected static $steps = array();
protected static $aggregated = array();
public static $labelWidth = 100;
/**
* Start profiling
*
* @param string $label
*/
public static function start($label = 'Start')
{
self::addStep($label);
}
/**
* End profiling
*/
public static function stop()
{
self::addStep('Done');
}
/**
* Add profiling step (split time)
*
* @param string $label
* @param bool $silent Don't output this step (see getResultAsTable())
*/
public static function addStep($label = '', $silent = false)
{
$diff = 0;
$total = 0;
$time = microtime(true);
if (count(self::$steps) != 0) {
$lastStep = end(self::$steps);
$diff = $time - $lastStep['time'];
$total = $lastStep['total'] + $diff;
}
self::$steps[] = array('label' => $label, 'time' => $time, 'diff' => $diff, 'total' => $total, 'silent' => $silent);
}
/**
* Add step without displaying the time (see getResultAsTable())
*
* @param string $label
*/
public static function addSilentStep($label = '')
{
self::addStep($label, true);
}
/**
* Start aggregation
*
* @param string $label
* @param string $sublabel
*/
public static function startAggregate($label, $sublabel = '')
{
$time = microtime(true);
if (empty(self::$aggregated[$label])) {
self::$aggregated[$label] = array('total' => 0, 'start' => $time, 'count' => 0, 'sub' => array());
} else {
self::$aggregated[$label]['start'] = $time;
}
if ($sublabel) {
if (empty(self::$aggregated[$label]['sub'][$sublabel])) {
self::$aggregated[$label]['sub'][$sublabel] = array('total' => 0, 'start' => $time, 'count' => 0);
} else {
self::$aggregated[$label]['sub'][$sublabel]['start'] = $time;
}
}
}
/**
* Stop aggregation
*
* @param $label
* @param string $sublabel
*/
public static function stopAggregate($label, $sublabel = '')
{
$time = microtime(true);
if (!empty(self::$aggregated[$label]) && self::$aggregated[$label]['start'] > 0) {
self::$aggregated[$label]['total'] += $time - self::$aggregated[$label]['start'];
self::$aggregated[$label]['start'] = 0;
self::$aggregated[$label]['count']++;
if ($sublabel && !empty(self::$aggregated[$label]['sub'][$sublabel]) && self::$aggregated[$label]['sub'][$sublabel]['start'] > 0) {
self::$aggregated[$label]['sub'][$sublabel]['total'] += $time - self::$aggregated[$label]['sub'][$sublabel]['start'];
self::$aggregated[$label]['sub'][$sublabel]['start'] = 0;
self::$aggregated[$label]['sub'][$sublabel]['count']++;
}
}
}
/**
* Clear all results
*/
public static function clear()
{
self::$steps = array();
self::$aggregated = array();
}
protected static function stringPad($input, $pad_length, $pad_string = ' ', $pad_style = STR_PAD_RIGHT, $encoding = 'UTF-8')
{
return str_pad($input, strlen($input) - mb_strlen($input, $encoding) + $pad_length, $pad_string, $pad_style);
}
protected static function padLabel($label, $width)
{
$result = substr(trim(preg_replace('!\s+!', ' ', strtr($label, array("\n" => ' ', "\t" => ' ')))), 0, $width);
$result = self::stringPad($result, $width);
return $result;
}
protected static function padSublabel($sublabel, $width)
{
$result = substr(trim(preg_replace('!\s+!', ' ', strtr($sublabel, array("\n" => ' ', "\t" => ' ')))), 0, $width - 2);
$result = self::stringPad(' ' . $result, $width);
return $result;
}
/**
* Return profiling results as plain text table
*
* @return string
*/
public static function getResultAsTable()
{
$labelWidth = self::$labelWidth;
$result = str_pad('Step', $labelWidth) . ' '
. str_pad('Total (ms)', 10, ' ', STR_PAD_LEFT) . ' '
. str_pad('Diff (ms)', 10, ' ', STR_PAD_LEFT) . ' '
. str_pad('Diff (%)', 10, ' ', STR_PAD_LEFT) . "\n";
$lastStep = end(self::$steps);
$totalTime = $lastStep['total'];
foreach (self::$steps as $step) {
if ($step['silent']) continue;
$result .= self::padLabel($step['label'], $labelWidth) . ' '
. str_pad(round($step['total'] * 1000), 10, ' ', STR_PAD_LEFT) . ' '
. str_pad(number_format($step['diff'] * 1000, 1), 10, ' ', STR_PAD_LEFT) . ' '
. str_pad(number_format(($step['diff'] / $totalTime) * 100, 1), 10, ' ', STR_PAD_LEFT) . "\n";
}
if (count(self::$aggregated) > 0) {
ksort(self::$aggregated);
$result .= "\n" . str_pad('Aggregated', $labelWidth) . ' '
. str_pad('Count', 10, ' ', STR_PAD_LEFT) . ' '
. str_pad('Time (ms)', 10, ' ', STR_PAD_LEFT) . ' '
. str_pad('Time (%)', 10, ' ', STR_PAD_LEFT) . "\n";
foreach (self::$aggregated as $label => $values) {
if ($values['count'] == 0) continue;
$result .= self::padLabel($label, $labelWidth) . ' '
. str_pad($values['count'], 10, ' ', STR_PAD_LEFT) . ' '
. str_pad(number_format($values['total'] * 1000, 1), 10, ' ', STR_PAD_LEFT) . ' '
. str_pad(number_format(($values['total'] / $totalTime) * 100, 1), 10, ' ', STR_PAD_LEFT) . "\n";
ksort($values['sub']);
foreach ($values['sub'] as $sublabel => $subvalues) {
$result .= self::padSublabel($sublabel, $labelWidth) . ' '
. str_pad($subvalues['count'], 10, ' ', STR_PAD_LEFT) . ' '
. str_pad(number_format($subvalues['total'] * 1000, 1), 10, ' ', STR_PAD_LEFT) . ' '
. str_pad(number_format(($subvalues['total'] / $totalTime) * 100, 1), 10, ' ', STR_PAD_LEFT) . "\n";
}
}
}
return $result;
}
/**
* Append profiling results to file (as plain text table)
*
* @param $filename
*/
public static function appendResultToFile($filename)
{
$result = self::getResultAsTable();
file_put_contents($filename, $result, FILE_APPEND);
}
/**
* Write profiling results to new file (as plain text table)
*
* @param $filename
*/
public static function writeResultToFile($filename)
{
$result = self::getResultAsTable();
file_put_contents($filename, $result);
}
/**
* Determine caller function using debug_backtrace()
*
* @param int $level
* @return mixed
*/
public static function getCallerFunction($level = 2)
{
$callers = debug_backtrace();
return $callers[$level]['function'];
}
/**
* Determine caller method using debug_backtrace()
*
* @param int $level
* @return mixed
*/
public static function getCallerMethod($level = 2)
{
$callers = debug_backtrace();
if (isset($callers[$level]['class'])) {
$class = $callers[$level]['class'] . '::';
} else {
$class = 'function ';
}
return $class . $callers[$level]['function'];
}
}