MediaWiki REL1_34
profileinfo.php
Go to the documentation of this file.
1<?php
39// This endpoint is supposed to be independent of request cookies and other
40// details of the session. Enforce this constraint with respect to session use.
41define( 'MW_NO_SESSION', 1 );
42
43define( 'MW_ENTRY_POINT', 'profileinfo' );
44
45ini_set( 'zlib.output_compression', 'off' );
46
47require __DIR__ . '/includes/WebStart.php';
48
49header( 'Content-Type: text/html; charset=utf-8' );
50
51?>
52<!DOCTYPE html>
53<html>
54<head>
55 <meta charset="UTF-8" />
56 <title>Profiling data</title>
57 <style>
58 /* noc.wikimedia.org/base.css */
59
60 * {
61 margin: 0;
62 padding: 0;
63 }
64
65 body {
66 padding: 0.5em 1em;
67 background: #fff;
68 font: 14px/1.6 sans-serif;
69 color: #333;
70 }
71
72 p, ul, ol, table {
73 margin: 0.5em 0;
74 }
75
76 a {
77 color: #0645AD;
78 text-decoration: none;
79 }
80
81 a:hover {
82 text-decoration: underline;
83 }
84
95 table {
96 max-width: 100%;
97 background-color: transparent;
98 border-collapse: collapse;
99 border-spacing: 0;
100 }
101
102 .table {
103 width: 100%;
104 margin-bottom: 20px;
105 }
106
107 .table th,
108 .table td {
109 padding: 0.1em;
110 text-align: left;
111 vertical-align: top;
112 border-top: 1px solid #ddd;
113 }
114
115 .table th {
116 font-weight: bold;
117 }
118
119 .table thead th {
120 vertical-align: bottom;
121 }
122
123 .table thead:first-child tr:first-child th,
124 .table thead:first-child tr:first-child td {
125 border-top: 0;
126 }
127
128 .table tbody + tbody {
129 border-top: 2px solid #ddd;
130 }
131
132 .table-condensed th,
133 .table-condensed td {
134 padding: 4px 5px;
135 }
136
137 .table-striped tbody tr:nth-child(odd) td,
138 .table-striped tbody tr:nth-child(odd) th {
139 background-color: #f9f9f9;
140 }
141
142 .table-hover tbody tr:hover td,
143 .table-hover tbody tr:hover th {
144 background-color: #f5f5f5;
145 }
146
147 hr {
148 margin: 20px 0;
149 border: 0;
150 border-top: 1px solid #eee;
151 border-bottom: 1px solid #fff;
152 }
153 </style>
154</head>
155<body>
156<?php
157
158if ( !$wgEnableProfileInfo ) {
159 echo '<p>Disabled</p>'
160 . '</body></html>';
161 exit( 1 );
162}
163
165
166if ( !$dbr->tableExists( 'profiling' ) ) {
167 echo '<p>No <code>profiling</code> table exists, so we can\'t show you anything.</p>'
168 . '<p>If you want to log profiling data, enable <code>$wgProfiler[\'output\'] = \'db\'</code>'
169 . ' in LocalSettings.php and run <code>maintenance/update.php</code> to'
170 . ' create the profiling table.'
171 . '</body></html>';
172 exit( 1 );
173}
174
176if ( isset( $_REQUEST['expand'] ) ) {
177 foreach ( explode( ',', $_REQUEST['expand'] ) as $f ) {
178 $expand[$f] = true;
179 }
180}
181wfDeprecated( 'profileinfo.php', '1.34' );
182
183// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
185
186 public $name;
187 public $count;
188 public $time;
189 public $children;
190
192
193 public function __construct( $name, $count, $time, $memory ) {
194 $this->name = $name;
195 $this->count = $count;
196 $this->time = $time;
197 $this->memory = $memory;
198 $this->children = [];
199 }
200
201 public function add_child( $child ) {
202 $this->children[] = $child;
203 }
204
205 public function display( $expand, $indent = 0.0 ) {
206 usort( $this->children, 'compare_point' );
207
208 $ex = isset( $expand[$this->name()] );
209
210 $anchor = str_replace( '"', '', $this->name() );
211
212 if ( !$ex ) {
213 if ( count( $this->children ) ) {
214 $url = getEscapedProfileUrl( false, false, $expand + [ $this->name() => true ] );
215 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[+]</a>";
216 } else {
217 $extet = '';
218 }
219 } else {
220 $e = [];
221 foreach ( $expand as $name => $ep ) {
222 if ( $name != $this->name() ) {
223 $e += [ $name => $ep ];
224 }
225 }
226 $url = getEscapedProfileUrl( false, false, $e );
227 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[–]</a>";
228 }
229 ?>
230 <tr>
231 <th>
232 <div style="margin-left: <?php echo (int)$indent; ?>em;">
233 <?php echo htmlspecialchars( str_replace( ',', ', ', $this->name() ) ) . $extet ?>
234 </div>
235 </th>
236 <?php // phpcs:disable Generic.Files.LineLength,Generic.PHP.NoSilencedErrors ?>
237 <td class="mw-profileinfo-timep"><?php echo @wfPercent( $this->time() / self::$totaltime * 100 ); ?></td>
238 <td class="mw-profileinfo-memoryp"><?php echo @wfPercent( $this->memory() / self::$totalmemory * 100 ); ?></td>
239 <td class="mw-profileinfo-count"><?php echo $this->count(); ?></td>
240 <td class="mw-profileinfo-cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ); ?></td>
241 <td class="mw-profileinfo-tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ); ?></td>
242 <td class="mw-profileinfo-mpc"><?php echo round( sprintf( '%.2f', $this->memoryPerCall() / 1024 ), 2 ); ?></td>
243 <td class="mw-profileinfo-tpr"><?php echo @round( sprintf( '%.2f', $this->time() / self::$totalcount ), 2 ); ?></td>
244 <td class="mw-profileinfo-mpr"><?php echo @round( sprintf( '%.2f', $this->memory() / self::$totalcount / 1024 ), 2 ); ?></td>
245 <?php // phpcs:enable ?>
246 </tr>
247 <?php
248 if ( $ex ) {
249 foreach ( $this->children as $child ) {
250 $child->display( $expand, $indent + 2 );
251 }
252 }
253 }
254
255 public function name() {
256 return $this->name;
257 }
258
259 public function count() {
260 return $this->count;
261 }
262
263 public function time() {
264 return $this->time;
265 }
266
267 public function memory() {
268 return $this->memory;
269 }
270
271 public function timePerCall() {
272 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
273 return @( $this->time / $this->count );
274 }
275
276 public function memoryPerCall() {
277 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
278 return @( $this->memory / $this->count );
279 }
280
281 public function callsPerRequest() {
282 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
283 return @( $this->count / self::$totalcount );
284 }
285
286 public function timePerRequest() {
287 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
288 return @( $this->time / self::$totalcount );
289 }
290
291 public function memoryPerRequest() {
292 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
293 return @( $this->memory / self::$totalcount );
294 }
295
296 public function fmttime() {
297 return sprintf( '%5.02f', $this->time );
298 }
299}
300
302 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
303 global $sort;
304
305 switch ( $sort ) {
306 // Sorted ascending:
307 case 'name':
308 return strcmp( $a->name(), $b->name() );
309 // Sorted descending:
310 case 'time':
311 return $b->time() <=> $a->time();
312 case 'memory':
313 return $b->memory() <=> $a->memory();
314 case 'count':
315 return $b->count() <=> $a->count();
316 case 'time_per_call':
317 return $b->timePerCall() <=> $a->timePerCall();
318 case 'memory_per_call':
319 return $b->memoryPerCall() <=> $a->memoryPerCall();
320 case 'calls_per_req':
321 return $b->callsPerRequest() <=> $a->callsPerRequest();
322 case 'time_per_req':
323 return $b->timePerRequest() <=> $a->timePerRequest();
324 case 'memory_per_req':
325 return $b->memoryPerRequest() <=> $a->memoryPerRequest();
326 }
327}
328
329$sorts = [ 'time', 'memory', 'count', 'calls_per_req', 'name',
330 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' ];
331$sort = 'time';
332if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) ) {
333 $sort = $_REQUEST['sort'];
334}
335
336$res = $dbr->select(
337 'profiling',
338 '*',
339 [],
340 'profileinfo.php',
341 [ 'ORDER BY' => 'pf_name ASC' ]
342);
343
344$filter = $_REQUEST['filter'] ?? '';
345
346?>
347<form method="get" action="profileinfo.php">
348 <p>
349 <input type="text" name="filter" value="<?php echo htmlspecialchars( $filter ); ?>">
350 <input type="hidden" name="sort" value="<?php echo htmlspecialchars( $sort ); ?>">
351 <input type="hidden" name="expand" value="<?php
352 echo htmlspecialchars( implode( ",", array_keys( $expand ) ) );
353 ?>">
354 <input type="submit" value="Filter">
355 </p>
356</form>
357
358<table class="mw-profileinfo-table table table-striped table-hover">
359 <thead>
360 <tr>
361 <th><a href="<?php
362 echo getEscapedProfileUrl( false, 'name' );
363 ?>">Name</a></th>
364 <th><a href="<?php
365 echo getEscapedProfileUrl( false, 'time' );
366 ?>">Time (%)</a></th>
367 <th><a href="<?php
368 echo getEscapedProfileUrl( false, 'memory' );
369 ?>">Memory (%)</a></th>
370 <th><a href="<?php
371 echo getEscapedProfileUrl( false, 'count' );
372 ?>">Count</a></th>
373 <th><a href="<?php
374 echo getEscapedProfileUrl( false, 'calls_per_req' );
375 ?>">Calls/req</a></th>
376 <th><a href="<?php
377 echo getEscapedProfileUrl( false, 'time_per_call' );
378 ?>">ms/call</a></th>
379 <th><a href="<?php
380 echo getEscapedProfileUrl( false, 'memory_per_call' );
381 ?>">kb/call</a></th>
382 <th><a href="<?php
383 echo getEscapedProfileUrl( false, 'time_per_req' );
384 ?>">ms/req</a></th>
385 <th><a href="<?php
386 echo getEscapedProfileUrl( false, 'memory_per_req' );
387 ?>">kb/req</a></th>
388 </tr>
389 </thead>
390 <tbody>
391 <?php
395
396 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
397 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
398 global $filter, $sort, $expand;
399
400 if ( $_expand === false ) {
401 $_expand = $expand;
402 }
403
404 return htmlspecialchars(
405 '?' .
406 wfArrayToCgi( [
407 'filter' => $_filter ?: $filter,
408 'sort' => $_sort ?: $sort,
409 'expand' => implode( ',', array_keys( $_expand ) )
410 ] )
411 );
412 }
413
417
419 $last = false;
420 foreach ( $res as $o ) {
421 $next = new profile_point( $o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory );
422 if ( $next->name() == '-total' || $next->name() == 'main()' ) {
423 profile_point::$totaltime = $next->time();
424 profile_point::$totalcount = $next->count();
425 profile_point::$totalmemory = $next->memory();
426 }
427 if ( $last !== false ) {
428 if ( preg_match( '/^' . preg_quote( $last->name(), '/' ) . '/', $next->name() ) ) {
429 $last->add_child( $next );
430 continue;
431 }
432 }
433 $last = $next;
434 if ( preg_match( '/^query: /', $next->name() ) || preg_match( '/^query-m: /', $next->name() ) ) {
435 $sqltotal += $next->time();
436 $queries[] = $next;
437 } else {
438 $points[] = $next;
439 }
440 }
441
442 $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0 );
443 foreach ( $queries as $q ) {
444 $s->add_child( $q );
445 }
447
448 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
449 @usort( $points, 'compare_point' );
450
451 foreach ( $points as $point ) {
452 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) ) {
453 continue;
454 }
455
456 $point->display( $expand );
457 }
458 ?>
459 </tbody>
460</table>
461<hr />
462<p>Total time: <code><?php printf( '%5.02f', profile_point::$totaltime ); ?></code></p>
463
464<p>Total memory: <code><?php printf( '%5.02f', profile_point::$totalmemory / 1024 ); ?></code></p>
465<hr />
466</body>
467</html>
$wgEnableProfileInfo
Allow the profileinfo.php entrypoint to be used.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfPercent( $nr, $acc=2, $round=true)
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
static $totalmemory
display( $expand, $indent=0.0)
__construct( $name, $count, $time, $memory)
add_child( $child)
if(isset( $_REQUEST['sort']) &&in_array( $_REQUEST['sort'], $sorts)) $res
$points
$queries
$sort
compare_point(profile_point $a, profile_point $b)
$last
foreach( $res as $o) $s
getEscapedProfileUrl( $_filter=false, $_sort=false, $_expand=false)
$filter
$sorts
if(! $wgEnableProfileInfo) $dbr
$sqltotal
if(! $dbr->tableExists( 'profiling')) $expand
const DB_REPLICA
Definition defines.php:25