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