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