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