Merge "Fixed dependencies for jquery.collapsibleTabs"
[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: 8px;
99 line-height: 20px;
100 text-align: left;
101 vertical-align: top;
102 border-top: 1px solid #ddd;
103 }
104
105 .table th {
106 font-weight: bold;
107 }
108
109 .table thead th {
110 vertical-align: bottom;
111 }
112
113 .table thead:first-child tr:first-child th,
114 .table thead:first-child tr:first-child td {
115 border-top: 0;
116 }
117
118 .table tbody + tbody {
119 border-top: 2px solid #ddd;
120 }
121
122 .table-condensed th,
123 .table-condensed td {
124 padding: 4px 5px;
125 }
126
127 .table-striped tbody tr:nth-child(odd) td,
128 .table-striped tbody tr:nth-child(odd) th {
129 background-color: #f9f9f9;
130 }
131
132 .table-hover tbody tr:hover td,
133 .table-hover tbody tr:hover th {
134 background-color: #f5f5f5;
135 }
136
137 hr {
138 margin: 20px 0;
139 border: 0;
140 border-top: 1px solid #eee;
141 border-bottom: 1px solid #fff;
142 }
143
144 </style>
145 </head>
146 <body>
147 <?php
148
149 if ( !$wgEnableProfileInfo ) {
150 echo '<p>Disabled</p>'
151 . '</body></html>';
152 exit( 1 );
153 }
154
155 $dbr = wfGetDB( DB_SLAVE );
156
157 if( !$dbr->tableExists( 'profiling' ) ) {
158 echo '<p>No <code>profiling</code> table exists, so we can\'t show you anything.</p>'
159 . '<p>If you want to log profiling data, create the table using '
160 . '<code>maintenance/archives/patch-profiling.sql</code> and enable '
161 . '<code>$wgProfileToDatabase</code>.</p>'
162 . '</body></html>';
163 exit( 1 );
164 }
165
166 $expand = array();
167 if ( isset( $_REQUEST['expand'] ) )
168 foreach( explode( ',', $_REQUEST['expand'] ) as $f )
169 $expand[$f] = true;
170
171 class profile_point {
172 var $name;
173 var $count;
174 var $time;
175 var $children;
176
177 static $totaltime, $totalmemory, $totalcount;
178
179 function __construct( $name, $count, $time, $memory ) {
180 $this->name = $name;
181 $this->count = $count;
182 $this->time = $time;
183 $this->memory = $memory;
184 $this->children = array();
185 }
186
187 function add_child( $child ) {
188 $this->children[] = $child;
189 }
190
191 function display( $expand, $indent = 0.0 ) {
192 usort( $this->children, 'compare_point' );
193
194 $ex = isset( $expand[$this->name()] );
195
196 if ( !$ex ) {
197 if ( count( $this->children ) ) {
198 $url = getEscapedProfileUrl( false, false, $expand + array( $this->name() => true ) );
199 $extet = ' <a href="' . $url . '">[+]</a>';
200 } else {
201 $extet = '';
202 }
203 } else {
204 $e = array();
205 foreach ( $expand as $name => $ep ) {
206 if ( $name != $this->name() ) {
207 $e += array( $name => $ep );
208 }
209 }
210
211 $extet = ' <a href="' . getEscapedProfileUrl( false, false, $e ) . '">[–]</a>';
212 }
213 ?>
214 <tr>
215 <th><div style="margin-left: <?php echo (int)$indent; ?>em;">
216 <?php echo htmlspecialchars( $this->name() ) . $extet ?>
217 </div></th>
218 <td class="mw-profileinfo-timep"><?php echo @wfPercent( $this->time() / self::$totaltime * 100 ); ?></td>
219 <td class="mw-profileinfo-memoryp"><?php echo @wfPercent( $this->memory() / self::$totalmemory * 100 ); ?></td>
220 <td class="mw-profileinfo-count"><?php echo $this->count(); ?></td>
221 <td class="mw-profileinfo-cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ); ?></td>
222 <td class="mw-profileinfo-tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ); ?></td>
223 <td class="mw-profileinfo-mpc"><?php echo round( sprintf( '%.2f' ,$this->memoryPerCall() / 1024 ), 2 ); ?></td>
224 <td class="mw-profileinfo-tpr"><?php echo @round( sprintf( '%.2f', $this->time() / self::$totalcount ), 2 ); ?></td>
225 <td class="mw-profileinfo-mpr"><?php echo @round( sprintf( '%.2f' ,$this->memory() / self::$totalcount / 1024 ), 2 ); ?></td>
226 </tr>
227 <?php
228 if ( $ex ) {
229 foreach ( $this->children as $child ) {
230 $child->display( $expand, $indent + 2 );
231 }
232 }
233 }
234
235 function name() {
236 return $this->name;
237 }
238
239 function count() {
240 return $this->count;
241 }
242
243 function time() {
244 return $this->time;
245 }
246
247 function memory() {
248 return $this->memory;
249 }
250
251 function timePerCall() {
252 return @( $this->time / $this->count );
253 }
254
255 function memoryPerCall() {
256 return @( $this->memory / $this->count );
257 }
258
259 function callsPerRequest() {
260 return @( $this->count / self::$totalcount );
261 }
262
263 function timePerRequest() {
264 return @( $this->time / self::$totalcount );
265 }
266
267 function memoryPerRequest() {
268 return @( $this->memory / self::$totalcount );
269 }
270
271 function fmttime() {
272 return sprintf( '%5.02f', $this->time );
273 }
274 };
275
276 function compare_point(profile_point $a, profile_point $b) {
277 global $sort;
278 switch ( $sort ) {
279 case 'name':
280 return strcmp( $a->name(), $b->name() );
281 case 'time':
282 return $a->time() > $b->time() ? -1 : 1;
283 case 'memory':
284 return $a->memory() > $b->memory() ? -1 : 1;
285 case 'count':
286 return $a->count() > $b->count() ? -1 : 1;
287 case 'time_per_call':
288 return $a->timePerCall() > $b->timePerCall() ? -1 : 1;
289 case 'memory_per_call':
290 return $a->memoryPerCall() > $b->memoryPerCall() ? -1 : 1;
291 case 'calls_per_req':
292 return $a->callsPerRequest() > $b->callsPerRequest() ? -1 : 1;
293 case 'time_per_req':
294 return $a->timePerRequest() > $b->timePerRequest() ? -1 : 1;
295 case 'memory_per_req':
296 return $a->memoryPerRequest() > $b->memoryPerRequest() ? -1 : 1;
297 }
298 }
299
300 $sorts = array( 'time', 'memory', 'count', 'calls_per_req', 'name',
301 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' );
302 $sort = 'time';
303 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) )
304 $sort = $_REQUEST['sort'];
305
306 $res = $dbr->select( 'profiling', '*', array(), 'profileinfo.php', array( 'ORDER BY' => 'pf_name ASC' ) );
307
308 if (isset( $_REQUEST['filter'] ) )
309 $filter = $_REQUEST['filter'];
310 else
311 $filter = '';
312
313 ?>
314 <form method="get" action="profileinfo.php">
315 <p>
316 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter); ?>">
317 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort); ?>">
318 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand))); ?>">
319 <input type="submit" value="Filter">
320 </p>
321 </form>
322
323 <table class="mw-profileinfo-table table table-striped table-hover">
324 <thead>
325 <tr>
326 <th><a href="<?php echo getEscapedProfileUrl( false, 'name' ); ?>">Name</a></th>
327 <th><a href="<?php echo getEscapedProfileUrl( false, 'time' ); ?>">Time (%)</a></th>
328 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory' ); ?>">Memory (%)</a></th>
329 <th><a href="<?php echo getEscapedProfileUrl( false, 'count' ); ?>">Count</a></th>
330 <th><a href="<?php echo getEscapedProfileUrl( false, 'calls_per_req' ); ?>">Calls/req</a></th>
331 <th><a href="<?php echo getEscapedProfileUrl( false, 'time_per_call' ); ?>">ms/call</a></th>
332 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory_per_call' ); ?>">kb/call</a></th>
333 <th><a href="<?php echo getEscapedProfileUrl( false, 'time_per_req' ); ?>">ms/req</a></th>
334 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory_per_req' ); ?>">kb/req</a></th>
335 </tr>
336 </thead>
337 <tbody>
338 <?php
339 profile_point::$totaltime = 0.0;
340 profile_point::$totalcount = 0;
341 profile_point::$totalmemory = 0.0;
342
343 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
344 global $filter, $sort, $expand;
345
346 if ( $_expand === false )
347 $_expand = $expand;
348
349 return htmlspecialchars(
350 '?' .
351 wfArrayToCGI( array(
352 'filter' => $_filter ? $_filter : $filter,
353 'sort' => $_sort ? $_sort : $sort,
354 'expand' => implode( ',', array_keys( $_expand ) )
355 ) )
356 );
357 }
358
359 $points = array();
360 $queries = array();
361 $sqltotal = 0.0;
362
363 $last = false;
364 foreach( $res as $o ) {
365 $next = new profile_point( $o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory );
366 if( $next->name() == '-total' ) {
367 profile_point::$totaltime = $next->time();
368 profile_point::$totalcount = $next->count();
369 profile_point::$totalmemory = $next->memory();
370 }
371 if ( $last !== false ) {
372 if ( preg_match( '/^'.preg_quote( $last->name(), '/' ).'/', $next->name() ) ) {
373 $last->add_child($next);
374 continue;
375 }
376 }
377 $last = $next;
378 if ( preg_match( '/^query: /', $next->name() ) || preg_match( '/^query-m: /', $next->name() ) ) {
379 $sqltotal += $next->time();
380 $queries[] = $next;
381 } else {
382 $points[] = $next;
383 }
384 }
385
386 $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0, 0 );
387 foreach ( $queries as $q )
388 $s->add_child($q);
389 $points[] = $s;
390
391 usort( $points, 'compare_point' );
392
393 foreach ( $points as $point ) {
394 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) )
395 continue;
396
397 $point->display( $expand );
398 }
399 ?>
400 </tbody>
401 </table>
402 <hr>
403 <p>Total time: <code><?php printf('%5.02f', profile_point::$totaltime); ?></code></p>
404 <p>Total memory: <code><?php printf('%5.02f', profile_point::$totalmemory / 1024 ); ?></code></p>
405 <hr>
406 </body>
407 </html>