profileinfo.php 12 KB

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