4 Copyright
2005 Kate Turner
.
6 Permission is hereby granted
, free of charge
, to any person obtaining a copy
7 of this software
and associated documentation
files (the
"Software"), to deal
8 in the Software without restriction
, including without limitation the rights
9 to
use, copy
, modify
, merge
, publish
, distribute
, sublicense
, and/or sell
10 copies of the Software
, and to permit persons to whom the Software is
11 furnished to
do so
, subject to the following conditions
:
13 The above copyright notice
and this permission notice shall be included in
14 all copies
or substantial portions of the Software
.
16 THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND
, EXPRESS
OR
17 IMPLIED
, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
,
18 FITNESS
FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT
. IN NO EVENT SHALL THE
19 AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM
, DAMAGES
OR OTHER
20 LIABILITY
, WHETHER IN AN ACTION OF CONTRACT
, TORT
OR OTHERWISE
, ARISING FROM
,
21 OUT OF
OR IN CONNECTION WITH THE SOFTWARE
OR THE
USE OR OTHER DEALINGS IN THE
27 <title
>Profiling data
</title
>
28 <style type
="text/css">
31 border
-bottom
: solid
1px black
;
39 td
.time
, td
.timep
, td
.memoryp
, td
.count
, td
.cpr
, td
.tpc
, td
.mpc
, td
.tpr
, td
.mpr
{
47 $wgDBadminuser = $wgDBadminpassword = $wgDBserver = $wgDBname = $wgEnableProfileInfo = $wgDBprefix = false;
49 define( 'MW_NO_SETUP', 1 );
50 require_once( './includes/WebStart.php' );
51 require_once("./AdminSettings.php");
52 require_once( './includes/GlobalFunctions.php' );
54 if (!$wgEnableProfileInfo) {
59 foreach (array("wgDBadminuser", "wgDBadminpassword", "wgDBserver", "wgDBname") as $var)
60 if ($
$var === false) {
61 echo "AdminSettings.php not correct\n";
67 if (isset($_REQUEST['expand']))
68 foreach(explode(",", $_REQUEST['expand']) as $f)
77 function profile_point($name, $count, $time, $memory ) {
79 $this->count
= $count;
81 $this->memory
= $memory;
82 $this->children
= array();
85 function add_child($child) {
86 $this->children
[] = $child;
89 function display($indent = 0.0) {
90 global $expand, $totaltime, $totalmemory, $totalcount;
91 usort($this->children
, "compare_point");
94 if (isset($expand[$this->name()]))
98 if (count($this->children
)) {
99 $url = makeurl(false, false, $expand +
array($this->name() => true));
100 $extet = " <a href=\"$url\">[+]</a>";
104 foreach ($expand as $name => $ep)
105 if ($name != $this->name())
106 $e +
= array($name => $ep);
108 $extet = " <a href=\"" . makeurl(false, false, $e) . "\">[–]</a>";
112 <td
class="name" style
="padding-left: <?php echo $indent ?>em">
113 <?php
echo htmlspecialchars($this->name()) . $extet ?
>
115 <td
class="timep"><?php
echo wfPercent( $this->time() / $totaltime * 100 ) ?
></td
>
116 <td
class="memoryp"><?php
echo wfPercent( $this->memory() / $totalmemory * 100 ) ?
></td
>
117 <td
class="count"><?php
echo $this->count() ?
></td
>
118 <td
class="cpr"><?php
echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ) ?
>
119 <td
class="tpc"><?php
echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ) ?
>
120 <td
class="mpc"><?php
echo round( sprintf( '%.2f' ,$this->memoryPerCall() / 1048576 ), 2 ) ?
>
121 <td
class="tpr"><?php
echo round( sprintf( '%.2f', $this->time() / $totalcount ), 2 ) ?
>
122 <td
class="mpr"><?php
echo round( sprintf( '%.2f' ,$this->memory() / $totalcount / 1048576 ), 2 ) ?
>
126 foreach ($this->children
as $child)
127 $child->display($indent +
2);
143 return $this->memory
;
146 function timePerCall() {
147 return @($this->time
/ $this->count
);
150 function memoryPerCall() {
151 return @($this->memory
/ $this->count
);
154 function callsPerRequest() {
156 return @($this->count
/ $totalcount);
159 function timePerRequest() {
161 return @($this->time
/ $totalcount);
164 function memoryPerRequest() {
166 return @($this->memory
/ $totalcount);
170 return sprintf("%5.02f", $this->time
);
174 function compare_point($a, $b) {
178 return strcmp($a->name(), $b->name());
180 return $a->time() > $b->time() ?
-1 : 1;
182 return $a->memory() > $b->memory() ?
-1 : 1;
184 return $a->count() > $b->count() ?
-1 : 1;
185 case "time_per_call":
186 return $a->timePerCall() > $b->timePerCall() ?
-1 : 1;
187 case "memory_per_call":
188 return $a->memoryPerCall() > $b->memoryPerCall() ?
-1 : 1;
189 case "calls_per_req":
190 return $a->callsPerRequest() > $b->callsPerRequest() ?
-1 : 1;
192 return $a->timePerRequest() > $b->timePerRequest() ?
-1 : 1;
193 case "memory_per_req":
194 return $a->memoryPerRequest() > $b->memoryPerRequest() ?
-1 : 1;
198 $sorts = array("time","memory","count","calls_per_req","name","time_per_call","memory_per_call","time_per_req","memory_per_req");
200 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
201 $sort = $_REQUEST['sort'];
203 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
204 or die("mysql server failed: " . mysql_error());
205 mysql_select_db($wgDBname, $dbh) or die(mysql_error($dbh));
207 SELECT pf_count, pf_time, pf_memory, pf_name
208 FROM {$wgDBprefix}profiling
210 ", $dbh) or die("query failed: " . mysql_error());
212 if (isset($_REQUEST['filter']))
213 $filter = $_REQUEST['filter'];
217 <form method
="profiling.php">
219 <input type
="text" name
="filter" value
="<?php echo htmlspecialchars($filter)?>"/>
220 <input type
="hidden" name
="sort" value
="<?php echo htmlspecialchars($sort)?>"/>
221 <input type
="hidden" name
="expand" value
="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
222 <input type
="submit" value
="Filter" />
226 <table cellspacing
="0">
228 <th
><a href
="<?php echo makeurl(false, "name
") ?>">Name
</a
></th
>
229 <th
><a href
="<?php echo makeurl(false, "time
") ?>">Time (%
)</a
></th
>
230 <th
><a href
="<?php echo makeurl(false, "memory
") ?>">Memory (%
)</a
></th
>
231 <th
><a href
="<?php echo makeurl(false, "count
") ?>">Count
</a
></th
>
232 <th
><a href
="<?php echo makeurl(false, "calls_per_req
") ?>">Calls
/request
</a
></th
>
233 <th
><a href
="<?php echo makeurl(false, "time_per_call
") ?>">ms
/call
</a
></th
>
234 <th
><a href
="<?php echo makeurl(false, "memory_per_call
") ?>">kb
/call
</a
></th
>
235 <th
><a href
="<?php echo makeurl(false, "time_per_req
") ?>">ms
/request
</a
></th
>
236 <th
><a href
="<?php echo makeurl(false, "memory_per_req
") ?>">kb
/request
</a
></th
>
243 function makeurl($_filter = false, $_sort = false, $_expand = false) {
244 global $filter, $sort, $expand;
246 if ($_expand === false)
249 $nfilter = $_filter ?
$_filter : $filter;
250 $nsort = $_sort ?
$_sort : $sort;
251 $exp = urlencode(implode(',', array_keys($_expand)));
252 return "?filter=$nfilter&sort=$nsort&expand=$exp";
260 while (($o = mysql_fetch_object($res)) !== false) {
261 $next = new profile_point($o->pf_name
, $o->pf_count
, $o->pf_time
, $o->pf_memory
);
262 if( $next->name() == '-total' ) {
263 $totaltime = $next->time();
264 $totalcount = $next->count();
265 $totalmemory = $next->memory();
267 if ($last !== false) {
268 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
269 $last->add_child($next);
274 if (preg_match("/^query: /", $next->name())) {
275 $sqltotal +
= $next->time();
282 $s = new profile_point("SQL Queries", 0, $sqltotal, 0, 0);
283 foreach ($queries as $q)
287 usort($points, "compare_point");
289 foreach ($points as $point) {
290 if (strlen($filter) && !strstr($point->name(), $filter))
298 <p
>Total time
: <tt
><?php
printf("%5.02f", $totaltime) ?
></tt
></p
>
299 <p
>Total memory
: <tt
><?php
printf("%5.02f", $totalmemory) ?
></tt
></p
>
302 mysql_free_result($res);