5 * Copyright 2005 Kate Turner.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
28 // This endpoint is supposed to be independent of request cookies and other
29 // details of the session. Log warnings for violations of the no-session
31 define( 'MW_NO_SESSION', 'warn' );
33 ini_set( 'zlib.output_compression', 'off' );
35 $wgEnableProfileInfo = false;
36 require __DIR__
. '/includes/WebStart.php';
38 header( 'Content-Type: text/html; charset=utf-8' );
44 <meta charset
="UTF-8" />
45 <title
>Profiling data
</title
>
47 /* noc.wikimedia.org/base.css */
57 font
: 14px
/1.6 sans
-serif
;
67 text
-decoration
: none
;
71 text
-decoration
: underline
;
77 * Copyright 2012 Twitter, Inc
78 * Licensed under the Apache License v2.0
79 * http://www.apache.org/licenses/LICENSE-2.0
81 * Designed and built with all the love in the world @twitter by @mdo and @fat.
86 background
-color
: transparent
;
87 border
-collapse
: collapse
;
101 border
-top
: 1px solid
#ddd;
109 vertical
-align
: bottom
;
112 .table thead
:first
-child tr
:first
-child th
,
113 .table thead
:first
-child tr
:first
-child td
{
117 .table tbody + tbody
{
118 border
-top
: 2px solid
#ddd;
122 .table
-condensed td
{
126 .table
-striped tbody tr
:nth
-child(odd
) td
,
127 .table
-striped tbody tr
:nth
-child(odd
) th
{
128 background
-color
: #f9f9f9;
131 .table
-hover tbody tr
:hover td
,
132 .table
-hover tbody tr
:hover th
{
133 background
-color
: #f5f5f5;
139 border
-top
: 1px solid
#eee;
140 border
-bottom
: 1px solid
#fff;
147 if ( !$wgEnableProfileInfo ) {
148 echo '<p>Disabled</p>'
153 $dbr = wfGetDB( DB_REPLICA
);
155 if ( !$dbr->tableExists( 'profiling' ) ) {
156 echo '<p>No <code>profiling</code> table exists, so we can\'t show you anything.</p>'
157 . '<p>If you want to log profiling data, enable <code>$wgProfiler[\'output\'] = \'db\'</code>'
158 . ' in your StartProfiler.php and run <code>maintenance/update.php</code> to'
159 . ' create the profiling table.'
165 if ( isset( $_REQUEST['expand'] ) ) {
166 foreach ( explode( ',', $_REQUEST['expand'] ) as $f ) {
171 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
172 class profile_point
{
179 public static $totaltime, $totalmemory, $totalcount;
181 public function __construct( $name, $count, $time, $memory ) {
183 $this->count
= $count;
185 $this->memory
= $memory;
186 $this->children
= [];
189 public function add_child( $child ) {
190 $this->children
[] = $child;
193 public function display( $expand, $indent = 0.0 ) {
194 usort( $this->children
, 'compare_point' );
196 $ex = isset( $expand[$this->name()] );
198 $anchor = str_replace( '"', '', $this->name() );
201 if ( count( $this->children
) ) {
202 $url = getEscapedProfileUrl( false, false, $expand +
[ $this->name() => true ] );
203 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[+]</a>";
209 foreach ( $expand as $name => $ep ) {
210 if ( $name != $this->name() ) {
211 $e +
= [ $name => $ep ];
214 $url = getEscapedProfileUrl( false, false, $e );
215 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[–]</a>";
220 <div style
="margin-left: <?php echo (int)$indent; ?>em;">
221 <?php
echo htmlspecialchars( str_replace( ',', ', ', $this->name() ) ) . $extet ?
>
224 <?php
// phpcs:disable Generic.Files.LineLength,Generic.PHP.NoSilencedErrors ?>
225 <td
class="mw-profileinfo-timep"><?php
echo @wfPercent
( $this->time() / self
::$totaltime * 100 ); ?
></td
>
226 <td
class="mw-profileinfo-memoryp"><?php
echo @wfPercent
( $this->memory() / self
::$totalmemory * 100 ); ?
></td
>
227 <td
class="mw-profileinfo-count"><?php
echo $this->count(); ?
></td
>
228 <td
class="mw-profileinfo-cpr"><?php
echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ); ?
></td
>
229 <td
class="mw-profileinfo-tpc"><?php
echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ); ?
></td
>
230 <td
class="mw-profileinfo-mpc"><?php
echo round( sprintf( '%.2f', $this->memoryPerCall() / 1024 ), 2 ); ?
></td
>
231 <td
class="mw-profileinfo-tpr"><?php
echo @round
( sprintf( '%.2f', $this->time() / self
::$totalcount ), 2 ); ?
></td
>
232 <td
class="mw-profileinfo-mpr"><?php
echo @round
( sprintf( '%.2f', $this->memory() / self
::$totalcount / 1024 ), 2 ); ?
></td
>
233 <?php
// phpcs:enable ?>
237 foreach ( $this->children
as $child ) {
238 $child->display( $expand, $indent +
2 );
243 public function name() {
247 public function count() {
251 public function time() {
255 public function memory() {
256 return $this->memory
;
259 public function timePerCall() {
260 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
261 return @( $this->time
/ $this->count
);
264 public function memoryPerCall() {
265 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
266 return @( $this->memory
/ $this->count
);
269 public function callsPerRequest() {
270 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
271 return @( $this->count
/ self
::$totalcount );
274 public function timePerRequest() {
275 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
276 return @( $this->time
/ self
::$totalcount );
279 public function memoryPerRequest() {
280 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
281 return @( $this->memory
/ self
::$totalcount );
284 public function fmttime() {
285 return sprintf( '%5.02f', $this->time
);
289 function compare_point( profile_point
$a, profile_point
$b ) {
290 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
296 return strcmp( $a->name(), $b->name() );
297 // Sorted descending:
299 return $b->time() <=> $a->time();
301 return $b->memory() <=> $a->memory();
303 return $b->count() <=> $a->count();
304 case 'time_per_call':
305 return $b->timePerCall() <=> $a->timePerCall();
306 case 'memory_per_call':
307 return $b->memoryPerCall() <=> $a->memoryPerCall();
308 case 'calls_per_req':
309 return $b->callsPerRequest() <=> $a->callsPerRequest();
311 return $b->timePerRequest() <=> $a->timePerRequest();
312 case 'memory_per_req':
313 return $b->memoryPerRequest() <=> $a->memoryPerRequest();
317 $sorts = [ 'time', 'memory', 'count', 'calls_per_req', 'name',
318 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' ];
320 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) ) {
321 $sort = $_REQUEST['sort'];
329 [ 'ORDER BY' => 'pf_name ASC' ]
332 if ( isset( $_REQUEST['filter'] ) ) {
333 $filter = $_REQUEST['filter'];
339 <form method
="get" action
="profileinfo.php">
341 <input type
="text" name
="filter" value
="<?php echo htmlspecialchars( $filter ); ?>">
342 <input type
="hidden" name
="sort" value
="<?php echo htmlspecialchars( $sort ); ?>">
343 <input type
="hidden" name
="expand" value
="<?php
344 echo htmlspecialchars( implode( ",", array_keys( $expand ) ) );
346 <input type
="submit" value
="Filter">
350 <table
class="mw-profileinfo-table table table-striped table-hover">
354 echo getEscapedProfileUrl( false, 'name' );
357 echo getEscapedProfileUrl( false, 'time' );
358 ?>">Time (%
)</a
></th
>
360 echo getEscapedProfileUrl( false, 'memory' );
361 ?>">Memory (%
)</a
></th
>
363 echo getEscapedProfileUrl( false, 'count' );
366 echo getEscapedProfileUrl( false, 'calls_per_req' );
367 ?>">Calls
/req
</a
></th
>
369 echo getEscapedProfileUrl( false, 'time_per_call' );
372 echo getEscapedProfileUrl( false, 'memory_per_call' );
375 echo getEscapedProfileUrl( false, 'time_per_req' );
378 echo getEscapedProfileUrl( false, 'memory_per_req' );
384 profile_point
::$totaltime = 0.0;
385 profile_point
::$totalcount = 0;
386 profile_point
::$totalmemory = 0.0;
388 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
389 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
390 global $filter, $sort, $expand;
392 if ( $_expand === false ) {
396 return htmlspecialchars(
399 'filter' => $_filter ?
: $filter,
400 'sort' => $_sort ?
: $sort,
401 'expand' => implode( ',', array_keys( $_expand ) )
411 foreach ( $res as $o ) {
412 $next = new profile_point( $o->pf_name
, $o->pf_count
, $o->pf_time
, $o->pf_memory
);
413 if ( $next->name() == '-total' ||
$next->name() == 'main()' ) {
414 profile_point
::$totaltime = $next->time();
415 profile_point
::$totalcount = $next->count();
416 profile_point
::$totalmemory = $next->memory();
418 if ( $last !== false ) {
419 if ( preg_match( '/^' . preg_quote( $last->name(), '/' ) . '/', $next->name() ) ) {
420 $last->add_child( $next );
425 if ( preg_match( '/^query: /', $next->name() ) ||
preg_match( '/^query-m: /', $next->name() ) ) {
426 $sqltotal +
= $next->time();
433 $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0, 0 );
434 foreach ( $queries as $q ) {
439 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
440 @usort
( $points, 'compare_point' );
442 foreach ( $points as $point ) {
443 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) ) {
447 $point->display( $expand );
453 <p
>Total time
: <code
><?php
printf( '%5.02f', profile_point
::$totaltime ); ?
></code
></p
>
455 <p
>Total memory
: <code
><?php
printf( '%5.02f', profile_point
::$totalmemory / 1024 ); ?
></code
></p
>