2 ini_set( 'zlib.output_compression', 'off' );
4 $wgDBadminuser = $wgDBadminpassword = $wgDBserver = $wgDBname = $wgDBprefix = false;
5 $wgEnableProfileInfo = $wgProfileToDatabase = false;
7 define( 'MW_NO_SETUP', 1 );
8 require_once( './includes/WebStart.php' );
9 @include_once( './AdminSettings.php' );
10 require_once( './includes/GlobalFunctions.php' );
16 Copyright 2005 Kate Turner.
18 Permission is hereby granted, free of charge, to any person obtaining a copy
19 of this software and associated documentation files (the "Software"), to deal
20 in the Software without restriction, including without limitation the rights
21 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22 copies of the Software, and to permit persons to whom the Software is
23 furnished to do so, subject to the following conditions:
25 The above copyright notice and this permission notice shall be included in
26 all copies or substantial portions of the Software.
28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 <title>Profiling data</title>
40 <style type="text/css">
43 border-bottom: solid 1px black;
51 td.timep, td.memoryp, td.count, td.cpr, td.tpc, td.mpc, td.tpr, td.mpr {
54 td.timep, td.tpc, td.tpr {
55 background-color: #fffff0;
57 td.memoryp, td.mpc, td.mpr {
58 background-color: #f0f8ff;
61 background-color: #f0fff0;
64 background-color: #f9f9f9;
71 if (!$wgEnableProfileInfo) {
72 echo "<p>Disabled</p>\n";
73 echo "</body></html>";
77 foreach (array("wgDBadminuser", "wgDBadminpassword", "wgDBserver", "wgDBname") as $var)
78 if ($$var === false) {
79 echo "AdminSettings.php not correct\n";
85 if (isset($_REQUEST['expand']))
86 foreach(explode(",", $_REQUEST['expand']) as $f)
95 function profile_point($name, $count, $time, $memory ) {
97 $this->count = $count;
99 $this->memory = $memory;
100 $this->children = array();
103 function add_child($child) {
104 $this->children[] = $child;
107 function display($indent = 0.0) {
108 global $expand, $totaltime, $totalmemory, $totalcount;
109 usort($this->children, "compare_point");
112 if (isset($expand[$this->name()]))
116 if (count($this->children)) {
117 $url = getEscapedProfileUrl(false, false, $expand + array($this->name() => true));
118 $extet = " <a href=\"$url\">[+]</a>";
122 foreach ($expand as $name => $ep)
123 if ($name != $this->name())
124 $e += array($name => $ep);
126 $extet = " <a href=\"" . getEscapedProfileUrl(false, false, $e) . "\">[–]</a>";
130 <td class="name" style="padding-left: <?php echo $indent ?>em;">
131 <?php echo htmlspecialchars($this->name()) . $extet ?>
133 <td class="timep"><?php echo @wfPercent( $this->time() / $totaltime * 100 ) ?></td>
134 <td class="memoryp"><?php echo @wfPercent( $this->memory() / $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() / $totalcount ), 2 ) ?></td>
140 <td class="mpr"><?php echo @round( sprintf( '%.2f' ,$this->memory() / $totalcount / 1024 ), 2 ) ?></td>
144 foreach ($this->children as $child) {
145 $child->display($indent + 2);
163 return $this->memory;
166 function timePerCall() {
167 return @($this->time / $this->count);
170 function memoryPerCall() {
171 return @($this->memory / $this->count);
174 function callsPerRequest() {
176 return @($this->count / $totalcount);
179 function timePerRequest() {
181 return @($this->time / $totalcount);
184 function memoryPerRequest() {
186 return @($this->memory / $totalcount);
190 return sprintf("%5.02f", $this->time);
194 function compare_point($a, $b) {
198 return strcmp($a->name(), $b->name());
200 return $a->time() > $b->time() ? -1 : 1;
202 return $a->memory() > $b->memory() ? -1 : 1;
204 return $a->count() > $b->count() ? -1 : 1;
205 case "time_per_call":
206 return $a->timePerCall() > $b->timePerCall() ? -1 : 1;
207 case "memory_per_call":
208 return $a->memoryPerCall() > $b->memoryPerCall() ? -1 : 1;
209 case "calls_per_req":
210 return $a->callsPerRequest() > $b->callsPerRequest() ? -1 : 1;
212 return $a->timePerRequest() > $b->timePerRequest() ? -1 : 1;
213 case "memory_per_req":
214 return $a->memoryPerRequest() > $b->memoryPerRequest() ? -1 : 1;
218 $sorts = array("time","memory","count","calls_per_req","name","time_per_call","memory_per_call","time_per_req","memory_per_req");
220 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
221 $sort = $_REQUEST['sort'];
223 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
224 or die("mysql server failed: " . mysql_error());
225 mysql_select_db($wgDBname, $dbh) or die(mysql_error($dbh));
227 SELECT pf_count, pf_time, pf_memory, pf_name
228 FROM {$wgDBprefix}profiling
230 ", $dbh) or die("query failed: " . mysql_error());
232 if (isset($_REQUEST['filter']))
233 $filter = $_REQUEST['filter'];
237 <form method="profiling.php">
239 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
240 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
241 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
242 <input type="submit" value="Filter" />
246 <table cellspacing="0" border="1">
248 <th><a href="<?php echo getEscapedProfileUrl(false, "name") ?>">Name</a></th>
249 <th><a href="<?php echo getEscapedProfileUrl(false, "time") ?>">Time (%)</a></th>
250 <th><a href="<?php echo getEscapedProfileUrl(false, "memory") ?>">Memory (%)</a></th>
251 <th><a href="<?php echo getEscapedProfileUrl(false, "count") ?>">Count</a></th>
252 <th><a href="<?php echo getEscapedProfileUrl(false, "calls_per_req") ?>">Calls/req</a></th>
253 <th><a href="<?php echo getEscapedProfileUrl(false, "time_per_call") ?>">ms/call</a></th>
254 <th><a href="<?php echo getEscapedProfileUrl(false, "memory_per_call") ?>">kb/call</a></th>
255 <th><a href="<?php echo getEscapedProfileUrl(false, "time_per_req") ?>">ms/req</a></th>
256 <th><a href="<?php echo getEscapedProfileUrl(false, "memory_per_req") ?>">kb/req</a></th>
263 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
264 global $filter, $sort, $expand;
266 if ( $_expand === false )
269 return htmlspecialchars(
272 'filter' => $_filter ? $_filter : $filter,
273 'sort' => $_sort ? $_sort : $sort,
274 'expand' => implode( ',', array_keys( $_expand ) )
284 while (($o = mysql_fetch_object($res)) !== false) {
285 $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory);
286 if( $next->name() == '-total' ) {
287 $totaltime = $next->time();
288 $totalcount = $next->count();
289 $totalmemory = $next->memory();
291 if ($last !== false) {
292 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
293 $last->add_child($next);
298 if (preg_match("/^query: /", $next->name()) || preg_match("/^query-m: /", $next->name())) {
299 $sqltotal += $next->time();
306 $s = new profile_point("SQL Queries", 0, $sqltotal, 0, 0);
307 foreach ($queries as $q)
311 usort($points, "compare_point");
313 foreach ($points as $point) {
314 if (strlen($filter) && !strstr($point->name(), $filter))
322 <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></tt></p>
323 <p>Total memory: <tt><?php printf("%5.02f", $totalmemory / 1024 ) ?></tt></p>
326 mysql_free_result($res);