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