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