Merge "(bug 19195) Make user IDs more readily available with the API"
[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 $zeroEntry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
33 var $errorEntry;
34
35 public function isPersistent() {
36 /* Implement in output subclasses */
37 return false;
38 }
39
40 protected function addInitialStack() {
41 $this->errorEntry = $this->zeroEntry;
42 $this->errorEntry['count'] = 1;
43
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 );
49
50 $this->profileOut( '-setup' );
51 } else {
52 $this->profileIn( '-total' );
53 }
54 }
55
56 function setMinimum( $min ) {
57 $this->mMinimumTime = $min;
58 }
59
60 function profileIn($functionname) {
61 global $wgDebugFunctionEntry;
62 if ($wgDebugFunctionEntry) {
63 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
64 }
65 $this->mWorkStack[] = array( $functionname, count( $this->mWorkStack ), $this->getTime(), $this->getTime( 'cpu' ) );
66 }
67
68 function profileOut($functionname) {
69 global $wgDebugFunctionEntry;
70
71 if ($wgDebugFunctionEntry) {
72 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
73 }
74
75 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
76
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;
85 }
86 elseif ($ofname != $functionname) {
87 $message = "Profiling error: in({$ofname}), out($functionname)";
88 $this->debug( "$message\n" );
89 $this->mCollated[$message] = $this->errorEntry;
90 }
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;
97 }
98 $entry['cpu'] += $elapsedcpu;
99 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
100 $entry['real'] += $elapsedreal;
101 $entry['real_sq'] += $elapsedreal*$elapsedreal;
102 $entry['count']++;
103
104 }
105 }
106
107 public function getFunctionReport() {
108 /* Implement in output subclasses */
109 return '';
110 }
111
112 public function logData() {
113 /* Implement in subclasses */
114 }
115
116 /**
117 * Get the actual CPU time or the initial one if $ru is set.
118 *
119 * @deprecated in 1.20
120 * @return float|null
121 */
122 function getCpuTime( $ru = null ) {
123 wfDeprecated( __METHOD__, '1.20' );
124
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' );
130 }
131 }
132 }