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