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 ini_set( 'zlib.output_compression', 'off' );
30 $wgEnableProfileInfo = $wgProfileToDatabase = false;
31 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
32 require ( 'core/includes/WebStart.php' );
34 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
;
102 border
-top
: 1px solid
#ddd;
110 vertical
-align
: bottom
;
113 .table thead
:first
-child tr
:first
-child th
,
114 .table thead
:first
-child tr
:first
-child td
{
118 .table tbody + tbody
{
119 border
-top
: 2px solid
#ddd;
123 .table
-condensed td
{
127 .table
-striped tbody tr
:nth
-child(odd
) td
,
128 .table
-striped tbody tr
:nth
-child(odd
) th
{
129 background
-color
: #f9f9f9;
132 .table
-hover tbody tr
:hover td
,
133 .table
-hover tbody tr
:hover th
{
134 background
-color
: #f5f5f5;
140 border
-top
: 1px solid
#eee;
141 border
-bottom
: 1px solid
#fff;
149 if ( !$wgEnableProfileInfo ) {
150 echo '<p>Disabled</p>'
155 $dbr = wfGetDB( DB_SLAVE
);
157 if( !$dbr->tableExists( 'profiling' ) ) {
158 echo '<p>No <code>profiling</code> table exists, so we can\'t show you anything.</p>'
159 . '<p>If you want to log profiling data, create the table using '
160 . '<code>maintenance/archives/patch-profiling.sql</code> and enable '
161 . '<code>$wgProfileToDatabase</code>.</p>'
167 if ( isset( $_REQUEST['expand'] ) )
168 foreach( explode( ',', $_REQUEST['expand'] ) as $f )
171 class profile_point
{
177 static $totaltime, $totalmemory, $totalcount;
179 function __construct( $name, $count, $time, $memory ) {
181 $this->count
= $count;
183 $this->memory
= $memory;
184 $this->children
= array();
187 function add_child( $child ) {
188 $this->children
[] = $child;
191 function display( $expand, $indent = 0.0 ) {
192 usort( $this->children
, 'compare_point' );
194 $ex = isset( $expand[$this->name()] );
197 if ( count( $this->children
) ) {
198 $url = getEscapedProfileUrl( false, false, $expand +
array( $this->name() => true ) );
199 $extet = ' <a href="' . $url . '">[+]</a>';
205 foreach ( $expand as $name => $ep ) {
206 if ( $name != $this->name() ) {
207 $e +
= array( $name => $ep );
211 $extet = ' <a href="' . getEscapedProfileUrl( false, false, $e ) . '">[–]</a>';
215 <th
><div style
="margin-left: <?php echo (int)$indent; ?>em;">
216 <?php
echo htmlspecialchars( $this->name() ) . $extet ?
>
218 <td
class="mw-profileinfo-timep"><?php
echo @wfPercent
( $this->time() / self
::$totaltime * 100 ); ?
></td
>
219 <td
class="mw-profileinfo-memoryp"><?php
echo @wfPercent
( $this->memory() / self
::$totalmemory * 100 ); ?
></td
>
220 <td
class="mw-profileinfo-count"><?php
echo $this->count(); ?
></td
>
221 <td
class="mw-profileinfo-cpr"><?php
echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ); ?
></td
>
222 <td
class="mw-profileinfo-tpc"><?php
echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ); ?
></td
>
223 <td
class="mw-profileinfo-mpc"><?php
echo round( sprintf( '%.2f' ,$this->memoryPerCall() / 1024 ), 2 ); ?
></td
>
224 <td
class="mw-profileinfo-tpr"><?php
echo @round
( sprintf( '%.2f', $this->time() / self
::$totalcount ), 2 ); ?
></td
>
225 <td
class="mw-profileinfo-mpr"><?php
echo @round
( sprintf( '%.2f' ,$this->memory() / self
::$totalcount / 1024 ), 2 ); ?
></td
>
229 foreach ( $this->children
as $child ) {
230 $child->display( $expand, $indent +
2 );
248 return $this->memory
;
251 function timePerCall() {
252 return @( $this->time
/ $this->count
);
255 function memoryPerCall() {
256 return @( $this->memory
/ $this->count
);
259 function callsPerRequest() {
260 return @( $this->count
/ self
::$totalcount );
263 function timePerRequest() {
264 return @( $this->time
/ self
::$totalcount );
267 function memoryPerRequest() {
268 return @( $this->memory
/ self
::$totalcount );
272 return sprintf( '%5.02f', $this->time
);
276 function compare_point(profile_point
$a, profile_point
$b) {
280 return strcmp( $a->name(), $b->name() );
282 return $a->time() > $b->time() ?
-1 : 1;
284 return $a->memory() > $b->memory() ?
-1 : 1;
286 return $a->count() > $b->count() ?
-1 : 1;
287 case 'time_per_call':
288 return $a->timePerCall() > $b->timePerCall() ?
-1 : 1;
289 case 'memory_per_call':
290 return $a->memoryPerCall() > $b->memoryPerCall() ?
-1 : 1;
291 case 'calls_per_req':
292 return $a->callsPerRequest() > $b->callsPerRequest() ?
-1 : 1;
294 return $a->timePerRequest() > $b->timePerRequest() ?
-1 : 1;
295 case 'memory_per_req':
296 return $a->memoryPerRequest() > $b->memoryPerRequest() ?
-1 : 1;
300 $sorts = array( 'time', 'memory', 'count', 'calls_per_req', 'name',
301 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' );
303 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) )
304 $sort = $_REQUEST['sort'];
306 $res = $dbr->select( 'profiling', '*', array(), 'profileinfo.php', array( 'ORDER BY' => 'pf_name ASC' ) );
308 if (isset( $_REQUEST['filter'] ) )
309 $filter = $_REQUEST['filter'];
314 <form method
="get" action
="profileinfo.php">
316 <input type
="text" name
="filter" value
="<?php echo htmlspecialchars($filter); ?>">
317 <input type
="hidden" name
="sort" value
="<?php echo htmlspecialchars($sort); ?>">
318 <input type
="hidden" name
="expand" value
="<?php echo htmlspecialchars(implode(",", array_keys($expand))); ?>">
319 <input type
="submit" value
="Filter">
323 <table
class="mw-profileinfo-table table table-striped table-hover">
326 <th
><a href
="<?php echo getEscapedProfileUrl( false, 'name' ); ?>">Name
</a
></th
>
327 <th
><a href
="<?php echo getEscapedProfileUrl( false, 'time' ); ?>">Time (%
)</a
></th
>
328 <th
><a href
="<?php echo getEscapedProfileUrl( false, 'memory' ); ?>">Memory (%
)</a
></th
>
329 <th
><a href
="<?php echo getEscapedProfileUrl( false, 'count' ); ?>">Count
</a
></th
>
330 <th
><a href
="<?php echo getEscapedProfileUrl( false, 'calls_per_req' ); ?>">Calls
/req
</a
></th
>
331 <th
><a href
="<?php echo getEscapedProfileUrl( false, 'time_per_call' ); ?>">ms
/call
</a
></th
>
332 <th
><a href
="<?php echo getEscapedProfileUrl( false, 'memory_per_call' ); ?>">kb
/call
</a
></th
>
333 <th
><a href
="<?php echo getEscapedProfileUrl( false, 'time_per_req' ); ?>">ms
/req
</a
></th
>
334 <th
><a href
="<?php echo getEscapedProfileUrl( false, 'memory_per_req' ); ?>">kb
/req
</a
></th
>
339 profile_point
::$totaltime = 0.0;
340 profile_point
::$totalcount = 0;
341 profile_point
::$totalmemory = 0.0;
343 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
344 global $filter, $sort, $expand;
346 if ( $_expand === false )
349 return htmlspecialchars(
352 'filter' => $_filter ?
$_filter : $filter,
353 'sort' => $_sort ?
$_sort : $sort,
354 'expand' => implode( ',', array_keys( $_expand ) )
364 foreach( $res as $o ) {
365 $next = new profile_point( $o->pf_name
, $o->pf_count
, $o->pf_time
, $o->pf_memory
);
366 if( $next->name() == '-total' ) {
367 profile_point
::$totaltime = $next->time();
368 profile_point
::$totalcount = $next->count();
369 profile_point
::$totalmemory = $next->memory();
371 if ( $last !== false ) {
372 if ( preg_match( '/^'.preg_quote( $last->name(), '/' ).'/', $next->name() ) ) {
373 $last->add_child($next);
378 if ( preg_match( '/^query: /', $next->name() ) ||
preg_match( '/^query-m: /', $next->name() ) ) {
379 $sqltotal +
= $next->time();
386 $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0, 0 );
387 foreach ( $queries as $q )
391 usort( $points, 'compare_point' );
393 foreach ( $points as $point ) {
394 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) )
397 $point->display( $expand );
403 <p
>Total time
: <code
><?php
printf('%5.02f', profile_point
::$totaltime); ?
></code
></p
>
404 <p
>Total memory
: <code
><?php
printf('%5.02f', profile_point
::$totalmemory / 1024 ); ?
></code
></p
>