profileinfo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. Enforce this constraint with respect to session use.
  40. define( 'MW_NO_SESSION', 1 );
  41. define( 'MW_ENTRY_POINT', 'profileinfo' );
  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. wfDeprecated( 'profileinfo.php', '1.34' );
  155. // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
  156. class profile_point {
  157. public $name;
  158. public $count;
  159. public $time;
  160. public $children;
  161. public static $totaltime, $totalmemory, $totalcount;
  162. public function __construct( $name, $count, $time, $memory ) {
  163. $this->name = $name;
  164. $this->count = $count;
  165. $this->time = $time;
  166. $this->memory = $memory;
  167. $this->children = [];
  168. }
  169. public function add_child( $child ) {
  170. $this->children[] = $child;
  171. }
  172. public function display( $expand, $indent = 0.0 ) {
  173. usort( $this->children, 'compare_point' );
  174. $ex = isset( $expand[$this->name()] );
  175. $anchor = str_replace( '"', '', $this->name() );
  176. if ( !$ex ) {
  177. if ( count( $this->children ) ) {
  178. $url = getEscapedProfileUrl( false, false, $expand + [ $this->name() => true ] );
  179. $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[+]</a>";
  180. } else {
  181. $extet = '';
  182. }
  183. } else {
  184. $e = [];
  185. foreach ( $expand as $name => $ep ) {
  186. if ( $name != $this->name() ) {
  187. $e += [ $name => $ep ];
  188. }
  189. }
  190. $url = getEscapedProfileUrl( false, false, $e );
  191. $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[–]</a>";
  192. }
  193. ?>
  194. <tr>
  195. <th>
  196. <div style="margin-left: <?php echo (int)$indent; ?>em;">
  197. <?php echo htmlspecialchars( str_replace( ',', ', ', $this->name() ) ) . $extet ?>
  198. </div>
  199. </th>
  200. <?php // phpcs:disable Generic.Files.LineLength,Generic.PHP.NoSilencedErrors ?>
  201. <td class="mw-profileinfo-timep"><?php echo @wfPercent( $this->time() / self::$totaltime * 100 ); ?></td>
  202. <td class="mw-profileinfo-memoryp"><?php echo @wfPercent( $this->memory() / self::$totalmemory * 100 ); ?></td>
  203. <td class="mw-profileinfo-count"><?php echo $this->count(); ?></td>
  204. <td class="mw-profileinfo-cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ); ?></td>
  205. <td class="mw-profileinfo-tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ); ?></td>
  206. <td class="mw-profileinfo-mpc"><?php echo round( sprintf( '%.2f', $this->memoryPerCall() / 1024 ), 2 ); ?></td>
  207. <td class="mw-profileinfo-tpr"><?php echo @round( sprintf( '%.2f', $this->time() / self::$totalcount ), 2 ); ?></td>
  208. <td class="mw-profileinfo-mpr"><?php echo @round( sprintf( '%.2f', $this->memory() / self::$totalcount / 1024 ), 2 ); ?></td>
  209. <?php // phpcs:enable ?>
  210. </tr>
  211. <?php
  212. if ( $ex ) {
  213. foreach ( $this->children as $child ) {
  214. $child->display( $expand, $indent + 2 );
  215. }
  216. }
  217. }
  218. public function name() {
  219. return $this->name;
  220. }
  221. public function count() {
  222. return $this->count;
  223. }
  224. public function time() {
  225. return $this->time;
  226. }
  227. public function memory() {
  228. return $this->memory;
  229. }
  230. public function timePerCall() {
  231. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  232. return @( $this->time / $this->count );
  233. }
  234. public function memoryPerCall() {
  235. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  236. return @( $this->memory / $this->count );
  237. }
  238. public function callsPerRequest() {
  239. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  240. return @( $this->count / self::$totalcount );
  241. }
  242. public function timePerRequest() {
  243. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  244. return @( $this->time / self::$totalcount );
  245. }
  246. public function memoryPerRequest() {
  247. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  248. return @( $this->memory / self::$totalcount );
  249. }
  250. public function fmttime() {
  251. return sprintf( '%5.02f', $this->time );
  252. }
  253. }
  254. function compare_point( profile_point $a, profile_point $b ) {
  255. // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
  256. global $sort;
  257. switch ( $sort ) {
  258. // Sorted ascending:
  259. case 'name':
  260. return strcmp( $a->name(), $b->name() );
  261. // Sorted descending:
  262. case 'time':
  263. return $b->time() <=> $a->time();
  264. case 'memory':
  265. return $b->memory() <=> $a->memory();
  266. case 'count':
  267. return $b->count() <=> $a->count();
  268. case 'time_per_call':
  269. return $b->timePerCall() <=> $a->timePerCall();
  270. case 'memory_per_call':
  271. return $b->memoryPerCall() <=> $a->memoryPerCall();
  272. case 'calls_per_req':
  273. return $b->callsPerRequest() <=> $a->callsPerRequest();
  274. case 'time_per_req':
  275. return $b->timePerRequest() <=> $a->timePerRequest();
  276. case 'memory_per_req':
  277. return $b->memoryPerRequest() <=> $a->memoryPerRequest();
  278. }
  279. }
  280. $sorts = [ 'time', 'memory', 'count', 'calls_per_req', 'name',
  281. 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' ];
  282. $sort = 'time';
  283. if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) ) {
  284. $sort = $_REQUEST['sort'];
  285. }
  286. $res = $dbr->select(
  287. 'profiling',
  288. '*',
  289. [],
  290. 'profileinfo.php',
  291. [ 'ORDER BY' => 'pf_name ASC' ]
  292. );
  293. $filter = $_REQUEST['filter'] ?? '';
  294. ?>
  295. <form method="get" action="profileinfo.php">
  296. <p>
  297. <input type="text" name="filter" value="<?php echo htmlspecialchars( $filter ); ?>">
  298. <input type="hidden" name="sort" value="<?php echo htmlspecialchars( $sort ); ?>">
  299. <input type="hidden" name="expand" value="<?php
  300. echo htmlspecialchars( implode( ",", array_keys( $expand ) ) );
  301. ?>">
  302. <input type="submit" value="Filter">
  303. </p>
  304. </form>
  305. <table class="mw-profileinfo-table table table-striped table-hover">
  306. <thead>
  307. <tr>
  308. <th><a href="<?php
  309. echo getEscapedProfileUrl( false, 'name' );
  310. ?>">Name</a></th>
  311. <th><a href="<?php
  312. echo getEscapedProfileUrl( false, 'time' );
  313. ?>">Time (%)</a></th>
  314. <th><a href="<?php
  315. echo getEscapedProfileUrl( false, 'memory' );
  316. ?>">Memory (%)</a></th>
  317. <th><a href="<?php
  318. echo getEscapedProfileUrl( false, 'count' );
  319. ?>">Count</a></th>
  320. <th><a href="<?php
  321. echo getEscapedProfileUrl( false, 'calls_per_req' );
  322. ?>">Calls/req</a></th>
  323. <th><a href="<?php
  324. echo getEscapedProfileUrl( false, 'time_per_call' );
  325. ?>">ms/call</a></th>
  326. <th><a href="<?php
  327. echo getEscapedProfileUrl( false, 'memory_per_call' );
  328. ?>">kb/call</a></th>
  329. <th><a href="<?php
  330. echo getEscapedProfileUrl( false, 'time_per_req' );
  331. ?>">ms/req</a></th>
  332. <th><a href="<?php
  333. echo getEscapedProfileUrl( false, 'memory_per_req' );
  334. ?>">kb/req</a></th>
  335. </tr>
  336. </thead>
  337. <tbody>
  338. <?php
  339. profile_point::$totaltime = 0.0;
  340. profile_point::$totalcount = 0;
  341. profile_point::$totalmemory = 0.0;
  342. function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
  343. // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
  344. global $filter, $sort, $expand;
  345. if ( $_expand === false ) {
  346. $_expand = $expand;
  347. }
  348. return htmlspecialchars(
  349. '?' .
  350. wfArrayToCgi( [
  351. 'filter' => $_filter ?: $filter,
  352. 'sort' => $_sort ?: $sort,
  353. 'expand' => implode( ',', array_keys( $_expand ) )
  354. ] )
  355. );
  356. }
  357. $points = [];
  358. $queries = [];
  359. $sqltotal = 0.0;
  360. /** @var profile_point|false $last */
  361. $last = false;
  362. foreach ( $res as $o ) {
  363. $next = new profile_point( $o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory );
  364. if ( $next->name() == '-total' || $next->name() == 'main()' ) {
  365. profile_point::$totaltime = $next->time();
  366. profile_point::$totalcount = $next->count();
  367. profile_point::$totalmemory = $next->memory();
  368. }
  369. if ( $last !== false ) {
  370. if ( preg_match( '/^' . preg_quote( $last->name(), '/' ) . '/', $next->name() ) ) {
  371. $last->add_child( $next );
  372. continue;
  373. }
  374. }
  375. $last = $next;
  376. if ( preg_match( '/^query: /', $next->name() ) || preg_match( '/^query-m: /', $next->name() ) ) {
  377. $sqltotal += $next->time();
  378. $queries[] = $next;
  379. } else {
  380. $points[] = $next;
  381. }
  382. }
  383. $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0 );
  384. foreach ( $queries as $q ) {
  385. $s->add_child( $q );
  386. }
  387. $points[] = $s;
  388. // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  389. @usort( $points, 'compare_point' );
  390. foreach ( $points as $point ) {
  391. if ( strlen( $filter ) && !strstr( $point->name(), $filter ) ) {
  392. continue;
  393. }
  394. $point->display( $expand );
  395. }
  396. ?>
  397. </tbody>
  398. </table>
  399. <hr />
  400. <p>Total time: <code><?php printf( '%5.02f', profile_point::$totaltime ); ?></code></p>
  401. <p>Total memory: <code><?php printf( '%5.02f', profile_point::$totalmemory / 1024 ); ?></code></p>
  402. <hr />
  403. </body>
  404. </html>