Catch excpt to avoid fatal in Message::__toString
[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
38 header( 'Content-Type: text/html; charset=utf-8' );
39
40 ?>
41 <!DOCTYPE html>
42 <html>
43 <head>
44 <meta charset="UTF-8">
45 <title>Profiling data</title>
46 <style>
47 /* noc.wikimedia.org/base.css */
48
49 * {
50 margin: 0;
51 padding: 0;
52 }
53
54 body {
55 padding: 0.5em 1em;
56 background: #fff;
57 font: 14px/1.6 sans-serif;
58 color: #333;
59 }
60
61 p, ul, ol, table {
62 margin: 0.5em 0;
63 }
64
65 a {
66 color: #0645AD;
67 text-decoration: none;
68 }
69
70 a:hover {
71 text-decoration: underline;
72 }
73
74 /*!
75 * Bootstrap v2.1.1
76 *
77 * Copyright 2012 Twitter, Inc
78 * Licensed under the Apache License v2.0
79 * http://www.apache.org/licenses/LICENSE-2.0
80 *
81 * Designed and built with all the love in the world @twitter by @mdo and @fat.
82 */
83
84 table {
85 max-width: 100%;
86 background-color: transparent;
87 border-collapse: collapse;
88 border-spacing: 0;
89 }
90
91 .table {
92 width: 100%;
93 margin-bottom: 20px;
94 }
95
96 .table th,
97 .table td {
98 padding: 0.1em;
99 text-align: left;
100 vertical-align: top;
101 border-top: 1px solid #ddd;
102 }
103
104 .table th {
105 font-weight: bold;
106 }
107
108 .table thead th {
109 vertical-align: bottom;
110 }
111
112 .table thead:first-child tr:first-child th,
113 .table thead:first-child tr:first-child td {
114 border-top: 0;
115 }
116
117 .table tbody + tbody {
118 border-top: 2px solid #ddd;
119 }
120
121 .table-condensed th,
122 .table-condensed td {
123 padding: 4px 5px;
124 }
125
126 .table-striped tbody tr:nth-child(odd) td,
127 .table-striped tbody tr:nth-child(odd) th {
128 background-color: #f9f9f9;
129 }
130
131 .table-hover tbody tr:hover td,
132 .table-hover tbody tr:hover th {
133 background-color: #f5f5f5;
134 }
135
136 hr {
137 margin: 20px 0;
138 border: 0;
139 border-top: 1px solid #eee;
140 border-bottom: 1px solid #fff;
141 }
142
143 </style>
144 </head>
145 <body>
146 <?php
147
148 if ( !$wgEnableProfileInfo ) {
149 echo '<p>Disabled</p>'
150 . '</body></html>';
151 exit( 1 );
152 }
153
154 $dbr = wfGetDB( DB_SLAVE );
155
156 if( !$dbr->tableExists( 'profiling' ) ) {
157 echo '<p>No <code>profiling</code> table exists, so we can\'t show you anything.</p>'
158 . '<p>If you want to log profiling data, enable <code>$wgProfileToDatabase</code>'
159 . ' in your LocalSettings.php and run <code>maintenance/update.php</code> to'
160 . ' create the profiling table.'
161 . '</body></html>';
162 exit( 1 );
163 }
164
165 $expand = array();
166 if ( isset( $_REQUEST['expand'] ) )
167 foreach( explode( ',', $_REQUEST['expand'] ) as $f )
168 $expand[$f] = true;
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><div style="margin-left: <?php echo (int)$indent; ?>em;">
217 <?php echo htmlspecialchars( str_replace( ',', ', ', $this->name() ) ) . $extet ?>
218 </div></th>
219 <td class="mw-profileinfo-timep"><?php echo @wfPercent( $this->time() / self::$totaltime * 100 ); ?></td>
220 <td class="mw-profileinfo-memoryp"><?php echo @wfPercent( $this->memory() / self::$totalmemory * 100 ); ?></td>
221 <td class="mw-profileinfo-count"><?php echo $this->count(); ?></td>
222 <td class="mw-profileinfo-cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ); ?></td>
223 <td class="mw-profileinfo-tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ); ?></td>
224 <td class="mw-profileinfo-mpc"><?php echo round( sprintf( '%.2f' ,$this->memoryPerCall() / 1024 ), 2 ); ?></td>
225 <td class="mw-profileinfo-tpr"><?php echo @round( sprintf( '%.2f', $this->time() / self::$totalcount ), 2 ); ?></td>
226 <td class="mw-profileinfo-mpr"><?php echo @round( sprintf( '%.2f' ,$this->memory() / self::$totalcount / 1024 ), 2 ); ?></td>
227 </tr>
228 <?php
229 if ( $ex ) {
230 foreach ( $this->children as $child ) {
231 $child->display( $expand, $indent + 2 );
232 }
233 }
234 }
235
236 function name() {
237 return $this->name;
238 }
239
240 function count() {
241 return $this->count;
242 }
243
244 function time() {
245 return $this->time;
246 }
247
248 function memory() {
249 return $this->memory;
250 }
251
252 function timePerCall() {
253 return @( $this->time / $this->count );
254 }
255
256 function memoryPerCall() {
257 return @( $this->memory / $this->count );
258 }
259
260 function callsPerRequest() {
261 return @( $this->count / self::$totalcount );
262 }
263
264 function timePerRequest() {
265 return @( $this->time / self::$totalcount );
266 }
267
268 function memoryPerRequest() {
269 return @( $this->memory / self::$totalcount );
270 }
271
272 function fmttime() {
273 return sprintf( '%5.02f', $this->time );
274 }
275 };
276
277 function compare_point(profile_point $a, profile_point $b) {
278 global $sort;
279 switch ( $sort ) {
280 case 'name':
281 return strcmp( $a->name(), $b->name() );
282 case 'time':
283 return $a->time() > $b->time() ? -1 : 1;
284 case 'memory':
285 return $a->memory() > $b->memory() ? -1 : 1;
286 case 'count':
287 return $a->count() > $b->count() ? -1 : 1;
288 case 'time_per_call':
289 return $a->timePerCall() > $b->timePerCall() ? -1 : 1;
290 case 'memory_per_call':
291 return $a->memoryPerCall() > $b->memoryPerCall() ? -1 : 1;
292 case 'calls_per_req':
293 return $a->callsPerRequest() > $b->callsPerRequest() ? -1 : 1;
294 case 'time_per_req':
295 return $a->timePerRequest() > $b->timePerRequest() ? -1 : 1;
296 case 'memory_per_req':
297 return $a->memoryPerRequest() > $b->memoryPerRequest() ? -1 : 1;
298 }
299 }
300
301 $sorts = array( 'time', 'memory', 'count', 'calls_per_req', 'name',
302 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' );
303 $sort = 'time';
304 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) )
305 $sort = $_REQUEST['sort'];
306
307 $res = $dbr->select( 'profiling', '*', array(), 'profileinfo.php', array( 'ORDER BY' => 'pf_name ASC' ) );
308
309 if (isset( $_REQUEST['filter'] ) )
310 $filter = $_REQUEST['filter'];
311 else
312 $filter = '';
313
314 ?>
315 <form method="get" action="profileinfo.php">
316 <p>
317 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter); ?>">
318 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort); ?>">
319 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand))); ?>">
320 <input type="submit" value="Filter">
321 </p>
322 </form>
323
324 <table class="mw-profileinfo-table table table-striped table-hover">
325 <thead>
326 <tr>
327 <th><a href="<?php echo getEscapedProfileUrl( false, 'name' ); ?>">Name</a></th>
328 <th><a href="<?php echo getEscapedProfileUrl( false, 'time' ); ?>">Time (%)</a></th>
329 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory' ); ?>">Memory (%)</a></th>
330 <th><a href="<?php echo getEscapedProfileUrl( false, 'count' ); ?>">Count</a></th>
331 <th><a href="<?php echo getEscapedProfileUrl( false, 'calls_per_req' ); ?>">Calls/req</a></th>
332 <th><a href="<?php echo getEscapedProfileUrl( false, 'time_per_call' ); ?>">ms/call</a></th>
333 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory_per_call' ); ?>">kb/call</a></th>
334 <th><a href="<?php echo getEscapedProfileUrl( false, 'time_per_req' ); ?>">ms/req</a></th>
335 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory_per_req' ); ?>">kb/req</a></th>
336 </tr>
337 </thead>
338 <tbody>
339 <?php
340 profile_point::$totaltime = 0.0;
341 profile_point::$totalcount = 0;
342 profile_point::$totalmemory = 0.0;
343
344 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
345 global $filter, $sort, $expand;
346
347 if ( $_expand === false )
348 $_expand = $expand;
349
350 return htmlspecialchars(
351 '?' .
352 wfArrayToCGI( array(
353 'filter' => $_filter ? $_filter : $filter,
354 'sort' => $_sort ? $_sort : $sort,
355 'expand' => implode( ',', array_keys( $_expand ) )
356 ) )
357 );
358 }
359
360 $points = array();
361 $queries = array();
362 $sqltotal = 0.0;
363
364 $last = false;
365 foreach( $res as $o ) {
366 $next = new profile_point( $o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory );
367 if( $next->name() == '-total' ) {
368 profile_point::$totaltime = $next->time();
369 profile_point::$totalcount = $next->count();
370 profile_point::$totalmemory = $next->memory();
371 }
372 if ( $last !== false ) {
373 if ( preg_match( '/^'.preg_quote( $last->name(), '/' ).'/', $next->name() ) ) {
374 $last->add_child($next);
375 continue;
376 }
377 }
378 $last = $next;
379 if ( preg_match( '/^query: /', $next->name() ) || preg_match( '/^query-m: /', $next->name() ) ) {
380 $sqltotal += $next->time();
381 $queries[] = $next;
382 } else {
383 $points[] = $next;
384 }
385 }
386
387 $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0, 0 );
388 foreach ( $queries as $q )
389 $s->add_child($q);
390 $points[] = $s;
391
392 usort( $points, 'compare_point' );
393
394 foreach ( $points as $point ) {
395 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) )
396 continue;
397
398 $point->display( $expand );
399 }
400 ?>
401 </tbody>
402 </table>
403 <hr>
404 <p>Total time: <code><?php printf('%5.02f', profile_point::$totaltime); ?></code></p>
405 <p>Total memory: <code><?php printf('%5.02f', profile_point::$totalmemory / 1024 ); ?></code></p>
406 <hr>
407 </body>
408 </html>