|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package toolkit |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* The Profiler class tracks various performance metrics while a Symphony |
|
9
|
|
|
* page is being generated. It provides a basic stopwatch functionality and |
|
10
|
|
|
* memory usage statistics. Profiling occurs in both the Frontend and |
|
11
|
|
|
* Administration execution. The Profiler implements the Singleton interface. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Profiler implements Singleton |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Holds the timestamp from when the profiler was first initialised |
|
17
|
|
|
* |
|
18
|
|
|
* @var integer |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $_starttime = 0; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* An array of arrays containing profiling samples. A record contains a |
|
24
|
|
|
* profile message, the time since `$_starttime` timestamp, the end timestamp, |
|
25
|
|
|
* the group for this record, the number of SQL queries and the result of |
|
26
|
|
|
* memory_get_usage() |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected static $_samples = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* A seed holds a start time to be used in profiling. If this is not null |
|
34
|
|
|
* the profiler will use this as the start time instead of `$_starttime`. This |
|
35
|
|
|
* is set with the seed function. |
|
36
|
|
|
* |
|
37
|
|
|
* @var integer |
|
38
|
|
|
* @see seed() |
|
39
|
|
|
*/ |
|
40
|
|
|
protected static $_seed = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* An instance of the Profiler class |
|
44
|
|
|
* |
|
45
|
|
|
* @var Profiler |
|
46
|
|
|
*/ |
|
47
|
|
|
protected static $_instance = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The constructor for the profile function sets the start time |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function __construct() |
|
53
|
|
|
{ |
|
54
|
|
|
Profiler::$_starttime = precision_timer(); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns the Profiler instance, creating one if it does not exist |
|
59
|
|
|
* |
|
60
|
|
|
* @return Profiler |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function instance() |
|
63
|
|
|
{ |
|
64
|
|
|
if (!(Profiler::$_instance instanceof Profiler)) { |
|
65
|
|
|
Profiler::$_instance = new self; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return Profiler::$_instance; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Sets the seed to be a timestamp so that time profiling will use this |
|
73
|
|
|
* as a starting point |
|
74
|
|
|
* |
|
75
|
|
|
* @param integer $time |
|
76
|
|
|
* The time in seconds |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function seed($time = null) |
|
79
|
|
|
{ |
|
80
|
|
|
Profiler::$_seed = (is_null($time)) ? precision_timer() : $time; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* This function creates a new report in the `$_samples` array where the message |
|
85
|
|
|
* is the name of this report. By default, all samples are compared to the `$_starttime` |
|
86
|
|
|
* but if the `PROFILE_LAP` constant is passed, it will be compared to specific `$_seed` |
|
87
|
|
|
* timestamp. Samples can grouped by type (ie. Datasources, Events), but by default |
|
88
|
|
|
* are grouped by 'General'. Optionally, the number of SQL queries that have occurred |
|
89
|
|
|
* since either `$_starttime` or `$_seed` can be passed. Memory usage is taken with each |
|
90
|
|
|
* sample which measures the amount of memory used by this script by PHP at the |
|
91
|
|
|
* time of sampling. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $msg |
|
94
|
|
|
* A description for this sample |
|
95
|
|
|
* @param integer $type |
|
96
|
|
|
* Either `PROFILE_RUNNING_TOTAL` or `PROFILE_LAP` |
|
97
|
|
|
* @param string $group |
|
98
|
|
|
* Allows samples to be grouped together, defaults to General. |
|
99
|
|
|
* @param integer $queries |
|
100
|
|
|
* The number of MySQL queries that occurred since the `$_starttime` or `$_seed` |
|
101
|
|
|
*/ |
|
102
|
|
|
public function sample($msg, $type = PROFILE_RUNNING_TOTAL, $group = 'General', $queries = null) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($type === PROFILE_RUNNING_TOTAL) { |
|
105
|
|
|
Profiler::$_samples[] = array( |
|
106
|
|
|
$msg, |
|
107
|
|
|
precision_timer('stop', Profiler::$_starttime), |
|
108
|
|
|
precision_timer(), |
|
109
|
|
|
$group, |
|
110
|
|
|
$queries, |
|
111
|
|
|
memory_get_usage() |
|
112
|
|
|
); |
|
113
|
|
|
} else { |
|
114
|
|
|
if (!is_null(Profiler::$_seed)) { |
|
115
|
|
|
$start = Profiler::$_seed; |
|
116
|
|
|
Profiler::$_seed = null; |
|
117
|
|
|
} else { |
|
118
|
|
|
$start = null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$prev = Profiler::retrieveLast(); |
|
122
|
|
|
Profiler::$_samples[] = array( |
|
123
|
|
|
$msg, |
|
124
|
|
|
precision_timer('stop', ($start ? $start : $prev[2])), |
|
125
|
|
|
precision_timer(), |
|
126
|
|
|
$group, |
|
127
|
|
|
$queries, |
|
128
|
|
|
memory_get_usage() |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns the last record from the `$_records` array |
|
135
|
|
|
* |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function retrieveLast() |
|
139
|
|
|
{ |
|
140
|
|
|
return end(Profiler::$_samples); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Returns a sample by message, if no sample is found, an empty |
|
145
|
|
|
* array is returned |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $msg |
|
148
|
|
|
* The name of the sample to return |
|
149
|
|
|
* @return array |
|
150
|
|
|
*/ |
|
151
|
|
|
public function retrieveByMessage($msg) |
|
152
|
|
|
{ |
|
153
|
|
|
foreach (Profiler::$_samples as $record) { |
|
154
|
|
|
if ($record[0] === $msg) { |
|
155
|
|
|
return $record; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return array(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Returns all the samples that belong to a particular group. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $group |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
|
|
public function retrieveGroup($group) |
|
169
|
|
|
{ |
|
170
|
|
|
$result = array(); |
|
171
|
|
|
|
|
172
|
|
|
foreach (Profiler::$_samples as $record) { |
|
173
|
|
|
if ($record[3] === $group) { |
|
174
|
|
|
$result[] = $record; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $result; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Returns the difference between when the Profiler was initialised |
|
183
|
|
|
* (aka `$_starttime`) and the last record the Profiler has. |
|
184
|
|
|
* |
|
185
|
|
|
* @return integer |
|
186
|
|
|
*/ |
|
187
|
|
|
public function retrieveTotalRunningTime() |
|
188
|
|
|
{ |
|
189
|
|
|
$last = Profiler::retrieveLast(); |
|
190
|
|
|
|
|
191
|
|
|
return $last[1]; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Returns the total memory usage from all samples taken by comparing |
|
196
|
|
|
* each sample to the base memory sample. |
|
197
|
|
|
* |
|
198
|
|
|
* @return integer |
|
199
|
|
|
* Memory usage in bytes. |
|
200
|
|
|
*/ |
|
201
|
|
|
public function retrieveTotalMemoryUsage() |
|
202
|
|
|
{ |
|
203
|
|
|
$base = $this->retrieve(0); |
|
204
|
|
|
$total = $last = 0; |
|
205
|
|
|
|
|
206
|
|
|
foreach ($this->retrieve() as $item) { |
|
207
|
|
|
$total += max(0, (($item[5] - $base[5]) - $last)); |
|
208
|
|
|
$last = $item[5] - $base[5]; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $total; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Given an index, return the sample at that position otherwise just |
|
216
|
|
|
* return all samples. |
|
217
|
|
|
* |
|
218
|
|
|
* @param integer $index |
|
219
|
|
|
* The array index to return the sample for |
|
220
|
|
|
* @return array |
|
221
|
|
|
* If no `$index` is passed an array of all the sample arrays are returned |
|
222
|
|
|
* otherwise just the sample at the given `$index` will be returned. |
|
223
|
|
|
*/ |
|
224
|
|
|
public function retrieve($index = null) |
|
225
|
|
|
{ |
|
226
|
|
|
return !is_null($index) ? Profiler::$_samples[$index] : Profiler::$_samples; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.