Tweak profileinfo.php:
[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.count, td.cpr {
40 text-align: right;
41 }
42 </style>
43 </head>
44 <body>
45 <?php
46
47 $wgDBadminuser = $wgDBadminpassword = $wgDBserver = $wgDBname = $wgEnableProfileInfo = 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) {
78 $this->name = $name;
79 $this->count = $count;
80 $this->time = $time;
81 $this->children = array();
82 }
83
84 function add_child($child) {
85 $this->children[] = $child;
86 }
87
88 function display($indent = 0.0) {
89 global $expand, $totaltime, $totalcount;
90 usort($this->children, "compare_point");
91
92 $extet = '';
93 if (isset($expand[$this->name()]))
94 $ex = true;
95 else $ex = false;
96 if (!$ex) {
97 if (count($this->children)) {
98 $url = makeurl(false, false, $expand + array($this->name() => true));
99 $extet = " <a href=\"$url\">[+]</a>";
100 } else $extet = '';
101 } else {
102 $e = array();
103 foreach ($expand as $name => $ep)
104 if ($name != $this->name())
105 $e += array($name => $ep);
106
107 $extet = " <a href=\"" . makeurl(false, false, $e) . "\">[&ndash;]</a>";
108 }
109 ?>
110 <tr>
111 <td class="time"><tt><?php echo $this->fmttime() ?></tt></td>
112 <td class="timep"><?php echo wfPercent( $this->time() / $totaltime * 100 ) ?></td>
113 <td class="count"><?php echo $this->count() ?></td>
114 <td class="cpr"><?php echo round( sprintf( '%.2f',$this->count() / $totalcount ), 2 ) ?>
115 <td class="name" style="padding-left: <?php echo $indent ?>em">
116 <?php echo htmlspecialchars($this->name()) . $extet ?>
117 </td>
118 </tr>
119 <?php
120 if ($ex)
121 foreach ($this->children as $child)
122 $child->display($indent + 2);
123 }
124
125 function name() {
126 return $this->name;
127 }
128
129 function count() {
130 return $this->count;
131 }
132
133 function time() {
134 return $this->time;
135 }
136
137 function fmttime() {
138 return sprintf("%5.02f", $this->time);
139 }
140 };
141
142 function compare_point($a, $b) {
143 global $sort;
144 switch ($sort) {
145 case "name":
146 return strcmp($a->name(), $b->name());
147 case "time":
148 return $a->time() > $b->time() ? -1 : 1;
149 case "count":
150 return $a->count() > $b->count() ? -1 : 1;
151 }
152 }
153
154 $sorts = array("time", "count", "name");
155 $sort = 'time';
156 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
157 $sort = $_REQUEST['sort'];
158
159 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
160 or die("mysql server failed: " . mysql_error());
161 mysql_select_db($wgDBname, $dbh) or die(mysql_error($dbh));
162 $res = mysql_query("
163 SELECT pf_count, pf_time, pf_name
164 FROM profiling
165 ORDER BY pf_name ASC
166 ", $dbh) or die("query failed: " . mysql_error());
167
168 if (isset($_REQUEST['filter']))
169 $filter = $_REQUEST['filter'];
170 else $filter = '';
171
172 ?>
173 <form method="profiling.php">
174 <p>
175 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
176 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
177 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
178 <input type="submit" value="Filter" />
179 </p>
180 </form>
181
182 <table cellspacing="0">
183 <tr id="top">
184 <th><a href="<?php echo makeurl(false, "time") ?>">Time</a></th>
185 <th>Time (%)</th>
186 <th><a href="<?php echo makeurl(false, "count") ?>">Count</a></th>
187 <th>Avg calls per request</th>
188 <th><a href="<?php echo makeurl(false, "name") ?>">Name</a></th>
189 </tr>
190 <?php
191 $totaltime = 0.0;
192 $totalcount = 0;
193
194 function makeurl($_filter = false, $_sort = false, $_expand = false) {
195 global $filter, $sort, $expand;
196
197 if ($_expand === false)
198 $_expand = $expand;
199
200 $nfilter = $_filter ? $_filter : $filter;
201 $nsort = $_sort ? $_sort : $sort;
202 $exp = urlencode(implode(',', array_keys($_expand)));
203 return "?filter=$nfilter&amp;sort=$nsort&amp;expand=$exp";
204 }
205
206 $points = array();
207 $queries = array();
208 $sqltotal = 0.0;
209
210 $last = false;
211 while (($o = mysql_fetch_object($res)) !== false) {
212 $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time);
213 if( $next->name() == '-total' ) {
214 $totaltime = $next->time();
215 $totalcount = $next->count();
216 }
217 if ($last !== false) {
218 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
219 $last->add_child($next);
220 continue;
221 }
222 }
223 $last = $next;
224 if (preg_match("/^query: /", $next->name())) {
225 $sqltotal += $next->time();
226 $queries[] = $next;
227 } else {
228 $points[] = $next;
229 }
230 }
231
232 $s = new profile_point("SQL Queries", 0, $sqltotal);
233 foreach ($queries as $q)
234 $s->add_child($q);
235 $points[] = $s;
236
237 usort($points, "compare_point");
238
239 foreach ($points as $point) {
240 if (strlen($filter) && !strstr($point->name(), $filter))
241 continue;
242
243 $point->display();
244 }
245 ?>
246 </table>
247
248 <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></p>
249 <?php
250
251 mysql_free_result($res);
252 mysql_close($dbh);
253
254 ?>
255 </body>
256 </html>