Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / profiler / ProfilerSimple.php
blobb59c528004fee87d5b8861cc4943a19a9d56df75
1 <?php
2 /**
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
20 * @file
21 * @ingroup Profiler
24 /**
25 * Simple profiler base class.
26 * @todo document methods (?)
27 * @ingroup Profiler
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 );
33 var $errorEntry;
35 public function isPersistent() {
36 /* Implement in output subclasses */
37 return false;
40 protected function addInitialStack() {
41 $this->errorEntry = $this->zeroEntry;
42 $this->errorEntry['count'] = 1;
44 $initialTime = $this->getInitialTime();
45 $initialCpu = $this->getInitialTime( 'cpu' );
46 if ( $initialTime !== null && $initialCpu !== null ) {
47 $this->mWorkStack[] = array( '-total', 0, $initialTime, $initialCpu );
48 $this->mWorkStack[] = array( '-setup', 1, $initialTime, $initialCpu );
50 $this->profileOut( '-setup' );
51 } else {
52 $this->profileIn( '-total' );
56 function setMinimum( $min ) {
57 $this->mMinimumTime = $min;
60 function profileIn( $functionname ) {
61 global $wgDebugFunctionEntry;
62 if ( $wgDebugFunctionEntry ) {
63 $this->debug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering ' . $functionname . "\n" );
65 $this->mWorkStack[] = array( $functionname, count( $this->mWorkStack ), $this->getTime(), $this->getTime( 'cpu' ) );
68 function profileOut( $functionname ) {
69 global $wgDebugFunctionEntry;
71 if ( $wgDebugFunctionEntry ) {
72 $this->debug( str_repeat( ' ', count( $this->mWorkStack ) - 1 ) . 'Exiting ' . $functionname . "\n" );
75 list( $ofname, /* $ocount */, $ortime, $octime ) = array_pop( $this->mWorkStack );
77 if ( !$ofname ) {
78 $this->debug( "Profiling error: $functionname\n" );
79 } else {
80 if ( $functionname == 'close' ) {
81 $message = "Profile section ended by close(): {$ofname}";
82 $functionname = $ofname;
83 $this->debug( "$message\n" );
84 $this->mCollated[$message] = $this->errorEntry;
86 elseif ( $ofname != $functionname ) {
87 $message = "Profiling error: in({$ofname}), out($functionname)";
88 $this->debug( "$message\n" );
89 $this->mCollated[$message] = $this->errorEntry;
91 $entry =& $this->mCollated[$functionname];
92 $elapsedcpu = $this->getTime( 'cpu' ) - $octime;
93 $elapsedreal = $this->getTime() - $ortime;
94 if ( !is_array( $entry ) ) {
95 $entry = $this->zeroEntry;
96 $this->mCollated[$functionname] =& $entry;
98 $entry['cpu'] += $elapsedcpu;
99 $entry['cpu_sq'] += $elapsedcpu * $elapsedcpu;
100 $entry['real'] += $elapsedreal;
101 $entry['real_sq'] += $elapsedreal * $elapsedreal;
102 $entry['count']++;
107 public function getFunctionReport() {
108 /* Implement in output subclasses */
109 return '';
112 public function logData() {
113 /* Implement in subclasses */
117 * Get the actual CPU time or the initial one if $ru is set.
119 * @deprecated in 1.20
120 * @return float|null
122 function getCpuTime( $ru = null ) {
123 wfDeprecated( __METHOD__, '1.20' );
125 if ( $ru === null ) {
126 return $this->getTime( 'cpu' );
127 } else {
128 # It theory we should use $ru here, but it always $wgRUstart that is passed here
129 return $this->getInitialTime( 'cpu' );