8 * Simple profiler base class.
9 * @todo document methods (?)
12 class ProfilerSimple
extends Profiler
{
13 var $mMinimumTime = 0;
15 var $zeroEntry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
18 protected function addInitialStack() {
19 $this->errorEntry
= $this->zeroEntry
;
20 $this->errorEntry
['count'] = 1;
22 $initialTime = $this->getInitialTime();
23 $initialCpu = $this->getInitialTime( 'cpu' );
24 if ( $initialTime !== null && $initialCpu !== null ) {
25 $this->mWorkStack
[] = array( '-total', 0, $initialTime, $initialCpu );
26 $this->mWorkStack
[] = array( '-setup', 1, $initialTime, $initialCpu );
28 $this->profileOut( '-setup' );
30 $this->profileIn( '-total' );
34 function setMinimum( $min ) {
35 $this->mMinimumTime
= $min;
38 function profileIn($functionname) {
39 global $wgDebugFunctionEntry;
40 if ($wgDebugFunctionEntry) {
41 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
43 $this->mWorkStack
[] = array( $functionname, count( $this->mWorkStack
), $this->getTime(), $this->getTime( 'cpu' ) );
46 function profileOut($functionname) {
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" );
62 $this->mCollated
[$message] = $this->errorEntry
;
64 elseif ($ofname != $functionname) {
65 $message = "Profiling error: in({$ofname}), out($functionname)";
66 $this->debug( "$message\n" );
67 $this->mCollated
[$message] = $this->errorEntry
;
69 $entry =& $this->mCollated
[$functionname];
70 $elapsedcpu = $this->getTime( 'cpu' ) - $octime;
71 $elapsedreal = $this->getTime() - $ortime;
72 if (!is_array($entry)) {
73 $entry = $this->zeroEntry
;
74 $this->mCollated
[$functionname] =& $entry;
76 $entry['cpu'] +
= $elapsedcpu;
77 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
78 $entry['real'] +
= $elapsedreal;
79 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
85 public function getFunctionReport() {
86 /* Implement in output subclasses */
90 public function logData() {
91 /* Implement in subclasses */
95 * Get the actual CPU time or the initial one if $ru is set.
100 function getCpuTime( $ru = null ) {
101 wfDeprecated( __METHOD__
, '1.20' );
103 if ( $ru === null ) {
104 return $this->getTime( 'cpu' );
106 # It theory we should use $ru here, but it always $wgRUstart that is passed here
107 return $this->getInitialTime( 'cpu' );