Disabling profiling in the debug toolbar for ProfilerSimple (for now)
[lhc/web/wiklou.git] / includes / profiler / ProfilerSimple.php
1 <?php
2 /**
3 * Base class for simple profiling.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Profiler
22 */
23
24 /**
25 * Simple profiler base class.
26 * @todo document methods (?)
27 * @ingroup Profiler
28 */
29 class ProfilerSimple extends Profiler {
30 var $mMinimumTime = 0;
31
32 var $errorEntry;
33
34 public function getZeroEntry() {
35 return array(
36 'cpu' => 0.0,
37 'cpu_sq' => 0.0,
38 'real' => 0.0,
39 'real_sq' => 0.0,
40 'count' => 0
41 );
42 }
43
44 public function getErrorEntry() {
45 $entry = $this->getZeroEntry();
46 $entry['count'] = 1;
47 return $entry;
48 }
49
50 public function updateEntry( $name, $elapsedCpu, $elapsedReal ) {
51 $entry =& $this->mCollated[$name];
52 if ( !is_array( $entry ) ) {
53 $entry = $this->getZeroEntry();
54 $this->mCollated[$name] =& $entry;
55 }
56 $entry['cpu'] += $elapsedCpu;
57 $entry['cpu_sq'] += $elapsedCpu * $elapsedCpu;
58 $entry['real'] += $elapsedReal;
59 $entry['real_sq'] += $elapsedReal * $elapsedReal;
60 $entry['count']++;
61 }
62
63 public function isPersistent() {
64 /* Implement in output subclasses */
65 return false;
66 }
67
68 protected function addInitialStack() {
69 $this->errorEntry = $this->getErrorEntry();
70
71 $initialTime = $this->getInitialTime();
72 $initialCpu = $this->getInitialTime( 'cpu' );
73 if ( $initialTime !== null && $initialCpu !== null ) {
74 $this->mWorkStack[] = array( '-total', 0, $initialTime, $initialCpu );
75 $this->mWorkStack[] = array( '-setup', 1, $initialTime, $initialCpu );
76
77 $this->profileOut( '-setup' );
78 } else {
79 $this->profileIn( '-total' );
80 }
81 }
82
83 function setMinimum( $min ) {
84 $this->mMinimumTime = $min;
85 }
86
87 function profileIn( $functionname ) {
88 global $wgDebugFunctionEntry;
89 if ( $wgDebugFunctionEntry ) {
90 $this->debug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering ' . $functionname . "\n" );
91 }
92 $this->mWorkStack[] = array( $functionname, count( $this->mWorkStack ), $this->getTime(), $this->getTime( 'cpu' ) );
93 }
94
95 function profileOut( $functionname ) {
96 global $wgDebugFunctionEntry;
97
98 if ( $wgDebugFunctionEntry ) {
99 $this->debug( str_repeat( ' ', count( $this->mWorkStack ) - 1 ) . 'Exiting ' . $functionname . "\n" );
100 }
101
102 list( $ofname, /* $ocount */, $ortime, $octime ) = array_pop( $this->mWorkStack );
103
104 if ( !$ofname ) {
105 $this->debugGroup( 'profileerror', "Profiling error: $functionname" );
106 } else {
107 if ( $functionname == 'close' ) {
108 if ( $ofname != '-total' ) {
109 $message = "Profile section ended by close(): {$ofname}";
110 $this->debugGroup( 'profileerror', $message );
111 $this->mCollated[$message] = $this->errorEntry;
112 }
113 $functionname = $ofname;
114 } elseif ( $ofname != $functionname ) {
115 $message = "Profiling error: in({$ofname}), out($functionname)";
116 $this->debugGroup( 'profileerror', $message );
117 $this->mCollated[$message] = $this->errorEntry;
118 }
119 $elapsedcpu = $this->getTime( 'cpu' ) - $octime;
120 $elapsedreal = $this->getTime() - $ortime;
121 $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal );
122 $this->updateTrxProfiling( $functionname, $elapsedreal );
123 }
124 }
125
126 public function getRawData() {
127 // Calling the method of the parent class results in fatal error.
128 // @todo Implement this correctly.
129 return array();
130 }
131
132 public function getFunctionReport() {
133 /* Implement in output subclasses */
134 return '';
135 }
136
137 public function logData() {
138 /* Implement in subclasses */
139 }
140 }