* Reorganised the includes directory, creating subdirectories db, parser and specials
[lhc/web/wiklou.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.timep, td.memoryp, td.count, td.cpr, td.tpc, td.mpc, td.tpr, td.mpr {
40 text-align: right;
41 }
42 td.timep, td.tpc, td.tpr {
43 background-color: #fffff0;
44 }
45 td.memoryp, td.mpc, td.mpr {
46 background-color: #f0f8ff;
47 }
48 td.count, td,cpr {
49 background-color: #f0fff0;
50 }
51 td.name {
52 background-color: #f9f9f9;
53 }
54 </style>
55 </head>
56 <body>
57 <?php
58
59 $wgDBadminuser = $wgDBadminpassword = $wgDBserver = $wgDBname = $wgEnableProfileInfo = $wgDBprefix = false;
60
61 define( 'MW_NO_SETUP', 1 );
62 require_once( './includes/WebStart.php' );
63 require_once("./AdminSettings.php");
64 require_once( './includes/GlobalFunctions.php' );
65
66 if (!$wgEnableProfileInfo) {
67 echo "disabled\n";
68 exit( 1 );
69 }
70
71 foreach (array("wgDBadminuser", "wgDBadminpassword", "wgDBserver", "wgDBname") as $var)
72 if ($$var === false) {
73 echo "AdminSettings.php not correct\n";
74 exit( 1 );
75 }
76
77
78 $expand = array();
79 if (isset($_REQUEST['expand']))
80 foreach(explode(",", $_REQUEST['expand']) as $f)
81 $expand[$f] = true;
82
83 class profile_point {
84 var $name;
85 var $count;
86 var $time;
87 var $children;
88
89 function profile_point($name, $count, $time, $memory ) {
90 $this->name = $name;
91 $this->count = $count;
92 $this->time = $time;
93 $this->memory = $memory;
94 $this->children = array();
95 }
96
97 function add_child($child) {
98 $this->children[] = $child;
99 }
100
101 function display($indent = 0.0) {
102 global $expand, $totaltime, $totalmemory, $totalcount;
103 usort($this->children, "compare_point");
104
105 $extet = '';
106 if (isset($expand[$this->name()]))
107 $ex = true;
108 else $ex = false;
109 if (!$ex) {
110 if (count($this->children)) {
111 $url = makeurl(false, false, $expand + array($this->name() => true));
112 $extet = " <a href=\"$url\">[+]</a>";
113 } else $extet = '';
114 } else {
115 $e = array();
116 foreach ($expand as $name => $ep)
117 if ($name != $this->name())
118 $e += array($name => $ep);
119
120 $extet = " <a href=\"" . makeurl(false, false, $e) . "\">[&ndash;]</a>";
121 }
122 ?>
123 <tr>
124 <td class="name" style="padding-left: <?php echo $indent ?>em;">
125 <?php echo htmlspecialchars($this->name()) . $extet ?>
126 </td>
127 <td class="timep"><?php echo @wfPercent( $this->time() / $totaltime * 100 ) ?></td>
128 <td class="memoryp"><?php echo @wfPercent( $this->memory() / $totalmemory * 100 ) ?></td>
129 <td class="count"><?php echo $this->count() ?></td>
130 <td class="cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ) ?></td>
131 <td class="tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ) ?></td>
132 <td class="mpc"><?php echo round( sprintf( '%.2f' ,$this->memoryPerCall() / 1024 ), 2 ) ?></td>
133 <td class="tpr"><?php echo @round( sprintf( '%.2f', $this->time() / $totalcount ), 2 ) ?></td>
134 <td class="mpr"><?php echo @round( sprintf( '%.2f' ,$this->memory() / $totalcount / 1024 ), 2 ) ?></td>
135 </tr>
136 <?php
137 if ($ex) {
138 foreach ($this->children as $child) {
139 $child->display($indent + 2);
140 }
141 }
142 }
143
144 function name() {
145 return $this->name;
146 }
147
148 function count() {
149 return $this->count;
150 }
151
152 function time() {
153 return $this->time;
154 }
155
156 function memory() {
157 return $this->memory;
158 }
159
160 function timePerCall() {
161 return @($this->time / $this->count);
162 }
163
164 function memoryPerCall() {
165 return @($this->memory / $this->count);
166 }
167
168 function callsPerRequest() {
169 global $totalcount;
170 return @($this->count / $totalcount);
171 }
172
173 function timePerRequest() {
174 global $totalcount;
175 return @($this->time / $totalcount);
176 }
177
178 function memoryPerRequest() {
179 global $totalcount;
180 return @($this->memory / $totalcount);
181 }
182
183 function fmttime() {
184 return sprintf("%5.02f", $this->time);
185 }
186 };
187
188 function compare_point($a, $b) {
189 global $sort;
190 switch ($sort) {
191 case "name":
192 return strcmp($a->name(), $b->name());
193 case "time":
194 return $a->time() > $b->time() ? -1 : 1;
195 case "memory":
196 return $a->memory() > $b->memory() ? -1 : 1;
197 case "count":
198 return $a->count() > $b->count() ? -1 : 1;
199 case "time_per_call":
200 return $a->timePerCall() > $b->timePerCall() ? -1 : 1;
201 case "memory_per_call":
202 return $a->memoryPerCall() > $b->memoryPerCall() ? -1 : 1;
203 case "calls_per_req":
204 return $a->callsPerRequest() > $b->callsPerRequest() ? -1 : 1;
205 case "time_per_req":
206 return $a->timePerRequest() > $b->timePerRequest() ? -1 : 1;
207 case "memory_per_req":
208 return $a->memoryPerRequest() > $b->memoryPerRequest() ? -1 : 1;
209 }
210 }
211
212 $sorts = array("time","memory","count","calls_per_req","name","time_per_call","memory_per_call","time_per_req","memory_per_req");
213 $sort = 'time';
214 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
215 $sort = $_REQUEST['sort'];
216
217 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
218 or die("mysql server failed: " . mysql_error());
219 mysql_select_db($wgDBname, $dbh) or die(mysql_error($dbh));
220 $res = mysql_query("
221 SELECT pf_count, pf_time, pf_memory, pf_name
222 FROM {$wgDBprefix}profiling
223 ORDER BY pf_name ASC
224 ", $dbh) or die("query failed: " . mysql_error());
225
226 if (isset($_REQUEST['filter']))
227 $filter = $_REQUEST['filter'];
228 else $filter = '';
229
230 ?>
231 <form method="profiling.php">
232 <p>
233 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
234 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
235 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
236 <input type="submit" value="Filter" />
237 </p>
238 </form>
239
240 <table cellspacing="0" border="1">
241 <tr id="top">
242 <th><a href="<?php echo makeurl(false, "name") ?>">Name</a></th>
243 <th><a href="<?php echo makeurl(false, "time") ?>">Time (%)</a></th>
244 <th><a href="<?php echo makeurl(false, "memory") ?>">Memory (%)</a></th>
245 <th><a href="<?php echo makeurl(false, "count") ?>">Count</a></th>
246 <th><a href="<?php echo makeurl(false, "calls_per_req") ?>">Calls/req</a></th>
247 <th><a href="<?php echo makeurl(false, "time_per_call") ?>">ms/call</a></th>
248 <th><a href="<?php echo makeurl(false, "memory_per_call") ?>">kb/call</a></th>
249 <th><a href="<?php echo makeurl(false, "time_per_req") ?>">ms/req</a></th>
250 <th><a href="<?php echo makeurl(false, "memory_per_req") ?>">kb/req</a></th>
251 </tr>
252 <?php
253 $totaltime = 0.0;
254 $totalcount = 0;
255 $totalmemory = 0.0;
256
257 function makeurl($_filter = false, $_sort = false, $_expand = false) {
258 global $filter, $sort, $expand;
259
260 if ($_expand === false)
261 $_expand = $expand;
262
263 $nfilter = $_filter ? $_filter : $filter;
264 $nsort = $_sort ? $_sort : $sort;
265 $exp = urlencode(implode(',', array_keys($_expand)));
266 return "?filter=$nfilter&amp;sort=$nsort&amp;expand=$exp";
267 }
268
269 $points = array();
270 $queries = array();
271 $sqltotal = 0.0;
272
273 $last = false;
274 while (($o = mysql_fetch_object($res)) !== false) {
275 $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory);
276 if( $next->name() == '-total' ) {
277 $totaltime = $next->time();
278 $totalcount = $next->count();
279 $totalmemory = $next->memory();
280 }
281 if ($last !== false) {
282 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
283 $last->add_child($next);
284 continue;
285 }
286 }
287 $last = $next;
288 if (preg_match("/^query: /", $next->name()) || preg_match("/^query-m: /", $next->name())) {
289 $sqltotal += $next->time();
290 $queries[] = $next;
291 } else {
292 $points[] = $next;
293 }
294 }
295
296 $s = new profile_point("SQL Queries", 0, $sqltotal, 0, 0);
297 foreach ($queries as $q)
298 $s->add_child($q);
299 $points[] = $s;
300
301 usort($points, "compare_point");
302
303 foreach ($points as $point) {
304 if (strlen($filter) && !strstr($point->name(), $filter))
305 continue;
306
307 $point->display();
308 }
309 ?>
310 </table>
311
312 <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></tt></p>
313 <p>Total memory: <tt><?php printf("%5.02f", $totalmemory / 1024 ) ?></tt></p>
314 <?php
315
316 mysql_free_result($res);
317 mysql_close($dbh);
318
319 ?>
320 </body>
321 </html>