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