profileinfo.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. ini_set( 'zlib.output_compression', 'off' );
  3. $wgDBadminuser = $wgDBadminpassword = $wgDBserver = $wgDBname = $wgDBprefix = false;
  4. $wgEnableProfileInfo = $wgProfileToDatabase = false;
  5. define( 'MW_NO_SETUP', 1 );
  6. require_once( './includes/WebStart.php' );
  7. @include_once( './AdminSettings.php' );
  8. require_once( './includes/GlobalFunctions.php' );
  9. ?>
  10. <!--
  11. Show profiling data.
  12. Copyright 2005 Kate Turner.
  13. Permission is hereby granted, free of charge, to any person obtaining a copy
  14. of this software and associated documentation files (the "Software"), to deal
  15. in the Software without restriction, including without limitation the rights
  16. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. copies of the Software, and to permit persons to whom the Software is
  18. furnished to do so, subject to the following conditions:
  19. The above copyright notice and this permission notice shall be included in
  20. all copies or substantial portions of the Software.
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. SOFTWARE.
  28. -->
  29. <html>
  30. <head>
  31. <title>Profiling data</title>
  32. <style type="text/css">
  33. th {
  34. text-align: left;
  35. border-bottom: solid 1px black;
  36. }
  37. th, td {
  38. padding-left: 0.5em;
  39. padding-right: 0.5em;
  40. }
  41. td.timep, td.memoryp, td.count, td.cpr, td.tpc, td.mpc, td.tpr, td.mpr {
  42. text-align: right;
  43. }
  44. td.timep, td.tpc, td.tpr {
  45. background-color: #fffff0;
  46. }
  47. td.memoryp, td.mpc, td.mpr {
  48. background-color: #f0f8ff;
  49. }
  50. td.count, td,cpr {
  51. background-color: #f0fff0;
  52. }
  53. td.name {
  54. background-color: #f9f9f9;
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <?php
  60. if (!$wgEnableProfileInfo) {
  61. echo "disabled\n";
  62. exit( 1 );
  63. }
  64. foreach (array("wgDBadminuser", "wgDBadminpassword", "wgDBserver", "wgDBname") as $var)
  65. if ($$var === false) {
  66. echo "AdminSettings.php not correct\n";
  67. exit( 1 );
  68. }
  69. $expand = array();
  70. if (isset($_REQUEST['expand']))
  71. foreach(explode(",", $_REQUEST['expand']) as $f)
  72. $expand[$f] = true;
  73. class profile_point {
  74. var $name;
  75. var $count;
  76. var $time;
  77. var $children;
  78. function profile_point($name, $count, $time, $memory ) {
  79. $this->name = $name;
  80. $this->count = $count;
  81. $this->time = $time;
  82. $this->memory = $memory;
  83. $this->children = array();
  84. }
  85. function add_child($child) {
  86. $this->children[] = $child;
  87. }
  88. function display($indent = 0.0) {
  89. global $expand, $totaltime, $totalmemory, $totalcount;
  90. usort($this->children, "compare_point");
  91. $extet = '';
  92. if (isset($expand[$this->name()]))
  93. $ex = true;
  94. else $ex = false;
  95. if (!$ex) {
  96. if (count($this->children)) {
  97. $url = makeurl(false, false, $expand + array($this->name() => true));
  98. $extet = " <a href=\"$url\">[+]</a>";
  99. } else $extet = '';
  100. } else {
  101. $e = array();
  102. foreach ($expand as $name => $ep)
  103. if ($name != $this->name())
  104. $e += array($name => $ep);
  105. $extet = " <a href=\"" . makeurl(false, false, $e) . "\">[&ndash;]</a>";
  106. }
  107. ?>
  108. <tr>
  109. <td class="name" style="padding-left: <?php echo $indent ?>em;">
  110. <?php echo htmlspecialchars($this->name()) . $extet ?>
  111. </td>
  112. <td class="timep"><?php echo @wfPercent( $this->time() / $totaltime * 100 ) ?></td>
  113. <td class="memoryp"><?php echo @wfPercent( $this->memory() / $totalmemory * 100 ) ?></td>
  114. <td class="count"><?php echo $this->count() ?></td>
  115. <td class="cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ) ?></td>
  116. <td class="tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ) ?></td>
  117. <td class="mpc"><?php echo round( sprintf( '%.2f' ,$this->memoryPerCall() / 1024 ), 2 ) ?></td>
  118. <td class="tpr"><?php echo @round( sprintf( '%.2f', $this->time() / $totalcount ), 2 ) ?></td>
  119. <td class="mpr"><?php echo @round( sprintf( '%.2f' ,$this->memory() / $totalcount / 1024 ), 2 ) ?></td>
  120. </tr>
  121. <?php
  122. if ($ex) {
  123. foreach ($this->children as $child) {
  124. $child->display($indent + 2);
  125. }
  126. }
  127. }
  128. function name() {
  129. return $this->name;
  130. }
  131. function count() {
  132. return $this->count;
  133. }
  134. function time() {
  135. return $this->time;
  136. }
  137. function memory() {
  138. return $this->memory;
  139. }
  140. function timePerCall() {
  141. return @($this->time / $this->count);
  142. }
  143. function memoryPerCall() {
  144. return @($this->memory / $this->count);
  145. }
  146. function callsPerRequest() {
  147. global $totalcount;
  148. return @($this->count / $totalcount);
  149. }
  150. function timePerRequest() {
  151. global $totalcount;
  152. return @($this->time / $totalcount);
  153. }
  154. function memoryPerRequest() {
  155. global $totalcount;
  156. return @($this->memory / $totalcount);
  157. }
  158. function fmttime() {
  159. return sprintf("%5.02f", $this->time);
  160. }
  161. };
  162. function compare_point($a, $b) {
  163. global $sort;
  164. switch ($sort) {
  165. case "name":
  166. return strcmp($a->name(), $b->name());
  167. case "time":
  168. return $a->time() > $b->time() ? -1 : 1;
  169. case "memory":
  170. return $a->memory() > $b->memory() ? -1 : 1;
  171. case "count":
  172. return $a->count() > $b->count() ? -1 : 1;
  173. case "time_per_call":
  174. return $a->timePerCall() > $b->timePerCall() ? -1 : 1;
  175. case "memory_per_call":
  176. return $a->memoryPerCall() > $b->memoryPerCall() ? -1 : 1;
  177. case "calls_per_req":
  178. return $a->callsPerRequest() > $b->callsPerRequest() ? -1 : 1;
  179. case "time_per_req":
  180. return $a->timePerRequest() > $b->timePerRequest() ? -1 : 1;
  181. case "memory_per_req":
  182. return $a->memoryPerRequest() > $b->memoryPerRequest() ? -1 : 1;
  183. }
  184. }
  185. $sorts = array("time","memory","count","calls_per_req","name","time_per_call","memory_per_call","time_per_req","memory_per_req");
  186. $sort = 'time';
  187. if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
  188. $sort = $_REQUEST['sort'];
  189. $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
  190. or die("mysql server failed: " . mysql_error());
  191. mysql_select_db($wgDBname, $dbh) or die(mysql_error($dbh));
  192. $res = mysql_query("
  193. SELECT pf_count, pf_time, pf_memory, pf_name
  194. FROM {$wgDBprefix}profiling
  195. ORDER BY pf_name ASC
  196. ", $dbh) or die("query failed: " . mysql_error());
  197. if (isset($_REQUEST['filter']))
  198. $filter = $_REQUEST['filter'];
  199. else $filter = '';
  200. ?>
  201. <form method="profiling.php">
  202. <p>
  203. <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
  204. <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
  205. <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
  206. <input type="submit" value="Filter" />
  207. </p>
  208. </form>
  209. <table cellspacing="0" border="1">
  210. <tr id="top">
  211. <th><a href="<?php echo makeurl(false, "name") ?>">Name</a></th>
  212. <th><a href="<?php echo makeurl(false, "time") ?>">Time (%)</a></th>
  213. <th><a href="<?php echo makeurl(false, "memory") ?>">Memory (%)</a></th>
  214. <th><a href="<?php echo makeurl(false, "count") ?>">Count</a></th>
  215. <th><a href="<?php echo makeurl(false, "calls_per_req") ?>">Calls/req</a></th>
  216. <th><a href="<?php echo makeurl(false, "time_per_call") ?>">ms/call</a></th>
  217. <th><a href="<?php echo makeurl(false, "memory_per_call") ?>">kb/call</a></th>
  218. <th><a href="<?php echo makeurl(false, "time_per_req") ?>">ms/req</a></th>
  219. <th><a href="<?php echo makeurl(false, "memory_per_req") ?>">kb/req</a></th>
  220. </tr>
  221. <?php
  222. $totaltime = 0.0;
  223. $totalcount = 0;
  224. $totalmemory = 0.0;
  225. function makeurl($_filter = false, $_sort = false, $_expand = false) {
  226. global $filter, $sort, $expand;
  227. if ($_expand === false)
  228. $_expand = $expand;
  229. $nfilter = $_filter ? $_filter : $filter;
  230. $nsort = $_sort ? $_sort : $sort;
  231. $exp = urlencode(implode(',', array_keys($_expand)));
  232. return "?filter=$nfilter&amp;sort=$nsort&amp;expand=$exp";
  233. }
  234. $points = array();
  235. $queries = array();
  236. $sqltotal = 0.0;
  237. $last = false;
  238. while (($o = mysql_fetch_object($res)) !== false) {
  239. $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory);
  240. if( $next->name() == '-total' ) {
  241. $totaltime = $next->time();
  242. $totalcount = $next->count();
  243. $totalmemory = $next->memory();
  244. }
  245. if ($last !== false) {
  246. if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
  247. $last->add_child($next);
  248. continue;
  249. }
  250. }
  251. $last = $next;
  252. if (preg_match("/^query: /", $next->name()) || preg_match("/^query-m: /", $next->name())) {
  253. $sqltotal += $next->time();
  254. $queries[] = $next;
  255. } else {
  256. $points[] = $next;
  257. }
  258. }
  259. $s = new profile_point("SQL Queries", 0, $sqltotal, 0, 0);
  260. foreach ($queries as $q)
  261. $s->add_child($q);
  262. $points[] = $s;
  263. usort($points, "compare_point");
  264. foreach ($points as $point) {
  265. if (strlen($filter) && !strstr($point->name(), $filter))
  266. continue;
  267. $point->display();
  268. }
  269. ?>
  270. </table>
  271. <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></tt></p>
  272. <p>Total memory: <tt><?php printf("%5.02f", $totalmemory / 1024 ) ?></tt></p>
  273. <?php
  274. mysql_free_result($res);
  275. mysql_close($dbh);
  276. ?>
  277. </body>
  278. </html>