Merge "Revert "Forward port of https://www.mediawiki.org/wiki/Special:Code/MediaWiki...
[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 protected function addInitialStack() {
36 $this->errorEntry = $this->zeroEntry;
37 $this->errorEntry['count'] = 1;
38
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 );
44
45 $this->profileOut( '-setup' );
46 } else {
47 $this->profileIn( '-total' );
48 }
49 }
50
51 function setMinimum( $min ) {
52 $this->mMinimumTime = $min;
53 }
54
55 function profileIn($functionname) {
56 global $wgDebugFunctionEntry;
57 if ($wgDebugFunctionEntry) {
58 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
59 }
60 $this->mWorkStack[] = array( $functionname, count( $this->mWorkStack ), $this->getTime(), $this->getTime( 'cpu' ) );
61 }
62
63 function profileOut($functionname) {
64 global $wgDebugFunctionEntry;
65
66 if ($wgDebugFunctionEntry) {
67 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
68 }
69
70 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
71
72 if (!$ofname) {
73 $this->debug("Profiling error: $functionname\n");
74 } else {
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;
80 }
81 elseif ($ofname != $functionname) {
82 $message = "Profiling error: in({$ofname}), out($functionname)";
83 $this->debug( "$message\n" );
84 $this->mCollated[$message] = $this->errorEntry;
85 }
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;
92 }
93 $entry['cpu'] += $elapsedcpu;
94 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
95 $entry['real'] += $elapsedreal;
96 $entry['real_sq'] += $elapsedreal*$elapsedreal;
97 $entry['count']++;
98
99 }
100 }
101
102 public function getFunctionReport() {
103 /* Implement in output subclasses */
104 return '';
105 }
106
107 public function logData() {
108 /* Implement in subclasses */
109 }
110
111 /**
112 * Get the actual CPU time or the initial one if $ru is set.
113 *
114 * @deprecated in 1.20
115 * @return float|null
116 */
117 function getCpuTime( $ru = null ) {
118 wfDeprecated( __METHOD__, '1.20' );
119
120 if ( $ru === null ) {
121 return $this->getTime( 'cpu' );
122 } else {
123 # It theory we should use $ru here, but it always $wgRUstart that is passed here
124 return $this->getInitialTime( 'cpu' );
125 }
126 }
127 }