eol w/s
[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 $dbr = wfGetDB( DB_SLAVE );
82
83 if( !$dbr->tableExists( 'profileinfo' ) ) {
84 echo "<p>No 'profileinfo' table exists, so we can't show you anything.</p>\n";
85 echo "<p>If you want to log profiling data, create the table using "
86 . "<tt>maintenance/archives/patch-profiling.sql</tt> and enable "
87 . "<tt>\$wgProfileToDatabase</tt>.</p>\n";
88 echo "</body></html>";
89 exit( 1 );
90 }
91
92 $expand = array();
93 if ( isset( $_REQUEST['expand'] ) )
94 foreach( explode( ',', $_REQUEST['expand'] ) as $f )
95 $expand[$f] = true;
96
97 class profile_point {
98 var $name;
99 var $count;
100 var $time;
101 var $children;
102
103 static $totaltime, $totalmemory, $totalcount;
104
105 function __construct( $name, $count, $time, $memory ) {
106 $this->name = $name;
107 $this->count = $count;
108 $this->time = $time;
109 $this->memory = $memory;
110 $this->children = array();
111 }
112
113 function add_child( $child ) {
114 $this->children[] = $child;
115 }
116
117 function display( $expand, $indent = 0.0 ) {
118 usort( $this->children, 'compare_point' );
119
120 $ex = isset( $expand[$this->name()] );
121
122 if ( !$ex ) {
123 if ( count( $this->children ) ) {
124 $url = getEscapedProfileUrl( false, false, $expand + array( $this->name() => true ) );
125 $extet = " <a href=\"$url\">[+]</a>";
126 } else {
127 $extet = '';
128 }
129 } else {
130 $e = array();
131 foreach ( $expand as $name => $ep ) {
132 if ( $name != $this->name() ) {
133 $e += array( $name => $ep );
134 }
135 }
136
137 $extet = " <a href=\"" . getEscapedProfileUrl( false, false, $e ) . "\">[–]</a>";
138 }
139 ?>
140 <tr>
141 <td class="name" style="padding-left: <?php echo $indent ?>em;">
142 <?php echo htmlspecialchars( $this->name() ) . $extet ?>
143 </td>
144 <td class="timep"><?php echo @wfPercent( $this->time() / self::$totaltime * 100 ) ?></td>
145 <td class="memoryp"><?php echo @wfPercent( $this->memory() / self::$totalmemory * 100 ) ?></td>
146 <td class="count"><?php echo $this->count() ?></td>
147 <td class="cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ) ?></td>
148 <td class="tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ) ?></td>
149 <td class="mpc"><?php echo round( sprintf( '%.2f' ,$this->memoryPerCall() / 1024 ), 2 ) ?></td>
150 <td class="tpr"><?php echo @round( sprintf( '%.2f', $this->time() / self::$totalcount ), 2 ) ?></td>
151 <td class="mpr"><?php echo @round( sprintf( '%.2f' ,$this->memory() / self::$totalcount / 1024 ), 2 ) ?></td>
152 </tr>
153 <?php
154 if ( $ex ) {
155 foreach ( $this->children as $child ) {
156 $child->display( $expand, $indent + 2 );
157 }
158 }
159 }
160
161 function name() {
162 return $this->name;
163 }
164
165 function count() {
166 return $this->count;
167 }
168
169 function time() {
170 return $this->time;
171 }
172
173 function memory() {
174 return $this->memory;
175 }
176
177 function timePerCall() {
178 return @( $this->time / $this->count );
179 }
180
181 function memoryPerCall() {
182 return @( $this->memory / $this->count );
183 }
184
185 function callsPerRequest() {
186 return @( $this->count / self::$totalcount );
187 }
188
189 function timePerRequest() {
190 return @( $this->time / self::$totalcount );
191 }
192
193 function memoryPerRequest() {
194 return @( $this->memory / self::$totalcount );
195 }
196
197 function fmttime() {
198 return sprintf( "%5.02f", $this->time );
199 }
200 };
201
202 function compare_point( $a, $b ) {
203 global $sort;
204 switch ( $sort ) {
205 case "name":
206 return strcmp( $a->name(), $b->name() );
207 case "time":
208 return $a->time() > $b->time() ? -1 : 1;
209 case "memory":
210 return $a->memory() > $b->memory() ? -1 : 1;
211 case "count":
212 return $a->count() > $b->count() ? -1 : 1;
213 case "time_per_call":
214 return $a->timePerCall() > $b->timePerCall() ? -1 : 1;
215 case "memory_per_call":
216 return $a->memoryPerCall() > $b->memoryPerCall() ? -1 : 1;
217 case "calls_per_req":
218 return $a->callsPerRequest() > $b->callsPerRequest() ? -1 : 1;
219 case "time_per_req":
220 return $a->timePerRequest() > $b->timePerRequest() ? -1 : 1;
221 case "memory_per_req":
222 return $a->memoryPerRequest() > $b->memoryPerRequest() ? -1 : 1;
223 }
224 }
225
226 $sorts = array( 'time', 'memory', 'count', 'calls_per_req', 'name',
227 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' );
228 $sort = 'time';
229 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) )
230 $sort = $_REQUEST['sort'];
231
232 $res = $dbr->select( 'profiling', '*', array(), 'profileinfo.php', array( 'ORDER BY' => 'pf_name ASC' ) );
233
234 if (isset( $_REQUEST['filter'] ) )
235 $filter = $_REQUEST['filter'];
236 else
237 $filter = '';
238
239 ?>
240 <form method="get" action="profileinfo.php">
241 <p>
242 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
243 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
244 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
245 <input type="submit" value="Filter" />
246 </p>
247 </form>
248
249 <table cellspacing="0" border="1">
250 <tr id="top">
251 <th><a href="<?php echo getEscapedProfileUrl( false, 'name' ) ?>">Name</a></th>
252 <th><a href="<?php echo getEscapedProfileUrl( false, 'time' ) ?>">Time (%)</a></th>
253 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory' ) ?>">Memory (%)</a></th>
254 <th><a href="<?php echo getEscapedProfileUrl( false, 'count' ) ?>">Count</a></th>
255 <th><a href="<?php echo getEscapedProfileUrl( false, 'calls_per_req' ) ?>">Calls/req</a></th>
256 <th><a href="<?php echo getEscapedProfileUrl( false, 'time_per_call' ) ?>">ms/call</a></th>
257 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory_per_call' ) ?>">kb/call</a></th>
258 <th><a href="<?php echo getEscapedProfileUrl( false, 'time_per_req' ) ?>">ms/req</a></th>
259 <th><a href="<?php echo getEscapedProfileUrl( false, 'memory_per_req' ) ?>">kb/req</a></th>
260 </tr>
261 <?php
262 profile_point::$totaltime = 0.0;
263 profile_point::$totalcount = 0;
264 profile_point::$totalmemory = 0.0;
265
266 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
267 global $filter, $sort, $expand;
268
269 if ( $_expand === false )
270 $_expand = $expand;
271
272 return htmlspecialchars(
273 '?' .
274 wfArrayToCGI( array(
275 'filter' => $_filter ? $_filter : $filter,
276 'sort' => $_sort ? $_sort : $sort,
277 'expand' => implode( ',', array_keys( $_expand ) )
278 ) )
279 );
280 }
281
282 $points = array();
283 $queries = array();
284 $sqltotal = 0.0;
285
286 $last = false;
287 foreach( $res as $o ) {
288 $next = new profile_point( $o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory );
289 if( $next->name() == '-total' ) {
290 profile_point::$totaltime = $next->time();
291 profile_point::$totalcount = $next->count();
292 profile_point::$totalmemory = $next->memory();
293 }
294 if ( $last !== false ) {
295 if ( preg_match( "/^".preg_quote( $last->name(), "/" )."/", $next->name() ) ) {
296 $last->add_child($next);
297 continue;
298 }
299 }
300 $last = $next;
301 if ( preg_match( "/^query: /", $next->name() ) || preg_match( "/^query-m: /", $next->name() ) ) {
302 $sqltotal += $next->time();
303 $queries[] = $next;
304 } else {
305 $points[] = $next;
306 }
307 }
308
309 $s = new profile_point( "SQL Queries", 0, $sqltotal, 0, 0 );
310 foreach ( $queries as $q )
311 $s->add_child($q);
312 $points[] = $s;
313
314 usort( $points, "compare_point" );
315
316 foreach ( $points as $point ) {
317 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) )
318 continue;
319
320 $point->display( $expand );
321 }
322 ?>
323 </table>
324
325 <p>Total time: <tt><?php printf("%5.02f", profile_point::$totaltime) ?></tt></p>
326 <p>Total memory: <tt><?php printf("%5.02f", profile_point::$totalmemory / 1024 ) ?></tt></p>
327 </body>
328 </html>