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 define( 'MW_ENTRY_POINT', 'profileinfo' );
45 ini_set( 'zlib.output_compression', 'off' );
47 require __DIR__
. '/includes/WebStart.php';
49 header( 'Content-Type: text/html; charset=utf-8' );
55 <meta charset
="UTF-8" />
56 <title
>Profiling data
</title
>
58 /* noc.wikimedia.org/base.css */
68 font
: 14px
/1.6 sans
-serif
;
78 text
-decoration
: none
;
82 text
-decoration
: underline
;
88 * Copyright 2012 Twitter, Inc
89 * Licensed under the Apache License v2.0
90 * http://www.apache.org/licenses/LICENSE-2.0
92 * Designed and built with all the love in the world @twitter by @mdo and @fat.
97 background
-color
: transparent
;
98 border
-collapse
: collapse
;
112 border
-top
: 1px solid
#ddd;
120 vertical
-align
: bottom
;
123 .table thead
:first
-child tr
:first
-child th
,
124 .table thead
:first
-child tr
:first
-child td
{
128 .table tbody + tbody
{
129 border
-top
: 2px solid
#ddd;
133 .table
-condensed td
{
137 .table
-striped tbody tr
:nth
-child(odd
) td
,
138 .table
-striped tbody tr
:nth
-child(odd
) th
{
139 background
-color
: #f9f9f9;
142 .table
-hover tbody tr
:hover td
,
143 .table
-hover tbody tr
:hover th
{
144 background
-color
: #f5f5f5;
150 border
-top
: 1px solid
#eee;
151 border
-bottom
: 1px solid
#fff;
158 if ( !$wgEnableProfileInfo ) {
159 echo '<p>Disabled</p>'
164 $dbr = wfGetDB( DB_REPLICA
);
166 if ( !$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.'
176 if ( isset( $_REQUEST['expand'] ) ) {
177 foreach ( explode( ',', $_REQUEST['expand'] ) as $f ) {
181 wfDeprecated( 'profileinfo.php', '1.34' );
183 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
184 class profile_point
{
191 public static $totaltime, $totalmemory, $totalcount;
193 public function __construct( $name, $count, $time, $memory ) {
195 $this->count
= $count;
197 $this->memory
= $memory;
198 $this->children
= [];
201 public function add_child( $child ) {
202 $this->children
[] = $child;
205 public function display( $expand, $indent = 0.0 ) {
206 usort( $this->children
, 'compare_point' );
208 $ex = isset( $expand[$this->name()] );
210 $anchor = str_replace( '"', '', $this->name() );
213 if ( count( $this->children
) ) {
214 $url = getEscapedProfileUrl( false, false, $expand +
[ $this->name() => true ] );
215 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[+]</a>";
221 foreach ( $expand as $name => $ep ) {
222 if ( $name != $this->name() ) {
223 $e +
= [ $name => $ep ];
226 $url = getEscapedProfileUrl( false, false, $e );
227 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[–]</a>";
232 <div style
="margin-left: <?php echo (int)$indent; ?>em;">
233 <?php
echo htmlspecialchars( str_replace( ',', ', ', $this->name() ) ) . $extet ?
>
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 ?>
249 foreach ( $this->children
as $child ) {
250 $child->display( $expand, $indent +
2 );
255 public function name() {
259 public function count() {
263 public function time() {
267 public function memory() {
268 return $this->memory
;
271 public function timePerCall() {
272 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
273 return @( $this->time
/ $this->count
);
276 public function memoryPerCall() {
277 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
278 return @( $this->memory
/ $this->count
);
281 public function callsPerRequest() {
282 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
283 return @( $this->count
/ self
::$totalcount );
286 public function timePerRequest() {
287 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
288 return @( $this->time
/ self
::$totalcount );
291 public function memoryPerRequest() {
292 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
293 return @( $this->memory
/ self
::$totalcount );
296 public function fmttime() {
297 return sprintf( '%5.02f', $this->time
);
301 function compare_point( profile_point
$a, profile_point
$b ) {
302 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
308 return strcmp( $a->name(), $b->name() );
309 // Sorted descending:
311 return $b->time() <=> $a->time();
313 return $b->memory() <=> $a->memory();
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();
323 return $b->timePerRequest() <=> $a->timePerRequest();
324 case 'memory_per_req':
325 return $b->memoryPerRequest() <=> $a->memoryPerRequest();
329 $sorts = [ 'time', 'memory', 'count', 'calls_per_req', 'name',
330 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' ];
332 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) ) {
333 $sort = $_REQUEST['sort'];
341 [ 'ORDER BY' => 'pf_name ASC' ]
344 $filter = $_REQUEST['filter'] ??
'';
347 <form method
="get" action
="profileinfo.php">
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 ) ) );
354 <input type
="submit" value
="Filter">
358 <table
class="mw-profileinfo-table table table-striped table-hover">
362 echo getEscapedProfileUrl( false, 'name' );
365 echo getEscapedProfileUrl( false, 'time' );
366 ?>">Time (%
)</a
></th
>
368 echo getEscapedProfileUrl( false, 'memory' );
369 ?>">Memory (%
)</a
></th
>
371 echo getEscapedProfileUrl( false, 'count' );
374 echo getEscapedProfileUrl( false, 'calls_per_req' );
375 ?>">Calls
/req
</a
></th
>
377 echo getEscapedProfileUrl( false, 'time_per_call' );
380 echo getEscapedProfileUrl( false, 'memory_per_call' );
383 echo getEscapedProfileUrl( false, 'time_per_req' );
386 echo getEscapedProfileUrl( false, 'memory_per_req' );
392 profile_point
::$totaltime = 0.0;
393 profile_point
::$totalcount = 0;
394 profile_point
::$totalmemory = 0.0;
396 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
397 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
398 global $filter, $sort, $expand;
400 if ( $_expand === false ) {
404 return htmlspecialchars(
407 'filter' => $_filter ?
: $filter,
408 'sort' => $_sort ?
: $sort,
409 'expand' => implode( ',', array_keys( $_expand ) )
418 /** @var profile_point|false $last */
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();
427 if ( $last !== false ) {
428 if ( preg_match( '/^' . preg_quote( $last->name(), '/' ) . '/', $next->name() ) ) {
429 $last->add_child( $next );
434 if ( preg_match( '/^query: /', $next->name() ) ||
preg_match( '/^query-m: /', $next->name() ) ) {
435 $sqltotal +
= $next->time();
442 $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0 );
443 foreach ( $queries as $q ) {
448 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
449 @usort
( $points, 'compare_point' );
451 foreach ( $points as $point ) {
452 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) ) {
456 $point->display( $expand );
462 <p
>Total time
: <code
><?php
printf( '%5.02f', profile_point
::$totaltime ); ?
></code
></p
>
464 <p
>Total memory
: <code
><?php
printf( '%5.02f', profile_point
::$totalmemory / 1024 ); ?
></code
></p
>