]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - profileinfo.php
MediaWiki 1.5.8 (initial commit)
[autoinstalls/mediawiki.git] / profileinfo.php
1 <!--
2      Show profiling data.
3
4      Copyright 2005 Kate Turner.
5
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:
12
13      The above copyright notice and this permission notice shall be included in
14      all copies or substantial portions of the Software.
15
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.
23
24 -->
25 <html>
26 <head>
27 <title>Profiling data</title>
28 <style type="text/css">
29         th {
30                 text-align: left;
31                 border-bottom: solid 1px black;
32         }
33
34         th, td {
35                 padding-left: 0.5em;
36                 padding-right: 0.5em;
37         }
38
39         td.time, td.count {
40                 text-align: right;
41         }
42 </style>
43 </head>
44 <body>
45 <?php
46
47 $wgDBadminuser = $wgDBadminpassword = $wgDBserver = $wgDBname = $wgEnableProfileInfo = false;
48
49 define("MEDIAWIKI", 1);
50
51 require_once("./includes/Defines.php");
52 require_once("./LocalSettings.php");
53 require_once("./AdminSettings.php");
54
55 if (!$wgEnableProfileInfo)
56         die("disabled");
57
58 foreach (array("wgDBadminuser", "wgDBadminpassword", "wgDBserver", "wgDBname") as $var)
59         if ($$var === false)
60                 die("AdminSettings.php not correct");
61
62 $expand = array();
63 if (isset($_REQUEST['expand']))
64         foreach(explode(",", $_REQUEST['expand']) as $f)
65                 $expand[$f] = true;
66
67 class profile_point {
68         var $name;
69         var $count;
70         var $time;
71         var $children;
72
73         function profile_point($name, $count, $time) {
74                 $this->name = $name;
75                 $this->count = $count;
76                 $this->time = $time;
77                 $this->children = array();
78         }
79
80         function add_child($child) {
81                 $this->children[] = $child;
82         }
83
84         function display($indent = 0.0) {
85                 global $expand;
86                 usort($this->children, "compare_point");
87
88                 $extet = '';
89                 if (isset($expand[$this->name()]))
90                         $ex = true;
91                 else    $ex = false;
92                 if (!$ex) {
93                         if (count($this->children)) {
94                                 $url = makeurl(false, false, $expand + array($this->name() => true));
95                                 $extet = " <a href=\"$url\">[+]</a>";
96                         } else $extet = '';
97                 } else {
98                         $e = array();
99                         foreach ($expand as $name => $ep)
100                                 if ($name != $this->name())
101                                         $e += array($name => $ep);
102
103                         $extet = " <a href=\"" . makeurl(false, false, $e) . "\">[&ndash;]</a>";
104                 }
105                 ?>
106                 <tr>
107                 <td class="time"><tt><?php echo $this->fmttime() ?></tt></td>
108                 <td class="count"><?php echo $this->count() ?></td>
109                 <td class="name" style="padding-left: <?php echo $indent ?>em">
110                         <?php echo htmlspecialchars($this->name()) . $extet ?>
111                 </td>
112                 </tr>
113                 <?php
114                 if ($ex)
115                         foreach ($this->children as $child)
116                                 $child->display($indent + 2);
117         }
118
119         function name() {
120                 return $this->name;
121         }
122
123         function count() {
124                 return $this->count;
125         }
126
127         function time() {
128                 return $this->time;
129         }
130
131         function fmttime() {
132                 return sprintf("%5.02f", $this->time);
133         }
134 };
135
136 function compare_point($a, $b) {
137         global $sort;
138         switch ($sort) {
139         case "name":
140                 return strcmp($a->name(), $b->name());
141         case "time":
142                 return $a->time() > $b->time() ? -1 : 1;
143         case "count":
144                 return $a->count() > $b->count() ? -1 : 1;
145         }
146 }
147
148 $sorts = array("time", "count", "name");
149 $sort = 'time';
150 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
151         $sort = $_REQUEST['sort'];
152
153 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
154         or die("mysql server failed: " . mysql_error());
155 mysql_select_db($wgDBname, $dbh) or die(mysql_error($dbh));
156 $res = mysql_query("
157         SELECT pf_count, pf_time, pf_name
158         FROM profiling
159         ORDER BY pf_name ASC
160 ", $dbh) or die("query failed: " . mysql_error());
161
162 if (isset($_REQUEST['filter']))
163         $filter = $_REQUEST['filter'];
164 else    $filter = '';
165
166 ?>
167 <form method="profiling.php">
168 <p>
169 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
170 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
171 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
172 <input type="submit" value="Filter" />
173 </p>
174 </form>
175
176 <table cellspacing="0">
177 <tr id="top">
178 <th><a href="<?php echo makeurl(false, "time") ?>">Time</a></th>
179 <th><a href="<?php echo makeurl(false, "count") ?>">Count</a></th>
180 <th><a href="<?php echo makeurl(false, "name") ?>">Name</a></th>
181 </tr>
182 <?php
183 $totaltime = 0.0;
184
185 function makeurl($_filter = false, $_sort = false, $_expand = false) {
186         global $filter, $sort, $expand;
187
188         if ($_expand === false)
189                 $_expand = $expand;
190
191         $nfilter = $_filter ? $_filter : $filter;
192         $nsort = $_sort ? $_sort : $sort;
193         $exp = urlencode(implode(',', array_keys($_expand)));
194         return "?filter=$nfilter&amp;sort=$nsort&amp;expand=$exp";
195 }
196
197 $points = array();
198 $queries = array();
199 $sqltotal = 0.0;
200
201 $last = false;
202 while (($o = mysql_fetch_object($res)) !== false) {
203         $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time);
204         $totaltime += $next->time();
205         if ($last !== false) {
206                 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
207                         $last->add_child($next);
208                         continue;
209                 }
210         }
211         $last = $next;
212         if (preg_match("/^query: /", $next->name())) {
213                 $sqltotal += $next->time();
214                 $queries[] = $next;
215         } else {
216                 $points[] = $next;
217         }
218 }
219
220 $s = new profile_point("SQL Queries", 0, $sqltotal);
221 foreach ($queries as $q)
222         $s->add_child($q);
223 $points[] = $s;
224
225 usort($points, "compare_point");
226
227 foreach ($points as $point) {
228         if (strlen($filter) && !strstr($point->name(), $filter))
229                 continue;
230
231         $point->display();
232 }
233 ?>
234 </table>
235
236 <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></p>
237 <?php
238
239 mysql_free_result($res);
240 mysql_close($dbh);
241
242 ?>
243 </body>
244 </html>