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