profileinfo.php 12 KB

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