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