Merge "Remove unused $wgDebugDBTransactions"
[lhc/web/wiklou.git] / includes / profiler / ProfilerSimple.php
1 <?php
2 /**
3 * @file
4 * @ingroup Profiler
5 */
6
7 /**
8 * Simple profiler base class.
9 * @todo document methods (?)
10 * @ingroup Profiler
11 */
12 class ProfilerSimple extends Profiler {
13 var $mMinimumTime = 0;
14
15 var $zeroEntry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
16 var $errorEntry;
17
18 protected function addInitialStack() {
19 $this->errorEntry = $this->zeroEntry;
20 $this->errorEntry['count'] = 1;
21
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 );
27
28 $this->profileOut( '-setup' );
29 } else {
30 $this->profileIn( '-total' );
31 }
32 }
33
34 function setMinimum( $min ) {
35 $this->mMinimumTime = $min;
36 }
37
38 function profileIn($functionname) {
39 global $wgDebugFunctionEntry;
40 if ($wgDebugFunctionEntry) {
41 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
42 }
43 $this->mWorkStack[] = array( $functionname, count( $this->mWorkStack ), $this->getTime(), $this->getTime( 'cpu' ) );
44 }
45
46 function profileOut($functionname) {
47 global $wgDebugFunctionEntry;
48
49 if ($wgDebugFunctionEntry) {
50 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
51 }
52
53 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
54
55 if (!$ofname) {
56 $this->debug("Profiling error: $functionname\n");
57 } else {
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;
63 }
64 elseif ($ofname != $functionname) {
65 $message = "Profiling error: in({$ofname}), out($functionname)";
66 $this->debug( "$message\n" );
67 $this->mCollated[$message] = $this->errorEntry;
68 }
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;
75 }
76 $entry['cpu'] += $elapsedcpu;
77 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
78 $entry['real'] += $elapsedreal;
79 $entry['real_sq'] += $elapsedreal*$elapsedreal;
80 $entry['count']++;
81
82 }
83 }
84
85 public function getFunctionReport() {
86 /* Implement in output subclasses */
87 return '';
88 }
89
90 public function logData() {
91 /* Implement in subclasses */
92 }
93
94 /**
95 * Get the actual CPU time or the initial one if $ru is set.
96 *
97 * @deprecated in 1.20
98 * @return float|null
99 */
100 function getCpuTime( $ru = null ) {
101 wfDeprecated( __METHOD__, '1.20' );
102
103 if ( $ru === null ) {
104 return $this->getTime( 'cpu' );
105 } else {
106 # It theory we should use $ru here, but it always $wgRUstart that is passed here
107 return $this->getInitialTime( 'cpu' );
108 }
109 }
110 }