]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - profileinfo.php
MediaWiki 1.11.0
[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( 'MW_NO_SETUP', 1 );
50 require_once( './includes/WebStart.php' );
51 require_once("./AdminSettings.php");
52
53 if (!$wgEnableProfileInfo) {
54         echo "disabled\n";
55         exit( 1 );
56 }
57
58 foreach (array("wgDBadminuser", "wgDBadminpassword", "wgDBserver", "wgDBname") as $var)
59         if ($$var === false) {
60                 echo "AdminSettings.php not correct\n";
61                 exit( 1 );
62         }
63
64
65 $expand = array();
66 if (isset($_REQUEST['expand']))
67         foreach(explode(",", $_REQUEST['expand']) as $f)
68                 $expand[$f] = true;
69
70 class profile_point {
71         var $name;
72         var $count;
73         var $time;
74         var $children;
75
76         function profile_point($name, $count, $time) {
77                 $this->name = $name;
78                 $this->count = $count;
79                 $this->time = $time;
80                 $this->children = array();
81         }
82
83         function add_child($child) {
84                 $this->children[] = $child;
85         }
86
87         function display($indent = 0.0) {
88                 global $expand;
89                 usort($this->children, "compare_point");
90
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
106                         $extet = " <a href=\"" . makeurl(false, false, $e) . "\">[&ndash;]</a>";
107                 }
108                 ?>
109                 <tr>
110                 <td class="time"><tt><?php echo $this->fmttime() ?></tt></td>
111                 <td class="count"><?php echo $this->count() ?></td>
112                 <td class="name" style="padding-left: <?php echo $indent ?>em">
113                         <?php echo htmlspecialchars($this->name()) . $extet ?>
114                 </td>
115                 </tr>
116                 <?php
117                 if ($ex)
118                         foreach ($this->children as $child)
119                                 $child->display($indent + 2);
120         }
121
122         function name() {
123                 return $this->name;
124         }
125
126         function count() {
127                 return $this->count;
128         }
129
130         function time() {
131                 return $this->time;
132         }
133
134         function fmttime() {
135                 return sprintf("%5.02f", $this->time);
136         }
137 };
138
139 function compare_point($a, $b) {
140         global $sort;
141         switch ($sort) {
142         case "name":
143                 return strcmp($a->name(), $b->name());
144         case "time":
145                 return $a->time() > $b->time() ? -1 : 1;
146         case "count":
147                 return $a->count() > $b->count() ? -1 : 1;
148         }
149 }
150
151 $sorts = array("time", "count", "name");
152 $sort = 'time';
153 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
154         $sort = $_REQUEST['sort'];
155
156 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
157         or die("mysql server failed: " . mysql_error());
158 mysql_select_db($wgDBname, $dbh) or die(mysql_error($dbh));
159 $res = mysql_query("
160         SELECT pf_count, pf_time, pf_name
161         FROM profiling
162         ORDER BY pf_name ASC
163 ", $dbh) or die("query failed: " . mysql_error());
164
165 if (isset($_REQUEST['filter']))
166         $filter = $_REQUEST['filter'];
167 else    $filter = '';
168
169 ?>
170 <form method="profiling.php">
171 <p>
172 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
173 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
174 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
175 <input type="submit" value="Filter" />
176 </p>
177 </form>
178
179 <table cellspacing="0">
180 <tr id="top">
181 <th><a href="<?php echo makeurl(false, "time") ?>">Time</a></th>
182 <th><a href="<?php echo makeurl(false, "count") ?>">Count</a></th>
183 <th><a href="<?php echo makeurl(false, "name") ?>">Name</a></th>
184 </tr>
185 <?php
186 $totaltime = 0.0;
187
188 function makeurl($_filter = false, $_sort = false, $_expand = false) {
189         global $filter, $sort, $expand;
190
191         if ($_expand === false)
192                 $_expand = $expand;
193
194         $nfilter = $_filter ? $_filter : $filter;
195         $nsort = $_sort ? $_sort : $sort;
196         $exp = urlencode(implode(',', array_keys($_expand)));
197         return "?filter=$nfilter&amp;sort=$nsort&amp;expand=$exp";
198 }
199
200 $points = array();
201 $queries = array();
202 $sqltotal = 0.0;
203
204 $last = false;
205 while (($o = mysql_fetch_object($res)) !== false) {
206         $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time);
207         $totaltime += $next->time();
208         if ($last !== false) {
209                 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
210                         $last->add_child($next);
211                         continue;
212                 }
213         }
214         $last = $next;
215         if (preg_match("/^query: /", $next->name())) {
216                 $sqltotal += $next->time();
217                 $queries[] = $next;
218         } else {
219                 $points[] = $next;
220         }
221 }
222
223 $s = new profile_point("SQL Queries", 0, $sqltotal);
224 foreach ($queries as $q)
225         $s->add_child($q);
226 $points[] = $s;
227
228 usort($points, "compare_point");
229
230 foreach ($points as $point) {
231         if (strlen($filter) && !strstr($point->name(), $filter))
232                 continue;
233
234         $point->display();
235 }
236 ?>
237 </table>
238
239 <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></p>
240 <?php
241
242 mysql_free_result($res);
243 mysql_close($dbh);
244
245 ?>
246 </body>
247 </html>