profileinfo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. /**
  3. * Simple interface for displaying request profile data stored in
  4. * the wikis' primary database.
  5. *
  6. * See also https://www.mediawiki.org/wiki/Manual:Profiling.
  7. *
  8. * To add profiling information to the database:
  9. *
  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.
  15. *
  16. * Copyright 2005 Kate Turner.
  17. *
  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:
  24. *
  25. * The above copyright notice and this permission notice shall be included in
  26. * all copies or substantial portions of the Software.
  27. *
  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
  34. * SOFTWARE.
  35. *
  36. * @file
  37. */
  38. // This endpoint is supposed to be independent of request cookies and other
  39. // details of the session. Log warnings for violations of the no-session
  40. // constraint.
  41. define( 'MW_NO_SESSION', 'warn' );
  42. ini_set( 'zlib.output_compression', 'off' );
  43. require __DIR__ . '/includes/WebStart.php';
  44. header( 'Content-Type: text/html; charset=utf-8' );
  45. ?>
  46. <!DOCTYPE html>
  47. <html>
  48. <head>
  49. <meta charset="UTF-8" />
  50. <title>Profiling data</title>
  51. <style>
  52. /* noc.wikimedia.org/base.css */
  53. * {
  54. margin: 0;
  55. padding: 0;
  56. }
  57. body {
  58. padding: 0.5em 1em;
  59. background: #fff;
  60. font: 14px/1.6 sans-serif;
  61. color: #333;
  62. }
  63. p, ul, ol, table {
  64. margin: 0.5em 0;
  65. }
  66. a {
  67. color: #0645AD;
  68. text-decoration: none;
  69. }
  70. a:hover {
  71. text-decoration: underline;
  72. }
  73. /*!
  74. * Bootstrap v2.1.1
  75. *
  76. * Copyright 2012 Twitter, Inc
  77. * Licensed under the Apache License v2.0
  78. * http://www.apache.org/licenses/LICENSE-2.0
  79. *
  80. * Designed and built with all the love in the world @twitter by @mdo and @fat.
  81. */
  82. table {
  83. max-width: 100%;
  84. background-color: transparent;
  85. border-collapse: collapse;
  86. border-spacing: 0;
  87. }
  88. .table {
  89. width: 100%;
  90. margin-bottom: 20px;
  91. }
  92. .table th,
  93. .table td {
  94. padding: 0.1em;
  95. text-align: left;
  96. vertical-align: top;
  97. border-top: 1px solid #ddd;
  98. }
  99. .table th {
  100. font-weight: bold;
  101. }
  102. .table thead th {
  103. vertical-align: bottom;
  104. }
  105. .table thead:first-child tr:first-child th,
  106. .table thead:first-child tr:first-child td {
  107. border-top: 0;
  108. }
  109. .table tbody + tbody {
  110. border-top: 2px solid #ddd;
  111. }
  112. .table-condensed th,
  113. .table-condensed td {
  114. padding: 4px 5px;
  115. }
  116. .table-striped tbody tr:nth-child(odd) td,
  117. .table-striped tbody tr:nth-child(odd) th {
  118. background-color: #f9f9f9;
  119. }
  120. .table-hover tbody tr:hover td,
  121. .table-hover tbody tr:hover th {
  122. background-color: #f5f5f5;
  123. }
  124. hr {
  125. margin: 20px 0;
  126. border: 0;
  127. border-top: 1px solid #eee;
  128. border-bottom: 1px solid #fff;
  129. }
  130. </style>
  131. </head>
  132. <body>
  133. <?php
  134. if ( !$wgEnableProfileInfo ) {
  135. echo '<p>Disabled</p>'
  136. . '</body></html>';
  137. exit( 1 );
  138. }
  139. $dbr = wfGetDB( DB_REPLICA );
  140. if ( !$dbr->tableExists( 'profiling' ) ) {
  141. echo '<p>No <code>profiling</code> table exists, so we can\'t show you anything.</p>'
  142. . '<p>If you want to log profiling data, enable <code>$wgProfiler[\'output\'] = \'db\'</code>'
  143. . ' in LocalSettings.php and run <code>maintenance/update.php</code> to'
  144. . ' create the profiling table.'
  145. . '</body></html>';
  146. exit( 1 );
  147. }
  148. $expand = [];
  149. if ( isset( $_REQUEST['expand'] ) ) {
  150. foreach ( explode( ',', $_REQUEST['expand'] ) as $f ) {
  151. $expand[$f] = true;
  152. }
  153. }
  154. // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
  155. class profile_point {
  156. public $name;
  157. public $count;
  158. public $time;
  159. public $children;
  160. public static $totaltime, $totalmemory, $totalcount;
  161. public function __construct( $name, $count, $time, $memory ) {
  162. $this->name = $name;
  163. $this->count = $count;
  164. $this->time = $time;
  165. $this->memory = $memory;
  166. $this->children = [];
  167. }
  168. public function add_child( $child ) {
  169. $this->children[] = $child;
  170. }
  171. public function display( $expand, $indent = 0.0 ) {
  172. usort( $this->children, 'compare_point' );
  173. $ex = isset( $expand[$this->name()] );
  174. $anchor = str_replace( '"', '', $this->name() );
  175. if ( !$ex ) {
  176. if ( count( $this->children ) ) {
  177. $url = getEscapedProfileUrl( false, false, $expand + [ $this->name() => true ] );
  178. $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[+]</a>";
  179. } else {
  180. $extet = '';
  181. }
  182. } else {
  183. $e = [];
  184. foreach ( $expand as $name => $ep ) {
  185. if ( $name != $this->name() ) {
  186. $e += [ $name => $ep ];
  187. }
  188. }
  189. $url = getEscapedProfileUrl( false, false, $e );
  190. $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[–]</a>";
  191. }
  192. ?>
  193. <tr>
  194. <th>
  195. <div style="margin-left: <?php echo (int)$indent; ?>em;">
  196. <?php echo htmlspecialchars( str_replace( ',', ', ', $this->name() ) ) . $extet ?>
  197. </div>
  198. </th>
  199. <?php // phpcs:disable Generic.Files.LineLength,Generic.PHP.NoSilencedErrors ?>
  200. <td class="mw-profileinfo-timep"><?php echo @wfPercent( $this->time() / self::$totaltime * 100 ); ?></td>
  201. <td class="mw-profileinfo-memoryp"><?php echo @wfPercent( $this->memory() / self::$totalmemory * 100 ); ?></td>
  202. <td class="mw-profileinfo-count"><?php echo $this->count(); ?></td>
  203. <td class="mw-profileinfo-cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ); ?></td>
  204. <td class="mw-profileinfo-tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ); ?></td>
  205. <td class="mw-profileinfo-mpc"><?php echo round( sprintf( '%.2f', $this->memoryPerCall() / 1024 ), 2 ); ?></td>
  206. <td class="mw-profileinfo-tpr"><?php echo @round( sprintf( '%.2f', $this->time() / self::$totalcount ), 2 ); ?></td>
  207. <td class="mw-profileinfo-mpr"><?php echo @round( sprintf( '%.2f', $this->memory() / self::$totalcount / 1024 ), 2 ); ?></td>
  208. <?php // phpcs:enable ?>
  209. </tr>
  210. <?php
  211. if ( $ex ) {
  212. foreach ( $this->children as $child ) {
  213. $child->display( $expand, $indent + 2 );
  214. }
  215. }
  216. }
  217. public function name() {
  218. return $this->name;
  219. }
  220. public function count() {
  221. return $this->count;
  222. }
  223. public function time() {
  224. return $this->time;
  225. }
  226. public function memory() {
  227. return $this->memory;
  228. }
  229. public function timePerCall() {
  230. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  231. return @( $this->time / $this->count );
  232. }
  233. public function memoryPerCall() {
  234. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  235. return @( $this->memory / $this->count );
  236. }
  237. public function callsPerRequest() {
  238. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  239. return @( $this->count / self::$totalcount );
  240. }
  241. public function timePerRequest() {
  242. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  243. return @( $this->time / self::$totalcount );
  244. }
  245. public function memoryPerRequest() {
  246. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  247. return @( $this->memory / self::$totalcount );
  248. }
  249. public function fmttime() {
  250. return sprintf( '%5.02f', $this->time );
  251. }
  252. }
  253. function compare_point( profile_point $a, profile_point $b ) {
  254. // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
  255. global $sort;
  256. switch ( $sort ) {
  257. // Sorted ascending:
  258. case 'name':
  259. return strcmp( $a->name(), $b->name() );
  260. // Sorted descending:
  261. case 'time':
  262. return $b->time() <=> $a->time();
  263. case 'memory':
  264. return $b->memory() <=> $a->memory();
  265. case 'count':
  266. return $b->count() <=> $a->count();
  267. case 'time_per_call':
  268. return $b->timePerCall() <=> $a->timePerCall();
  269. case 'memory_per_call':
  270. return $b->memoryPerCall() <=> $a->memoryPerCall();
  271. case 'calls_per_req':
  272. return $b->callsPerRequest() <=> $a->callsPerRequest();
  273. case 'time_per_req':
  274. return $b->timePerRequest() <=> $a->timePerRequest();
  275. case 'memory_per_req':
  276. return $b->memoryPerRequest() <=> $a->memoryPerRequest();
  277. }
  278. }
  279. $sorts = [ 'time', 'memory', 'count', 'calls_per_req', 'name',
  280. 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' ];
  281. $sort = 'time';
  282. if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) ) {
  283. $sort = $_REQUEST['sort'];
  284. }
  285. $res = $dbr->select(
  286. 'profiling',
  287. '*',
  288. [],
  289. 'profileinfo.php',
  290. [ 'ORDER BY' => 'pf_name ASC' ]
  291. );
  292. if ( isset( $_REQUEST['filter'] ) ) {
  293. $filter = $_REQUEST['filter'];
  294. } else {
  295. $filter = '';
  296. }
  297. ?>
  298. <form method="get" action="profileinfo.php">
  299. <p>
  300. <input type="text" name="filter" value="<?php echo htmlspecialchars( $filter ); ?>">
  301. <input type="hidden" name="sort" value="<?php echo htmlspecialchars( $sort ); ?>">
  302. <input type="hidden" name="expand" value="<?php
  303. echo htmlspecialchars( implode( ",", array_keys( $expand ) ) );
  304. ?>">
  305. <input type="submit" value="Filter">
  306. </p>
  307. </form>
  308. <table class="mw-profileinfo-table table table-striped table-hover">
  309. <thead>
  310. <tr>
  311. <th><a href="<?php
  312. echo getEscapedProfileUrl( false, 'name' );
  313. ?>">Name</a></th>
  314. <th><a href="<?php
  315. echo getEscapedProfileUrl( false, 'time' );
  316. ?>">Time (%)</a></th>
  317. <th><a href="<?php
  318. echo getEscapedProfileUrl( false, 'memory' );
  319. ?>">Memory (%)</a></th>
  320. <th><a href="<?php
  321. echo getEscapedProfileUrl( false, 'count' );
  322. ?>">Count</a></th>
  323. <th><a href="<?php
  324. echo getEscapedProfileUrl( false, 'calls_per_req' );
  325. ?>">Calls/req</a></th>
  326. <th><a href="<?php
  327. echo getEscapedProfileUrl( false, 'time_per_call' );
  328. ?>">ms/call</a></th>
  329. <th><a href="<?php
  330. echo getEscapedProfileUrl( false, 'memory_per_call' );
  331. ?>">kb/call</a></th>
  332. <th><a href="<?php
  333. echo getEscapedProfileUrl( false, 'time_per_req' );
  334. ?>">ms/req</a></th>
  335. <th><a href="<?php
  336. echo getEscapedProfileUrl( false, 'memory_per_req' );
  337. ?>">kb/req</a></th>
  338. </tr>
  339. </thead>
  340. <tbody>
  341. <?php
  342. profile_point::$totaltime = 0.0;
  343. profile_point::$totalcount = 0;
  344. profile_point::$totalmemory = 0.0;
  345. function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
  346. // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
  347. global $filter, $sort, $expand;
  348. if ( $_expand === false ) {
  349. $_expand = $expand;
  350. }
  351. return htmlspecialchars(
  352. '?' .
  353. wfArrayToCgi( [
  354. 'filter' => $_filter ?: $filter,
  355. 'sort' => $_sort ?: $sort,
  356. 'expand' => implode( ',', array_keys( $_expand ) )
  357. ] )
  358. );
  359. }
  360. $points = [];
  361. $queries = [];
  362. $sqltotal = 0.0;
  363. $last = false;
  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' || $next->name() == 'main()' ) {
  367. profile_point::$totaltime = $next->time();
  368. profile_point::$totalcount = $next->count();
  369. profile_point::$totalmemory = $next->memory();
  370. }
  371. if ( $last !== false ) {
  372. if ( preg_match( '/^' . preg_quote( $last->name(), '/' ) . '/', $next->name() ) ) {
  373. $last->add_child( $next );
  374. continue;
  375. }
  376. }
  377. $last = $next;
  378. if ( preg_match( '/^query: /', $next->name() ) || preg_match( '/^query-m: /', $next->name() ) ) {
  379. $sqltotal += $next->time();
  380. $queries[] = $next;
  381. } else {
  382. $points[] = $next;
  383. }
  384. }
  385. $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0, 0 );
  386. foreach ( $queries as $q ) {
  387. $s->add_child( $q );
  388. }
  389. $points[] = $s;
  390. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  391. @usort( $points, 'compare_point' );
  392. foreach ( $points as $point ) {
  393. if ( strlen( $filter ) && !strstr( $point->name(), $filter ) ) {
  394. continue;
  395. }
  396. $point->display( $expand );
  397. }
  398. ?>
  399. </tbody>
  400. </table>
  401. <hr />
  402. <p>Total time: <code><?php printf( '%5.02f', profile_point::$totaltime ); ?></code></p>
  403. <p>Total memory: <code><?php printf( '%5.02f', profile_point::$totalmemory / 1024 ); ?></code></p>
  404. <hr />
  405. </body>
  406. </html>