3 * Simple profiler base class
11 require_once(dirname(__FILE__
).'/Profiler.php');
13 class ProfilerSimple
extends Profiler
{
14 function ProfilerSimple() {
15 global $wgRequestTime,$wgRUstart;
16 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
17 $this->mWorkStack
[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
19 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
20 $elapsedreal = microtime(true) - $wgRequestTime;
22 $entry =& $this->mCollated
["-setup"];
23 if (!is_array($entry)) {
24 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
25 $this->mCollated
[$functionname] =& $entry;
28 $entry['cpu'] +
= $elapsedcpu;
29 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
30 $entry['real'] +
= $elapsedreal;
31 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
36 function profileIn($functionname) {
37 global $wgDebugFunctionEntry;
38 if ($wgDebugFunctionEntry) {
39 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
41 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(true), $this->getCpuTime());
44 function profileOut($functionname) {
45 $memory = memory_get_usage();
47 global $wgDebugFunctionEntry;
49 if ($wgDebugFunctionEntry) {
50 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
53 list($ofname,$ocount,$ortime,$octime) = array_pop($this->mWorkStack
);
56 $this->debug("Profiling error: $functionname\n");
58 if ($functionname == 'close') {
59 $message = "Profile section ended by close(): {$ofname}";
60 $functionname = $ofname;
61 $this->debug( "$message\n" );
63 elseif ($ofname != $functionname) {
64 $message = "Profiling error: in({$ofname}), out($functionname)";
65 $this->debug( "$message\n" );
67 $entry =& $this->mCollated
[$functionname];
68 $elapsedcpu = $this->getCpuTime() - $octime;
69 $elapsedreal = microtime(true) - $ortime;
70 if (!is_array($entry)) {
71 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
72 $this->mCollated
[$functionname] =& $entry;
75 $entry['cpu'] +
= $elapsedcpu;
76 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
77 $entry['real'] +
= $elapsedreal;
78 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
84 function getFunctionReport() {
85 /* Implement in output subclasses */
88 function getCpuTime($ru=null) {
89 if ( function_exists( 'getrusage' ) ) {
92 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
93 $ru['ru_stime.tv_usec']) * 1e-6);
99 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
100 function getTime($time=null) {
102 return microtime(true);
103 list($a,$b)=explode(" ",$time);
104 return (float)($a+
$b);
107 function debug( $s ) {
108 if (function_exists( 'wfDebug' ) ) {