profileinfo.php 12 KB

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