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