3 * Simple interface for displaying request profile data stored in
4 * the wikis' primary database.
6 * See also https://www.mediawiki.org/wiki/Manual:Profiling.
8 * To add profiling information to the database:
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.
16 * Copyright 2005 Kate Turner.
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:
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
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
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.
41 define( 'MW_NO_SESSION', 1 );
43 ini_set( 'zlib.output_compression', 'off' );
45 require __DIR__
. '/includes/WebStart.php';
47 header( 'Content-Type: text/html; charset=utf-8' );
53 <meta charset
="UTF-8" />
54 <title
>Profiling data
</title
>
56 /* noc.wikimedia.org/base.css */
66 font
: 14px
/1.6 sans
-serif
;
76 text
-decoration
: none
;
80 text
-decoration
: underline
;
86 * Copyright 2012 Twitter, Inc
87 * Licensed under the Apache License v2.0
88 * http://www.apache.org/licenses/LICENSE-2.0
90 * Designed and built with all the love in the world @twitter by @mdo and @fat.
95 background
-color
: transparent
;
96 border
-collapse
: collapse
;
110 border
-top
: 1px solid
#ddd;
118 vertical
-align
: bottom
;
121 .table thead
:first
-child tr
:first
-child th
,
122 .table thead
:first
-child tr
:first
-child td
{
126 .table tbody + tbody
{
127 border
-top
: 2px solid
#ddd;
131 .table
-condensed td
{
135 .table
-striped tbody tr
:nth
-child(odd
) td
,
136 .table
-striped tbody tr
:nth
-child(odd
) th
{
137 background
-color
: #f9f9f9;
140 .table
-hover tbody tr
:hover td
,
141 .table
-hover tbody tr
:hover th
{
142 background
-color
: #f5f5f5;
148 border
-top
: 1px solid
#eee;
149 border
-bottom
: 1px solid
#fff;
156 if ( !$wgEnableProfileInfo ) {
157 echo '<p>Disabled</p>'
162 $dbr = wfGetDB( DB_REPLICA
);
164 if ( !$dbr->tableExists( 'profiling' ) ) {
165 echo '<p>No <code>profiling</code> table exists, so we can\'t show you anything.</p>'
166 . '<p>If you want to log profiling data, enable <code>$wgProfiler[\'output\'] = \'db\'</code>'
167 . ' in LocalSettings.php and run <code>maintenance/update.php</code> to'
168 . ' create the profiling table.'
174 if ( isset( $_REQUEST['expand'] ) ) {
175 foreach ( explode( ',', $_REQUEST['expand'] ) as $f ) {
180 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
181 class profile_point
{
188 public static $totaltime, $totalmemory, $totalcount;
190 public function __construct( $name, $count, $time, $memory ) {
192 $this->count
= $count;
194 $this->memory
= $memory;
195 $this->children
= [];
198 public function add_child( $child ) {
199 $this->children
[] = $child;
202 public function display( $expand, $indent = 0.0 ) {
203 usort( $this->children
, 'compare_point' );
205 $ex = isset( $expand[$this->name()] );
207 $anchor = str_replace( '"', '', $this->name() );
210 if ( count( $this->children
) ) {
211 $url = getEscapedProfileUrl( false, false, $expand +
[ $this->name() => true ] );
212 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[+]</a>";
218 foreach ( $expand as $name => $ep ) {
219 if ( $name != $this->name() ) {
220 $e +
= [ $name => $ep ];
223 $url = getEscapedProfileUrl( false, false, $e );
224 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[–]</a>";
229 <div style
="margin-left: <?php echo (int)$indent; ?>em;">
230 <?php
echo htmlspecialchars( str_replace( ',', ', ', $this->name() ) ) . $extet ?
>
233 <?php
// phpcs:disable Generic.Files.LineLength,Generic.PHP.NoSilencedErrors ?>
234 <td
class="mw-profileinfo-timep"><?php
echo @wfPercent
( $this->time() / self
::$totaltime * 100 ); ?
></td
>
235 <td
class="mw-profileinfo-memoryp"><?php
echo @wfPercent
( $this->memory() / self
::$totalmemory * 100 ); ?
></td
>
236 <td
class="mw-profileinfo-count"><?php
echo $this->count(); ?
></td
>
237 <td
class="mw-profileinfo-cpr"><?php
echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ); ?
></td
>
238 <td
class="mw-profileinfo-tpc"><?php
echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ); ?
></td
>
239 <td
class="mw-profileinfo-mpc"><?php
echo round( sprintf( '%.2f', $this->memoryPerCall() / 1024 ), 2 ); ?
></td
>
240 <td
class="mw-profileinfo-tpr"><?php
echo @round
( sprintf( '%.2f', $this->time() / self
::$totalcount ), 2 ); ?
></td
>
241 <td
class="mw-profileinfo-mpr"><?php
echo @round
( sprintf( '%.2f', $this->memory() / self
::$totalcount / 1024 ), 2 ); ?
></td
>
242 <?php
// phpcs:enable ?>
246 foreach ( $this->children
as $child ) {
247 $child->display( $expand, $indent +
2 );
252 public function name() {
256 public function count() {
260 public function time() {
264 public function memory() {
265 return $this->memory
;
268 public function timePerCall() {
269 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
270 return @( $this->time
/ $this->count
);
273 public function memoryPerCall() {
274 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
275 return @( $this->memory
/ $this->count
);
278 public function callsPerRequest() {
279 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
280 return @( $this->count
/ self
::$totalcount );
283 public function timePerRequest() {
284 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
285 return @( $this->time
/ self
::$totalcount );
288 public function memoryPerRequest() {
289 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
290 return @( $this->memory
/ self
::$totalcount );
293 public function fmttime() {
294 return sprintf( '%5.02f', $this->time
);
298 function compare_point( profile_point
$a, profile_point
$b ) {
299 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
305 return strcmp( $a->name(), $b->name() );
306 // Sorted descending:
308 return $b->time() <=> $a->time();
310 return $b->memory() <=> $a->memory();
312 return $b->count() <=> $a->count();
313 case 'time_per_call':
314 return $b->timePerCall() <=> $a->timePerCall();
315 case 'memory_per_call':
316 return $b->memoryPerCall() <=> $a->memoryPerCall();
317 case 'calls_per_req':
318 return $b->callsPerRequest() <=> $a->callsPerRequest();
320 return $b->timePerRequest() <=> $a->timePerRequest();
321 case 'memory_per_req':
322 return $b->memoryPerRequest() <=> $a->memoryPerRequest();
326 $sorts = [ 'time', 'memory', 'count', 'calls_per_req', 'name',
327 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' ];
329 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) ) {
330 $sort = $_REQUEST['sort'];
338 [ 'ORDER BY' => 'pf_name ASC' ]
341 $filter = $_REQUEST['filter'] ??
'';
344 <form method
="get" action
="profileinfo.php">
346 <input type
="text" name
="filter" value
="<?php echo htmlspecialchars( $filter ); ?>">
347 <input type
="hidden" name
="sort" value
="<?php echo htmlspecialchars( $sort ); ?>">
348 <input type
="hidden" name
="expand" value
="<?php
349 echo htmlspecialchars( implode( ",", array_keys( $expand ) ) );
351 <input type
="submit" value
="Filter">
355 <table
class="mw-profileinfo-table table table-striped table-hover">
359 echo getEscapedProfileUrl( false, 'name' );
362 echo getEscapedProfileUrl( false, 'time' );
363 ?>">Time (%
)</a
></th
>
365 echo getEscapedProfileUrl( false, 'memory' );
366 ?>">Memory (%
)</a
></th
>
368 echo getEscapedProfileUrl( false, 'count' );
371 echo getEscapedProfileUrl( false, 'calls_per_req' );
372 ?>">Calls
/req
</a
></th
>
374 echo getEscapedProfileUrl( false, 'time_per_call' );
377 echo getEscapedProfileUrl( false, 'memory_per_call' );
380 echo getEscapedProfileUrl( false, 'time_per_req' );
383 echo getEscapedProfileUrl( false, 'memory_per_req' );
389 profile_point
::$totaltime = 0.0;
390 profile_point
::$totalcount = 0;
391 profile_point
::$totalmemory = 0.0;
393 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
394 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
395 global $filter, $sort, $expand;
397 if ( $_expand === false ) {
401 return htmlspecialchars(
404 'filter' => $_filter ?
: $filter,
405 'sort' => $_sort ?
: $sort,
406 'expand' => implode( ',', array_keys( $_expand ) )
415 /** @var profile_point|false $last */
417 foreach ( $res as $o ) {
418 $next = new profile_point( $o->pf_name
, $o->pf_count
, $o->pf_time
, $o->pf_memory
);
419 if ( $next->name() == '-total' ||
$next->name() == 'main()' ) {
420 profile_point
::$totaltime = $next->time();
421 profile_point
::$totalcount = $next->count();
422 profile_point
::$totalmemory = $next->memory();
424 if ( $last !== false ) {
425 if ( preg_match( '/^' . preg_quote( $last->name(), '/' ) . '/', $next->name() ) ) {
426 $last->add_child( $next );
431 if ( preg_match( '/^query: /', $next->name() ) ||
preg_match( '/^query-m: /', $next->name() ) ) {
432 $sqltotal +
= $next->time();
439 $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0 );
440 foreach ( $queries as $q ) {
445 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
446 @usort
( $points, 'compare_point' );
448 foreach ( $points as $point ) {
449 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) ) {
453 $point->display( $expand );
459 <p
>Total time
: <code
><?php
printf( '%5.02f', profile_point
::$totaltime ); ?
></code
></p
>
461 <p
>Total memory
: <code
><?php
printf( '%5.02f', profile_point
::$totalmemory / 1024 ); ?
></code
></p
>