* suggestion for using tables.sql parsing instead of database query for tables list...
[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 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
32 require ( 'phase3/includes/WebStart.php' );
33 } else {
34 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
35 }
36
37
38 header( 'Content-Type: text/html; charset=utf-8' );
39
40 ?>
41 <html>
42 <head>
43 <title>Profiling data</title>
44 <style type="text/css">
45 th {
46 text-align: left;
47 border-bottom: solid 1px black;
48 }
49
50 th, td {
51 padding-left: 0.5em;
52 padding-right: 0.5em;
53 }
54
55 td.timep, td.memoryp, td.count, td.cpr, td.tpc, td.mpc, td.tpr, td.mpr {
56 text-align: right;
57 }
58 td.timep, td.tpc, td.tpr {
59 background-color: #fffff0;
60 }
61 td.memoryp, td.mpc, td.mpr {
62 background-color: #f0f8ff;
63 }
64 td.count, td,cpr {
65 background-color: #f0fff0;
66 }
67 td.name {
68 background-color: #f9f9f9;
69 }
70 </style>
71 </head>
72 <body>
73 <?php
74
75 if ( !$wgEnableProfileInfo ) {
76 echo "<p>Disabled</p>\n";
77 echo "</body></html>";
78 exit( 1 );
79 }
80
81 $expand = array();
82 if ( isset( $_REQUEST['expand'] ) )
83 foreach( explode( ',', $_REQUEST['expand'] ) as $f )
84 $expand[$f] = true;
85
86 class profile_point {
87 var $name;
88 var $count;
89 var $time;
90 var $children;
91
92 static $totaltime, $totalmemory, $totalcount;
93
94 function __construct( $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( $expand, $indent = 0.0 ) {
107 usort( $this->children, 'compare_point' );
108
109 $ex = isset( $expand[$this->name()] );
110
111 if ( !$ex ) {
112 if ( count( $this->children ) ) {
113 $url = getEscapedProfileUrl( false, false, $expand + array( $this->name() => true ) );
114 $extet = " <a href=\"$url\">[+]</a>";
115 } else {
116 $extet = '';
117 }
118 } else {
119 $e = array();
120 foreach ( $expand as $name => $ep ) {
121 if ( $name != $this->name() ) {
122 $e += array( $name => $ep );
123 }
124 }
125
126 $extet = " <a href=\"" . getEscapedProfileUrl( false, false, $e ) . "\">[–]</a>";
127 }
128 ?>
129 <tr>
130 <td class="name" style="padding-left: <?php echo $indent ?>em;">
131 <?php echo htmlspecialchars( $this->name() ) . $extet ?>
132 </td>
133 <td class="timep"><?php echo @wfPercent( $this->time() / self::$totaltime * 100 ) ?></td>
134 <td class="memoryp"><?php echo @wfPercent( $this->memory() / self::$totalmemory * 100 ) ?></td>
135 <td class="count"><?php echo $this->count() ?></td>
136 <td class="cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ) ?></td>
137 <td class="tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ) ?></td>
138 <td class="mpc"><?php echo round( sprintf( '%.2f' ,$this->memoryPerCall() / 1024 ), 2 ) ?></td>
139 <td class="tpr"><?php echo @round( sprintf( '%.2f', $this->time() / self::$totalcount ), 2 ) ?></td>
140 <td class="mpr"><?php echo @round( sprintf( '%.2f' ,$this->memory() / self::$totalcount / 1024 ), 2 ) ?></td>
141 </tr>
142 <?php
143 if ( $ex ) {
144 foreach ( $this->children as $child ) {
145 $child->display( $expand, $indent + 2 );
146 }
147 }
148 }
149
150 function name() {
151 return $this->name;
152 }
153
154 function count() {
155 return $this->count;
156 }
157
158 function time() {
159 return $this->time;
160 }
161
162 function memory() {
163 return $this->memory;
164 }
165
166 function timePerCall() {
167 return @( $this->time / $this->count );
168 }
169
170 function memoryPerCall() {
171 return @( $this->memory / $this->count );
172 }
173
174 function callsPerRequest() {
175 return @( $this->count / self::$totalcount );
176 }
177
178 function timePerRequest() {
179 return @( $this->time / self::$totalcount );
180 }
181
182 function memoryPerRequest() {
183 return @( $this->memory / self::$totalcount );
184 }
185
186 function fmttime() {
187 return sprintf( "%5.02f", $this->time );
188 }
189 };
190
191 function compare_point( $a, $b ) {
192 global $sort;
193 switch ( $sort ) {
194 case "name":
195 return strcmp( $a->name(), $b->name() );
196 case "time":
197 return $a->time() > $b->time() ? -1 : 1;
198 case "memory":
199 return $a->memory() > $b->memory() ? -1 : 1;
200 case "count":
201 return $a->count() > $b->count() ? -1 : 1;
202 case "time_per_call":
203 return $a->timePerCall() > $b->timePerCall() ? -1 : 1;
204 case "memory_per_call":
205 return $a->memoryPerCall() > $b->memoryPerCall() ? -1 : 1;
206 case "calls_per_req":
207 return $a->callsPerRequest() > $b->callsPerRequest() ? -1 : 1;
208 case "time_per_req":
209 return $a->timePerRequest() > $b->timePerRequest() ? -1 : 1;
210 case "memory_per_req":
211 return $a->memoryPerRequest() > $b->memoryPerRequest() ? -1 : 1;
212 }
213 }
214
215 $sorts = array( 'time', 'memory', 'count', 'calls_per_req', 'name',
216 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' );
217 $sort = 'time';
218 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) )
219 $sort = $_REQUEST['sort'];
220
221
222 $dbr = wfGetDB( DB_SLAVE );
223 $res = $dbr->select( 'profiling', '*', array(), 'profileinfo.php', array( 'ORDER BY' => 'pf_name ASC' ) );
224
225 if (isset( $_REQUEST['filter'] ) )
226 $filter = $_REQUEST['filter'];
227 else
228 $filter = '';
229
230 ?>
231 <form method="get" action="profileinfo.php">
232 <p>
233 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
234 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
235 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
236 <input type="submit" value="Filter" />
237 </p>
238 </form>
239
240 <table cellspacing="0" border="1">
241 <tr id="top">
242 <th><a href="<?php echo getEscapedProfileUrl( false, 'name' ) ?>">Name</a></th>
243 <th><a href="<?php echo getEscapedProfileUrl( false, 'time' ) ?>">Time (%)</a></th>
244 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory' ) ?>">Memory (%)</a></th>
245 <th><a href="<?php echo getEscapedProfileUrl( false, 'count' ) ?>">Count</a></th>
246 <th><a href="<?php echo getEscapedProfileUrl( false, 'calls_per_req' ) ?>">Calls/req</a></th>
247 <th><a href="<?php echo getEscapedProfileUrl( false, 'time_per_call' ) ?>">ms/call</a></th>
248 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory_per_call' ) ?>">kb/call</a></th>
249 <th><a href="<?php echo getEscapedProfileUrl( false, 'time_per_req' ) ?>">ms/req</a></th>
250 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory_per_req' ) ?>">kb/req</a></th>
251 </tr>
252 <?php
253 profile_point::$totaltime = 0.0;
254 profile_point::$totalcount = 0;
255 profile_point::$totalmemory = 0.0;
256
257 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
258 global $filter, $sort, $expand;
259
260 if ( $_expand === false )
261 $_expand = $expand;
262
263 return htmlspecialchars(
264 '?' .
265 wfArrayToCGI( array(
266 'filter' => $_filter ? $_filter : $filter,
267 'sort' => $_sort ? $_sort : $sort,
268 'expand' => implode( ',', array_keys( $_expand ) )
269 ) )
270 );
271 }
272
273 $points = array();
274 $queries = array();
275 $sqltotal = 0.0;
276
277 $last = false;
278 foreach( $res as $o ) {
279 $next = new profile_point( $o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory );
280 if( $next->name() == '-total' ) {
281 profile_point::$totaltime = $next->time();
282 profile_point::$totalcount = $next->count();
283 profile_point::$totalmemory = $next->memory();
284 }
285 if ( $last !== false ) {
286 if ( preg_match( "/^".preg_quote( $last->name(), "/" )."/", $next->name() ) ) {
287 $last->add_child($next);
288 continue;
289 }
290 }
291 $last = $next;
292 if ( preg_match( "/^query: /", $next->name() ) || preg_match( "/^query-m: /", $next->name() ) ) {
293 $sqltotal += $next->time();
294 $queries[] = $next;
295 } else {
296 $points[] = $next;
297 }
298 }
299
300 $s = new profile_point( "SQL Queries", 0, $sqltotal, 0, 0 );
301 foreach ( $queries as $q )
302 $s->add_child($q);
303 $points[] = $s;
304
305 usort( $points, "compare_point" );
306
307 foreach ( $points as $point ) {
308 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) )
309 continue;
310
311 $point->display( $expand );
312 }
313 ?>
314 </table>
315
316 <p>Total time: <tt><?php printf("%5.02f", profile_point::$totaltime) ?></tt></p>
317 <p>Total memory: <tt><?php printf("%5.02f", profile_point::$totalmemory / 1024 ) ?></tt></p>
318 </body>
319 </html>