3 * Base class for simple profiling.
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.
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.
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
25 * Simple profiler base class.
26 * @todo document methods (?)
29 class ProfilerSimple
extends Profiler
{
30 var $mMinimumTime = 0;
32 var $zeroEntry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
35 protected function addInitialStack() {
36 $this->errorEntry
= $this->zeroEntry
;
37 $this->errorEntry
['count'] = 1;
39 $initialTime = $this->getInitialTime();
40 $initialCpu = $this->getInitialTime( 'cpu' );
41 if ( $initialTime !== null && $initialCpu !== null ) {
42 $this->mWorkStack
[] = array( '-total', 0, $initialTime, $initialCpu );
43 $this->mWorkStack
[] = array( '-setup', 1, $initialTime, $initialCpu );
45 $this->profileOut( '-setup' );
47 $this->profileIn( '-total' );
51 function setMinimum( $min ) {
52 $this->mMinimumTime
= $min;
55 function profileIn($functionname) {
56 global $wgDebugFunctionEntry;
57 if ($wgDebugFunctionEntry) {
58 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
60 $this->mWorkStack
[] = array( $functionname, count( $this->mWorkStack
), $this->getTime(), $this->getTime( 'cpu' ) );
63 function profileOut($functionname) {
64 global $wgDebugFunctionEntry;
66 if ($wgDebugFunctionEntry) {
67 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
70 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack
);
73 $this->debug("Profiling error: $functionname\n");
75 if ($functionname == 'close') {
76 $message = "Profile section ended by close(): {$ofname}";
77 $functionname = $ofname;
78 $this->debug( "$message\n" );
79 $this->mCollated
[$message] = $this->errorEntry
;
81 elseif ($ofname != $functionname) {
82 $message = "Profiling error: in({$ofname}), out($functionname)";
83 $this->debug( "$message\n" );
84 $this->mCollated
[$message] = $this->errorEntry
;
86 $entry =& $this->mCollated
[$functionname];
87 $elapsedcpu = $this->getTime( 'cpu' ) - $octime;
88 $elapsedreal = $this->getTime() - $ortime;
89 if (!is_array($entry)) {
90 $entry = $this->zeroEntry
;
91 $this->mCollated
[$functionname] =& $entry;
93 $entry['cpu'] +
= $elapsedcpu;
94 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
95 $entry['real'] +
= $elapsedreal;
96 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
102 public function getFunctionReport() {
103 /* Implement in output subclasses */
107 public function logData() {
108 /* Implement in subclasses */
112 * Get the actual CPU time or the initial one if $ru is set.
114 * @deprecated in 1.20
117 function getCpuTime( $ru = null ) {
118 wfDeprecated( __METHOD__
, '1.20' );
120 if ( $ru === null ) {
121 return $this->getTime( 'cpu' );
123 # It theory we should use $ru here, but it always $wgRUstart that is passed here
124 return $this->getInitialTime( 'cpu' );