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