MediaWiki  REL1_31
profileinfo.php
Go to the documentation of this file.
1 <?php
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
30 // constraint.
31 define( 'MW_NO_SESSION', 'warn' );
32 
33 ini_set( 'zlib.output_compression', 'off' );
34 
36 require __DIR__ . '/includes/WebStart.php';
37 
38 header( 'Content-Type: text/html; charset=utf-8' );
39 
40 ?>
41 <!DOCTYPE html>
42 <html>
43 <head>
44  <meta charset="UTF-8" />
45  <title>Profiling data</title>
46  <style>
47  /* noc.wikimedia.org/base.css */
48 
49  * {
50  margin: 0;
51  padding: 0;
52  }
53 
54  body {
55  padding: 0.5em 1em;
56  background: #fff;
57  font: 14px/1.6 sans-serif;
58  color: #333;
59  }
60 
61  p, ul, ol, table {
62  margin: 0.5em 0;
63  }
64 
65  a {
66  color: #0645AD;
67  text-decoration: none;
68  }
69 
70  a:hover {
71  text-decoration: underline;
72  }
73 
84  table {
85  max-width: 100%;
86  background-color: transparent;
87  border-collapse: collapse;
88  border-spacing: 0;
89  }
90 
91  .table {
92  width: 100%;
93  margin-bottom: 20px;
94  }
95 
96  .table th,
97  .table td {
98  padding: 0.1em;
99  text-align: left;
100  vertical-align: top;
101  border-top: 1px solid #ddd;
102  }
103 
104  .table th {
105  font-weight: bold;
106  }
107 
108  .table thead th {
109  vertical-align: bottom;
110  }
111 
112  .table thead:first-child tr:first-child th,
113  .table thead:first-child tr:first-child td {
114  border-top: 0;
115  }
116 
117  .table tbody + tbody {
118  border-top: 2px solid #ddd;
119  }
120 
121  .table-condensed th,
122  .table-condensed td {
123  padding: 4px 5px;
124  }
125 
126  .table-striped tbody tr:nth-child(odd) td,
127  .table-striped tbody tr:nth-child(odd) th {
128  background-color: #f9f9f9;
129  }
130 
131  .table-hover tbody tr:hover td,
132  .table-hover tbody tr:hover th {
133  background-color: #f5f5f5;
134  }
135 
136  hr {
137  margin: 20px 0;
138  border: 0;
139  border-top: 1px solid #eee;
140  border-bottom: 1px solid #fff;
141  }
142  </style>
143 </head>
144 <body>
145 <?php
146 
147 if ( !$wgEnableProfileInfo ) {
148  echo '<p>Disabled</p>'
149  . '</body></html>';
150  exit( 1 );
151 }
152 
154 
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.'
160  . '</body></html>';
161  exit( 1 );
162 }
163 
164 $expand = [];
165 if ( isset( $_REQUEST['expand'] ) ) {
166  foreach ( explode( ',', $_REQUEST['expand'] ) as $f ) {
167  $expand[$f] = true;
168  }
169 }
170 
171 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
173 
174  public $name;
175  public $count;
176  public $time;
177  public $children;
178 
180 
181  public function __construct( $name, $count, $time, $memory ) {
182  $this->name = $name;
183  $this->count = $count;
184  $this->time = $time;
185  $this->memory = $memory;
186  $this->children = [];
187  }
188 
189  public function add_child( $child ) {
190  $this->children[] = $child;
191  }
192 
193  public function display( $expand, $indent = 0.0 ) {
194  usort( $this->children, 'compare_point' );
195 
196  $ex = isset( $expand[$this->name()] );
197 
198  $anchor = str_replace( '"', '', $this->name() );
199 
200  if ( !$ex ) {
201  if ( count( $this->children ) ) {
202  $url = getEscapedProfileUrl( false, false, $expand + [ $this->name() => true ] );
203  $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[+]</a>";
204  } else {
205  $extet = '';
206  }
207  } else {
208  $e = [];
209  foreach ( $expand as $name => $ep ) {
210  if ( $name != $this->name() ) {
211  $e += [ $name => $ep ];
212  }
213  }
214  $url = getEscapedProfileUrl( false, false, $e );
215  $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[–]</a>";
216  }
217  ?>
218  <tr>
219  <th>
220  <div style="margin-left: <?php echo (int)$indent; ?>em;">
221  <?php echo htmlspecialchars( str_replace( ',', ', ', $this->name() ) ) . $extet ?>
222  </div>
223  </th>
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 ?>
234  </tr>
235  <?php
236  if ( $ex ) {
237  foreach ( $this->children as $child ) {
238  $child->display( $expand, $indent + 2 );
239  }
240  }
241  }
242 
243  public function name() {
244  return $this->name;
245  }
246 
247  public function count() {
248  return $this->count;
249  }
250 
251  public function time() {
252  return $this->time;
253  }
254 
255  public function memory() {
256  return $this->memory;
257  }
258 
259  public function timePerCall() {
260  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
261  return @( $this->time / $this->count );
262  }
263 
264  public function memoryPerCall() {
265  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
266  return @( $this->memory / $this->count );
267  }
268 
269  public function callsPerRequest() {
270  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
271  return @( $this->count / self::$totalcount );
272  }
273 
274  public function timePerRequest() {
275  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
276  return @( $this->time / self::$totalcount );
277  }
278 
279  public function memoryPerRequest() {
280  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
281  return @( $this->memory / self::$totalcount );
282  }
283 
284  public function fmttime() {
285  return sprintf( '%5.02f', $this->time );
286  }
287 };
288 
290  // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
291  global $sort;
292 
293  switch ( $sort ) {
294  case 'name':
295  return strcmp( $a->name(), $b->name() );
296  case 'time':
297  return $a->time() > $b->time() ? -1 : 1;
298  case 'memory':
299  return $a->memory() > $b->memory() ? -1 : 1;
300  case 'count':
301  return $a->count() > $b->count() ? -1 : 1;
302  case 'time_per_call':
303  return $a->timePerCall() > $b->timePerCall() ? -1 : 1;
304  case 'memory_per_call':
305  return $a->memoryPerCall() > $b->memoryPerCall() ? -1 : 1;
306  case 'calls_per_req':
307  return $a->callsPerRequest() > $b->callsPerRequest() ? -1 : 1;
308  case 'time_per_req':
309  return $a->timePerRequest() > $b->timePerRequest() ? -1 : 1;
310  case 'memory_per_req':
311  return $a->memoryPerRequest() > $b->memoryPerRequest() ? -1 : 1;
312  }
313 }
314 
315 $sorts = [ 'time', 'memory', 'count', 'calls_per_req', 'name',
316  'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' ];
317 $sort = 'time';
318 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) ) {
319  $sort = $_REQUEST['sort'];
320 }
321 
322 $res = $dbr->select(
323  'profiling',
324  '*',
325  [],
326  'profileinfo.php',
327  [ 'ORDER BY' => 'pf_name ASC' ]
328 );
329 
330 if ( isset( $_REQUEST['filter'] ) ) {
331  $filter = $_REQUEST['filter'];
332 } else {
333  $filter = '';
334 }
335 
336 ?>
337 <form method="get" action="profileinfo.php">
338  <p>
339  <input type="text" name="filter" value="<?php echo htmlspecialchars( $filter ); ?>">
340  <input type="hidden" name="sort" value="<?php echo htmlspecialchars( $sort ); ?>">
341  <input type="hidden" name="expand" value="<?php
342  echo htmlspecialchars( implode( ",", array_keys( $expand ) ) );
343  ?>">
344  <input type="submit" value="Filter">
345  </p>
346 </form>
347 
348 <table class="mw-profileinfo-table table table-striped table-hover">
349  <thead>
350  <tr>
351  <th><a href="<?php
352  echo getEscapedProfileUrl( false, 'name' );
353  ?>">Name</a></th>
354  <th><a href="<?php
355  echo getEscapedProfileUrl( false, 'time' );
356  ?>">Time (%)</a></th>
357  <th><a href="<?php
358  echo getEscapedProfileUrl( false, 'memory' );
359  ?>">Memory (%)</a></th>
360  <th><a href="<?php
361  echo getEscapedProfileUrl( false, 'count' );
362  ?>">Count</a></th>
363  <th><a href="<?php
364  echo getEscapedProfileUrl( false, 'calls_per_req' );
365  ?>">Calls/req</a></th>
366  <th><a href="<?php
367  echo getEscapedProfileUrl( false, 'time_per_call' );
368  ?>">ms/call</a></th>
369  <th><a href="<?php
370  echo getEscapedProfileUrl( false, 'memory_per_call' );
371  ?>">kb/call</a></th>
372  <th><a href="<?php
373  echo getEscapedProfileUrl( false, 'time_per_req' );
374  ?>">ms/req</a></th>
375  <th><a href="<?php
376  echo getEscapedProfileUrl( false, 'memory_per_req' );
377  ?>">kb/req</a></th>
378  </tr>
379  </thead>
380  <tbody>
381  <?php
385 
386  function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
387  // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
388  global $filter, $sort, $expand;
389 
390  if ( $_expand === false ) {
391  $_expand = $expand;
392  }
393 
394  return htmlspecialchars(
395  '?' .
396  wfArrayToCgi( [
397  'filter' => $_filter ? $_filter : $filter,
398  'sort' => $_sort ? $_sort : $sort,
399  'expand' => implode( ',', array_keys( $_expand ) )
400  ] )
401  );
402  }
403 
404  $points = [];
405  $queries = [];
406  $sqltotal = 0.0;
407 
408  $last = false;
409  foreach ( $res as $o ) {
410  $next = new profile_point( $o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory );
411  if ( $next->name() == '-total' || $next->name() == 'main()' ) {
412  profile_point::$totaltime = $next->time();
413  profile_point::$totalcount = $next->count();
414  profile_point::$totalmemory = $next->memory();
415  }
416  if ( $last !== false ) {
417  if ( preg_match( '/^' . preg_quote( $last->name(), '/' ) . '/', $next->name() ) ) {
418  $last->add_child( $next );
419  continue;
420  }
421  }
422  $last = $next;
423  if ( preg_match( '/^query: /', $next->name() ) || preg_match( '/^query-m: /', $next->name() ) ) {
424  $sqltotal += $next->time();
425  $queries[] = $next;
426  } else {
427  $points[] = $next;
428  }
429  }
430 
431  $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0, 0 );
432  foreach ( $queries as $q ) {
433  $s->add_child( $q );
434  }
435  $points[] = $s;
436 
437  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
438  @usort( $points, 'compare_point' );
439 
440  foreach ( $points as $point ) {
441  if ( strlen( $filter ) && !strstr( $point->name(), $filter ) ) {
442  continue;
443  }
444 
445  $point->display( $expand );
446  }
447  ?>
448  </tbody>
449 </table>
450 <hr />
451 <p>Total time: <code><?php printf( '%5.02f', profile_point::$totaltime ); ?></code></p>
452 
453 <p>Total memory: <code><?php printf( '%5.02f', profile_point::$totalmemory / 1024 ); ?></code></p>
454 <hr />
455 </body>
456 </html>
code
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of or an object and a method hook function The function part of a third party developers and administrators to define code that will be run at certain points in the mainline code
Definition: hooks.txt:28
wfPercent
wfPercent( $nr, $acc=2, $round=true)
Definition: GlobalFunctions.php:2144
profile_point\callsPerRequest
callsPerRequest()
Definition: profileinfo.php:269
type
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition: postgres.txt:30
profile_point\$name
$name
Definition: profileinfo.php:174
compare_point
compare_point(profile_point $a, profile_point $b)
Definition: profileinfo.php:289
$last
$last
Definition: profileinfo.php:408
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:18
$sqltotal
$sqltotal
Definition: profileinfo.php:406
profile_point\__construct
__construct( $name, $count, $time, $memory)
Definition: profileinfo.php:181
profile_point\timePerRequest
timePerRequest()
Definition: profileinfo.php:274
profile_point\$totalmemory
static $totalmemory
Definition: profileinfo.php:179
profile_point\$time
$time
Definition: profileinfo.php:176
$wgEnableProfileInfo
$wgEnableProfileInfo
Definition: profileinfo.php:35
$dbr
if(! $wgEnableProfileInfo) $dbr
Definition: profileinfo.php:153
profile_point\$count
$count
Definition: profileinfo.php:175
$s
foreach( $res as $o) $s
Definition: profileinfo.php:431
profile_point\$children
$children
Definition: profileinfo.php:177
memory
MediaWiki has optional support for a high distributed memory object caching system For general information on but for a larger site with heavy like it should help lighten the load on the database servers by caching data and objects in memory
Definition: memcached.txt:10
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
profile_point\$totaltime
static $totaltime
Definition: profileinfo.php:179
width
width
Definition: parserTests.txt:163
profile_point\memoryPerCall
memoryPerCall()
Definition: profileinfo.php:264
$expand
if(! $dbr->tableExists( 'profiling')) $expand
Definition: profileinfo.php:164
profile_point\display
display( $expand, $indent=0.0)
Definition: profileinfo.php:193
table
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if so it s not worth the trouble Since there is a job queue in the jobs table
Definition: deferred.txt:16
$sorts
$sorts
Definition: profileinfo.php:315
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2812
$points
$points
Definition: profileinfo.php:404
title
title
Definition: parserTests.txt:219
getEscapedProfileUrl
getEscapedProfileUrl( $_filter=false, $_sort=false, $_expand=false)
Definition: profileinfo.php:386
profile_point\memory
memory()
Definition: profileinfo.php:255
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
div
div
Definition: parserTests.txt:6593
profile_point\$totalcount
static $totalcount
Definition: profileinfo.php:179
$sort
$sort
Definition: profileinfo.php:317
f9f9f9
f9f9f9
Definition: parserTests.txt:157
style
Bar style
Definition: parserTests.txt:192
$res
if(isset( $_REQUEST['sort']) &&in_array( $_REQUEST['sort'], $sorts)) $res
Definition: profileinfo.php:322
profile_point
Definition: profileinfo.php:172
profile_point\timePerCall
timePerCall()
Definition: profileinfo.php:259
profile_point\memoryPerRequest
memoryPerRequest()
Definition: profileinfo.php:279
profile_point\fmttime
fmttime()
Definition: profileinfo.php:284
profile_point\count
count()
Definition: profileinfo.php:247
color
in the sidebar</td >< td > font color
Definition: All_system_messages.txt:425
profile_point\time
time()
Definition: profileinfo.php:251
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
profile_point\name
name()
Definition: profileinfo.php:243
name
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at name
Definition: design.txt:12
profile_point\add_child
add_child( $child)
Definition: profileinfo.php:189
href
shown</td >< td > a href
Definition: All_system_messages.txt:2667
$queries
$queries
Definition: profileinfo.php:405
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2171
data
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of data
Definition: hooks.txt:18
form
null means default in associative array form
Definition: hooks.txt:1996
wfArrayToCgi
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
Definition: GlobalFunctions.php:377