fix 3175
[mediawiki.git] / profileinfo.php
blob1c603b6709a58ec934f0d66394a9e8394d3f7302
1 <!--
2 Show profiling data.
4 Copyright 2005 Kate Turner.
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
24 $Id$
25 -->
26 <html>
27 <head>
28 <title>Profiling data</title>
29 <style type="text/css">
30 th {
31 text-align: left;
32 border-bottom: solid 1px black;
35 th, td {
36 padding-left: 0.5em;
37 padding-right: 0.5em;
40 td.time, td.count {
41 text-align: right;
43 </style>
44 </head>
45 <body>
46 <?php
48 $wgDBadminuser = $wgDBadminpassword = $wgDBserver = $wgDBname = $wgEnableProfileInfo = false;
50 define("MEDIAWIKI", 1);
52 require_once("./includes/Defines.php");
53 require_once("./LocalSettings.php");
54 require_once("./AdminSettings.php");
56 if (!$wgEnableProfileInfo)
57 die("disabled");
59 foreach (array("wgDBadminuser", "wgDBadminpassword", "wgDBserver", "wgDBname") as $var)
60 if ($$var === false)
61 die("AdminSettings.php not correct");
63 $expand = array();
64 if (isset($_REQUEST['expand']))
65 foreach(explode(",", $_REQUEST['expand']) as $f)
66 $expand[$f] = true;
68 class profile_point {
69 var $name;
70 var $count;
71 var $time;
72 var $children;
74 function profile_point($name, $count, $time) {
75 $this->name = $name;
76 $this->count = $count;
77 $this->time = $time;
78 $this->children = array();
81 function add_child($child) {
82 $this->children[] = $child;
85 function display($indent = 0.0) {
86 global $expand;
87 usort($this->children, "compare_point");
89 $extet = '';
90 if (isset($expand[$this->name()]))
91 $ex = true;
92 else $ex = false;
93 if (!$ex) {
94 if (count($this->children)) {
95 $url = makeurl(false, false, $expand + array($this->name() => true));
96 $extet = " <a href=\"$url\">[+]</a>";
97 } else $extet = '';
98 } else {
99 $e = array();
100 foreach ($expand as $name => $ep)
101 if ($name != $this->name())
102 $e += array($name => $ep);
104 $extet = " <a href=\"" . makeurl(false, false, $e) . "\">[&ndash;]</a>";
107 <tr>
108 <td class="time"><tt><?php echo $this->fmttime() ?></tt></td>
109 <td class="count"><?php echo $this->count() ?></td>
110 <td class="name" style="padding-left: <?php echo $indent ?>em">
111 <?php echo htmlspecialchars($this->name()) . $extet ?>
112 </td>
113 </tr>
114 <?php
115 if ($ex)
116 foreach ($this->children as $child)
117 $child->display($indent + 2);
120 function name() {
121 return $this->name;
124 function count() {
125 return $this->count;
128 function time() {
129 return $this->time;
132 function fmttime() {
133 return sprintf("%5.02f", $this->time);
137 function compare_point($a, $b) {
138 global $sort;
139 switch ($sort) {
140 case "name":
141 return strcmp($a->name(), $b->name());
142 case "time":
143 return $a->time() > $b->time() ? -1 : 1;
144 case "count":
145 return $a->count() > $b->count() ? -1 : 1;
149 $sorts = array("time", "count", "name");
150 $sort = 'time';
151 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
152 $sort = $_REQUEST['sort'];
154 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
155 or die("mysql server failed: " . mysql_error());
156 mysql_select_db($wgDBname, $dbh) or die(mysql_error($dbh));
157 $res = mysql_query("
158 SELECT pf_count, pf_time, pf_name
159 FROM profiling
160 ORDER BY pf_name ASC
161 ", $dbh) or die("query failed: " . mysql_error());
163 if (isset($_REQUEST['filter']))
164 $filter = $_REQUEST['filter'];
165 else $filter = '';
168 <form method="profiling.php">
170 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
171 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
172 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
173 <input type="submit" value="Filter" />
174 </p>
175 </form>
177 <table cellspacing="0">
178 <tr id="top">
179 <th><a href="<?php echo makeurl(false, "time") ?>">Time</a></th>
180 <th><a href="<?php echo makeurl(false, "count") ?>">Count</a></th>
181 <th><a href="<?php echo makeurl(false, "name") ?>">Name</a></th>
182 </tr>
183 <?php
184 $totaltime = 0.0;
186 function makeurl($_filter = false, $_sort = false, $_expand = false) {
187 global $filter, $sort, $expand;
189 if ($_expand === false)
190 $_expand = $expand;
192 $nfilter = $_filter ? $_filter : $filter;
193 $nsort = $_sort ? $_sort : $sort;
194 $exp = urlencode(implode(',', array_keys($_expand)));
195 return "?filter=$nfilter&amp;sort=$nsort&amp;expand=$exp";
198 $points = array();
199 $queries = array();
200 $sqltotal = 0.0;
202 $last = false;
203 while (($o = mysql_fetch_object($res)) !== false) {
204 $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time);
205 $totaltime += $next->time();
206 if ($last !== false) {
207 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
208 $last->add_child($next);
209 continue;
212 $last = $next;
213 if (preg_match("/^query: /", $next->name())) {
214 $sqltotal += $next->time();
215 $queries[] = $next;
216 } else {
217 $points[] = $next;
221 $s = new profile_point("SQL Queries", 0, $sqltotal);
222 foreach ($queries as $q)
223 $s->add_child($q);
224 $points[] = $s;
226 usort($points, "compare_point");
228 foreach ($points as $point) {
229 if (strlen($filter) && !strstr($point->name(), $filter))
230 continue;
232 $point->display();
235 </table>
237 <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></p>
238 <?php
240 mysql_free_result($res);
241 mysql_close($dbh);
244 </body>
245 </html>