Simplify the nested ifs of Preprocessor_DOM::preprocessToObj()
[lhc/web/wiklou.git] / profileinfo.php
1 <?php
2 /**
3 * Show profiling data.
4 *
5 * Copyright 2005 Kate Turner.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * @file
26 */
27
28 ini_set( 'zlib.output_compression', 'off' );
29
30 $wgEnableProfileInfo = $wgProfileToDatabase = false;
31 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
32 require ( 'core/includes/WebStart.php' );
33 } else {
34 require ( __DIR__ . '/includes/WebStart.php' );
35 }
36
37 header( 'Content-Type: text/html; charset=utf-8' );
38
39 ?>
40 <!DOCTYPE html>
41 <html>
42 <head>
43 <meta charset="UTF-8">
44 <title>Profiling data</title>
45 <style>
46 /* noc.wikimedia.org/base.css */
47
48 * {
49 margin: 0;
50 padding: 0;
51 }
52
53 body {
54 padding: 0.5em 1em;
55 background: #fff;
56 font: 14px/1.6 sans-serif;
57 color: #333;
58 }
59
60 p, ul, ol, table {
61 margin: 0.5em 0;
62 }
63
64 a {
65 color: #0645AD;
66 text-decoration: none;
67 }
68
69 a:hover {
70 text-decoration: underline;
71 }
72
73 /*!
74 * Bootstrap v2.1.1
75 *
76 * Copyright 2012 Twitter, Inc
77 * Licensed under the Apache License v2.0
78 * http://www.apache.org/licenses/LICENSE-2.0
79 *
80 * Designed and built with all the love in the world @twitter by @mdo and @fat.
81 */
82
83 table {
84 max-width: 100%;
85 background-color: transparent;
86 border-collapse: collapse;
87 border-spacing: 0;
88 }
89
90 .table {
91 width: 100%;
92 margin-bottom: 20px;
93 }
94
95 .table th,
96 .table td {
97 padding: 0.1em;
98 text-align: left;
99 vertical-align: top;
100 border-top: 1px solid #ddd;
101 }
102
103 .table th {
104 font-weight: bold;
105 }
106
107 .table thead th {
108 vertical-align: bottom;
109 }
110
111 .table thead:first-child tr:first-child th,
112 .table thead:first-child tr:first-child td {
113 border-top: 0;
114 }
115
116 .table tbody + tbody {
117 border-top: 2px solid #ddd;
118 }
119
120 .table-condensed th,
121 .table-condensed td {
122 padding: 4px 5px;
123 }
124
125 .table-striped tbody tr:nth-child(odd) td,
126 .table-striped tbody tr:nth-child(odd) th {
127 background-color: #f9f9f9;
128 }
129
130 .table-hover tbody tr:hover td,
131 .table-hover tbody tr:hover th {
132 background-color: #f5f5f5;
133 }
134
135 hr {
136 margin: 20px 0;
137 border: 0;
138 border-top: 1px solid #eee;
139 border-bottom: 1px solid #fff;
140 }
141 </style>
142 </head>
143 <body>
144 <?php
145
146 if ( !$wgEnableProfileInfo ) {
147 echo '<p>Disabled</p>'
148 . '</body></html>';
149 exit( 1 );
150 }
151
152 $dbr = wfGetDB( DB_SLAVE );
153
154 if ( !$dbr->tableExists( 'profiling' ) ) {
155 echo '<p>No <code>profiling</code> table exists, so we can\'t show you anything.</p>'
156 . '<p>If you want to log profiling data, enable <code>$wgProfileToDatabase</code>'
157 . ' in your LocalSettings.php and run <code>maintenance/update.php</code> to'
158 . ' create the profiling table.'
159 . '</body></html>';
160 exit( 1 );
161 }
162
163 $expand = array();
164 if ( isset( $_REQUEST['expand'] ) ) {
165 foreach ( explode( ',', $_REQUEST['expand'] ) as $f ) {
166 $expand[$f] = true;
167 }
168 }
169
170 class profile_point {
171 var $name;
172 var $count;
173 var $time;
174 var $children;
175
176 static $totaltime, $totalmemory, $totalcount;
177
178 function __construct( $name, $count, $time, $memory ) {
179 $this->name = $name;
180 $this->count = $count;
181 $this->time = $time;
182 $this->memory = $memory;
183 $this->children = array();
184 }
185
186 function add_child( $child ) {
187 $this->children[] = $child;
188 }
189
190 function display( $expand, $indent = 0.0 ) {
191 usort( $this->children, 'compare_point' );
192
193 $ex = isset( $expand[$this->name()] );
194
195 $anchor = str_replace( '"', '', $this->name() );
196
197 if ( !$ex ) {
198 if ( count( $this->children ) ) {
199 $url = getEscapedProfileUrl( false, false, $expand + array( $this->name() => true ) );
200 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[+]</a>";
201 } else {
202 $extet = '';
203 }
204 } else {
205 $e = array();
206 foreach ( $expand as $name => $ep ) {
207 if ( $name != $this->name() ) {
208 $e += array( $name => $ep );
209 }
210 }
211 $url = getEscapedProfileUrl( false, false, $e );
212 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[–]</a>";
213 }
214 ?>
215 <tr>
216 <th>
217 <div style="margin-left: <?php echo (int)$indent; ?>em;">
218 <?php echo htmlspecialchars( str_replace( ',', ', ', $this->name() ) ) . $extet ?>
219 </div>
220 </th>
221 <td class="mw-profileinfo-timep"><?php echo @wfPercent( $this->time() / self::$totaltime * 100 ); ?></td>
222 <td class="mw-profileinfo-memoryp"><?php echo @wfPercent( $this->memory() / self::$totalmemory * 100 ); ?></td>
223 <td class="mw-profileinfo-count"><?php echo $this->count(); ?></td>
224 <td class="mw-profileinfo-cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ); ?></td>
225 <td class="mw-profileinfo-tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ); ?></td>
226 <td class="mw-profileinfo-mpc"><?php echo round( sprintf( '%.2f', $this->memoryPerCall() / 1024 ), 2 ); ?></td>
227 <td class="mw-profileinfo-tpr"><?php echo @round( sprintf( '%.2f', $this->time() / self::$totalcount ), 2 ); ?></td>
228 <td class="mw-profileinfo-mpr"><?php echo @round( sprintf( '%.2f', $this->memory() / self::$totalcount / 1024 ), 2 ); ?></td>
229 </tr>
230 <?php
231 if ( $ex ) {
232 foreach ( $this->children as $child ) {
233 $child->display( $expand, $indent + 2 );
234 }
235 }
236 }
237
238 function name() {
239 return $this->name;
240 }
241
242 function count() {
243 return $this->count;
244 }
245
246 function time() {
247 return $this->time;
248 }
249
250 function memory() {
251 return $this->memory;
252 }
253
254 function timePerCall() {
255 return @( $this->time / $this->count );
256 }
257
258 function memoryPerCall() {
259 return @( $this->memory / $this->count );
260 }
261
262 function callsPerRequest() {
263 return @( $this->count / self::$totalcount );
264 }
265
266 function timePerRequest() {
267 return @( $this->time / self::$totalcount );
268 }
269
270 function memoryPerRequest() {
271 return @( $this->memory / self::$totalcount );
272 }
273
274 function fmttime() {
275 return sprintf( '%5.02f', $this->time );
276 }
277 };
278
279 function compare_point( profile_point $a, profile_point $b ) {
280 global $sort;
281 switch ( $sort ) {
282 case 'name':
283 return strcmp( $a->name(), $b->name() );
284 case 'time':
285 return $a->time() > $b->time() ? -1 : 1;
286 case 'memory':
287 return $a->memory() > $b->memory() ? -1 : 1;
288 case 'count':
289 return $a->count() > $b->count() ? -1 : 1;
290 case 'time_per_call':
291 return $a->timePerCall() > $b->timePerCall() ? -1 : 1;
292 case 'memory_per_call':
293 return $a->memoryPerCall() > $b->memoryPerCall() ? -1 : 1;
294 case 'calls_per_req':
295 return $a->callsPerRequest() > $b->callsPerRequest() ? -1 : 1;
296 case 'time_per_req':
297 return $a->timePerRequest() > $b->timePerRequest() ? -1 : 1;
298 case 'memory_per_req':
299 return $a->memoryPerRequest() > $b->memoryPerRequest() ? -1 : 1;
300 }
301 }
302
303 $sorts = array( 'time', 'memory', 'count', 'calls_per_req', 'name',
304 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' );
305 $sort = 'time';
306 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) ) {
307 $sort = $_REQUEST['sort'];
308 }
309
310 $res = $dbr->select( 'profiling', '*', array(), 'profileinfo.php', array( 'ORDER BY' => 'pf_name ASC' ) );
311
312 if ( isset( $_REQUEST['filter'] ) ) {
313 $filter = $_REQUEST['filter'];
314 } else {
315 $filter = '';
316 }
317
318 ?>
319 <form method="get" action="profileinfo.php">
320 <p>
321 <input type="text" name="filter" value="<?php echo htmlspecialchars( $filter ); ?>">
322 <input type="hidden" name="sort" value="<?php echo htmlspecialchars( $sort ); ?>">
323 <input type="hidden" name="expand" value="<?php echo htmlspecialchars( implode( ",", array_keys( $expand ) ) ); ?>">
324 <input type="submit" value="Filter">
325 </p>
326 </form>
327
328 <table class="mw-profileinfo-table table table-striped table-hover">
329 <thead>
330 <tr>
331 <th><a href="<?php echo getEscapedProfileUrl( false, 'name' ); ?>">Name</a></th>
332 <th><a href="<?php echo getEscapedProfileUrl( false, 'time' ); ?>">Time (%)</a></th>
333 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory' ); ?>">Memory (%)</a></th>
334 <th><a href="<?php echo getEscapedProfileUrl( false, 'count' ); ?>">Count</a></th>
335 <th><a href="<?php echo getEscapedProfileUrl( false, 'calls_per_req' ); ?>">Calls/req</a></th>
336 <th><a href="<?php echo getEscapedProfileUrl( false, 'time_per_call' ); ?>">ms/call</a></th>
337 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory_per_call' ); ?>">kb/call</a></th>
338 <th><a href="<?php echo getEscapedProfileUrl( false, 'time_per_req' ); ?>">ms/req</a></th>
339 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory_per_req' ); ?>">kb/req</a></th>
340 </tr>
341 </thead>
342 <tbody>
343 <?php
344 profile_point::$totaltime = 0.0;
345 profile_point::$totalcount = 0;
346 profile_point::$totalmemory = 0.0;
347
348 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
349 global $filter, $sort, $expand;
350
351 if ( $_expand === false ) {
352 $_expand = $expand;
353 }
354
355 return htmlspecialchars(
356 '?' .
357 wfArrayToCgi( array(
358 'filter' => $_filter ? $_filter : $filter,
359 'sort' => $_sort ? $_sort : $sort,
360 'expand' => implode( ',', array_keys( $_expand ) )
361 ) )
362 );
363 }
364
365 $points = array();
366 $queries = array();
367 $sqltotal = 0.0;
368
369 $last = false;
370 foreach ( $res as $o ) {
371 $next = new profile_point( $o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory );
372 if ( $next->name() == '-total' ) {
373 profile_point::$totaltime = $next->time();
374 profile_point::$totalcount = $next->count();
375 profile_point::$totalmemory = $next->memory();
376 }
377 if ( $last !== false ) {
378 if ( preg_match( '/^' . preg_quote( $last->name(), '/' ) . '/', $next->name() ) ) {
379 $last->add_child( $next );
380 continue;
381 }
382 }
383 $last = $next;
384 if ( preg_match( '/^query: /', $next->name() ) || preg_match( '/^query-m: /', $next->name() ) ) {
385 $sqltotal += $next->time();
386 $queries[] = $next;
387 } else {
388 $points[] = $next;
389 }
390 }
391
392 $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0, 0 );
393 foreach ( $queries as $q )
394 $s->add_child( $q );
395 $points[] = $s;
396
397 usort( $points, 'compare_point' );
398
399 foreach ( $points as $point ) {
400 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) ) {
401 continue;
402 }
403
404 $point->display( $expand );
405 }
406 ?>
407 </tbody>
408 </table>
409 <hr>
410 <p>Total time: <code><?php printf( '%5.02f', profile_point::$totaltime ); ?></code></p>
411
412 <p>Total memory: <code><?php printf( '%5.02f', profile_point::$totalmemory / 1024 ); ?></code></p>
413 <hr />
414 </body>
415 </html>