profileinfo.php 11 KB

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