Re-arrange a little. Show memory.
[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.memory, td.memoryp, td.count, td.cpr {
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="time"><tt><?php echo $this->fmttime() ?></tt></td>
116 <td class="timep"><?php echo @wfPercent( $this->time() / $totaltime * 100 ) ?></td>
117 <td class="memoryp"><?php echo @wfPercent( $this->memory() / $totalmemory * 100 ) ?></td>
118 <td class="count"><?php echo $this->count() ?></td>
119 <td class="cpr"><?php echo @round( sprintf( '%.2f', $this->count() / $totalcount ), 2 ) ?>
120 <td class="tpc"><?php echo @round( sprintf( '%.2f', $this->time() / $this->count() ), 2 ) ?>
121 <td class="mpr"><?php echo @round( sprintf( '%.2f' ,$this->memory() / $this->count() / 1048576 ), 2 ) ?>
122 </tr>
123 <?php
124 if ($ex)
125 foreach ($this->children as $child)
126 $child->display($indent + 2);
127 }
128
129 function name() {
130 return $this->name;
131 }
132
133 function count() {
134 return $this->count;
135 }
136
137 function time() {
138 return $this->time;
139 }
140
141 function memory() {
142 return $this->memory;
143 }
144
145 function fmttime() {
146 return sprintf("%5.02f", $this->time);
147 }
148 };
149
150 function compare_point($a, $b) {
151 global $sort;
152 switch ($sort) {
153 case "name":
154 return strcmp($a->name(), $b->name());
155 case "time":
156 return $a->time() > $b->time() ? -1 : 1;
157 case "memory":
158 return $a->memory() > $b->memory() ? -1 : 1;
159 case "count":
160 return $a->count() > $b->count() ? -1 : 1;
161 }
162 }
163
164 $sorts = array("time", "memory", "count", "name");
165 $sort = 'time';
166 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
167 $sort = $_REQUEST['sort'];
168
169 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
170 or die("mysql server failed: " . mysql_error());
171 mysql_select_db($wgDBname, $dbh) or die(mysql_error($dbh));
172 $res = mysql_query("
173 SELECT pf_count, pf_time, pf_memory, pf_name
174 FROM {$wgDBprefix}profiling
175 ORDER BY pf_name ASC
176 ", $dbh) or die("query failed: " . mysql_error());
177
178 if (isset($_REQUEST['filter']))
179 $filter = $_REQUEST['filter'];
180 else $filter = '';
181
182 ?>
183 <form method="profiling.php">
184 <p>
185 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
186 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
187 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
188 <input type="submit" value="Filter" />
189 </p>
190 </form>
191
192 <table cellspacing="0">
193 <tr id="top">
194 <th><a href="<?php echo makeurl(false, "name") ?>">Name</a></th>
195 <th><a href="<?php echo makeurl(false, "time") ?>">Time (ms)</a></th>
196 <th>Time (%)</th>
197 <th><a href="<?php echo makeurl(false, "memory") ?>">Memory (%)</a></th>
198 <th><a href="<?php echo makeurl(false, "count") ?>">Count</a></th>
199 <th>Calls/request</th>
200 <th>ms/call</th>
201 <th>kb/call</th>
202 </tr>
203 <?php
204 $totaltime = 0.0;
205 $totalcount = 0;
206 $totalmemory = 0.0;
207
208 function makeurl($_filter = false, $_sort = false, $_expand = false) {
209 global $filter, $sort, $expand;
210
211 if ($_expand === false)
212 $_expand = $expand;
213
214 $nfilter = $_filter ? $_filter : $filter;
215 $nsort = $_sort ? $_sort : $sort;
216 $exp = urlencode(implode(',', array_keys($_expand)));
217 return "?filter=$nfilter&amp;sort=$nsort&amp;expand=$exp";
218 }
219
220 $points = array();
221 $queries = array();
222 $sqltotal = 0.0;
223
224 $last = false;
225 while (($o = mysql_fetch_object($res)) !== false) {
226 $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory);
227 if( $next->name() == '-total' ) {
228 $totaltime = $next->time();
229 $totalcount = $next->count();
230 $totalmemory = $next->memory();
231 }
232 if ($last !== false) {
233 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
234 $last->add_child($next);
235 continue;
236 }
237 }
238 $last = $next;
239 if (preg_match("/^query: /", $next->name())) {
240 $sqltotal += $next->time();
241 $queries[] = $next;
242 } else {
243 $points[] = $next;
244 }
245 }
246
247 $s = new profile_point("SQL Queries", 0, $sqltotal, 0, 0);
248 foreach ($queries as $q)
249 $s->add_child($q);
250 $points[] = $s;
251
252 usort($points, "compare_point");
253
254 foreach ($points as $point) {
255 if (strlen($filter) && !strstr($point->name(), $filter))
256 continue;
257
258 $point->display();
259 }
260 ?>
261 </table>
262
263 <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></p>
264 <?php
265
266 mysql_free_result($res);
267 mysql_close($dbh);
268
269 ?>
270 </body>
271 </html>