-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathTimeDataCollector.php
279 lines (255 loc) · 7.21 KB
/
TimeDataCollector.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar\DataCollector;
use DebugBar\DebugBarException;
/**
* Collects info about the request duration as well as providing
* a way to log duration of any operations
*/
class TimeDataCollector extends DataCollector implements Renderable
{
/**
* @var float
*/
protected $requestStartTime;
/**
* @var float
*/
protected $requestEndTime;
/**
* @var array
*/
protected $startedMeasures = array();
/**
* @var array
*/
protected $measures = array();
/**
* @var bool
*/
protected $memoryMeasure = false;
/**
* @param float $requestStartTime
*/
public function __construct($requestStartTime = null)
{
if ($requestStartTime === null) {
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
$requestStartTime = $_SERVER['REQUEST_TIME_FLOAT'];
} else {
$requestStartTime = microtime(true);
}
}
$this->requestStartTime = (float)$requestStartTime;
static::getDefaultDataFormatter(); // initializes formatter for lineal timeline
}
/**
* Starts memory measuring
*/
public function showMemoryUsage()
{
$this->memoryMeasure = true;
}
/**
* Starts a measure
*
* @param string $name Internal name, used to stop the measure
* @param string|null $label Public name
* @param string|null $collector The source of the collector
* @param string|null $group The group for aggregates
*/
public function startMeasure($name, $label = null, $collector = null, $group = null)
{
$start = microtime(true);
$this->startedMeasures[$name] = array(
'label' => $label ?: $name,
'start' => $start,
'memory' => $this->memoryMeasure ? memory_get_usage(false) : null,
'collector' => $collector,
'group' => $group,
);
}
/**
* Check a measure exists
*
* @param string $name
* @return bool
*/
public function hasStartedMeasure($name)
{
return isset($this->startedMeasures[$name]);
}
/**
* Stops a measure
*
* @param string $name
* @param array $params
* @throws DebugBarException
*/
public function stopMeasure($name, $params = array())
{
$end = microtime(true);
if (!$this->hasStartedMeasure($name)) {
throw new DebugBarException("Failed stopping measure '$name' because it hasn't been started");
}
if (! is_null($this->startedMeasures[$name]['memory'])) {
$params['memoryUsage'] = memory_get_usage(false) - $this->startedMeasures[$name]['memory'];
}
$this->addMeasure(
$this->startedMeasures[$name]['label'],
$this->startedMeasures[$name]['start'],
$end,
$params,
$this->startedMeasures[$name]['collector'],
$this->startedMeasures[$name]['group']
);
unset($this->startedMeasures[$name]);
}
/**
* Adds a measure
*
* @param string $label
* @param float $start
* @param float $end
* @param array $params
* @param string|null $collector
* @param string|null $group
*/
public function addMeasure($label, $start, $end, $params = array(), $collector = null, $group = null)
{
if (isset($params['memoryUsage'])) {
$memory = $this->memoryMeasure ? $params['memoryUsage'] : 0;
unset($params['memoryUsage']);
}
$this->measures[] = array(
'label' => $label,
'start' => $start,
'relative_start' => $start - $this->requestStartTime,
'end' => $end,
'relative_end' => $end - $this->requestEndTime,
'duration' => $end - $start,
'duration_str' => $this->getDataFormatter()->formatDuration($end - $start),
'memory' => $memory ?? 0,
'memory_str' => $this->getDataFormatter()->formatBytes($memory ?? 0),
'params' => $params,
'collector' => $collector,
'group' => $group,
);
}
/**
* Utility function to measure the execution of a Closure
*
* @param string $label
* @param \Closure $closure
* @param string|null $collector
* @return mixed
*/
public function measure($label, \Closure $closure, $collector = null)
{
$name = spl_object_hash($closure);
$this->startMeasure($name, $label, $collector);
$result = $closure();
$params = is_array($result) ? $result : array();
$this->stopMeasure($name, $params);
return $result;
}
/**
* Returns an array of all measures
*
* @return array
*/
public function getMeasures()
{
return $this->measures;
}
/**
* Returns the request start time
*
* @return float
*/
public function getRequestStartTime()
{
return $this->requestStartTime;
}
/**
* Returns the request end time
*
* @return float
*/
public function getRequestEndTime()
{
return $this->requestEndTime;
}
/**
* Returns the duration of a request
*
* @return float
*/
public function getRequestDuration()
{
if ($this->requestEndTime !== null) {
return $this->requestEndTime - $this->requestStartTime;
}
return microtime(true) - $this->requestStartTime;
}
/**
* @return array
* @throws DebugBarException
*/
public function collect()
{
$this->requestEndTime = microtime(true);
foreach (array_keys($this->startedMeasures) as $name) {
$this->stopMeasure($name);
}
usort($this->measures, function($a, $b) {
if ($a['start'] == $b['start']) {
return 0;
}
return $a['start'] < $b['start'] ? -1 : 1;
});
return array(
'count' => count($this->measures),
'start' => $this->requestStartTime,
'end' => $this->requestEndTime,
'duration' => $this->getRequestDuration(),
'duration_str' => $this->getDataFormatter()->formatDuration($this->getRequestDuration()),
'measures' => array_values($this->measures)
);
}
/**
* @return string
*/
public function getName()
{
return 'time';
}
/**
* @return array
*/
public function getWidgets()
{
return array(
"time" => array(
"icon" => "clock-o",
"tooltip" => "Request Duration",
"map" => "time.duration_str",
'link' => 'timeline',
"default" => "'0ms'"
),
"timeline" => array(
"icon" => "tasks",
"widget" => "PhpDebugBar.Widgets.TimelineWidget",
"map" => "time",
"default" => "{}"
)
);
}
}