Remove deprecated profiling config parameters, clarify docs
[lhc/web/wiklou.git] / includes / profiler / Profiler.php
1 <?php
2 /**
3 * Base class for 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 * @defgroup Profiler Profiler
23 */
24
25 /**
26 * Profiler base class that defines the interface and some trivial
27 * functionality
28 *
29 * @ingroup Profiler
30 */
31 abstract class Profiler {
32 /** @var string|bool Profiler ID for bucketing data */
33 protected $profileID = false;
34 /** @var bool Whether MediaWiki is in a SkinTemplate output context */
35 protected $templated = false;
36 /** @var array All of the params passed from $wgProfiler */
37 protected $params = array();
38
39 /** @var TransactionProfiler */
40 protected $trxProfiler;
41
42 /**
43 * @var array Mapping of output type to class name
44 */
45 private static $outputTypes = array(
46 'db' => 'ProfilerOutputDb',
47 'text' => 'ProfilerOutputText',
48 'udp' => 'ProfilerOutputUdp',
49 );
50
51 // @codingStandardsIgnoreStart PSR2.Classes.PropertyDeclaration.Underscore
52 /** @var Profiler Do not call this outside Profiler and ProfileSection */
53 public static $__instance = null;
54 // @codingStandardsIgnoreEnd
55
56 /**
57 * @param array $params
58 */
59 public function __construct( array $params ) {
60 if ( isset( $params['profileID'] ) ) {
61 $this->profileID = $params['profileID'];
62 }
63 $this->params = $params;
64 $this->trxProfiler = new TransactionProfiler();
65 }
66
67 /**
68 * Singleton
69 * @return Profiler
70 */
71 final public static function instance() {
72 if ( self::$__instance === null ) {
73 global $wgProfiler;
74 if ( is_array( $wgProfiler ) ) {
75 $class = isset( $wgProfiler['class'] ) ? $wgProfiler['class'] : 'ProfilerStub';
76 $factor = isset( $wgProfiler['sampling'] ) ? $wgProfiler['sampling'] : 1;
77 if ( PHP_SAPI === 'cli' || mt_rand( 0, $factor - 1 ) != 0 ) {
78 $class = 'ProfilerStub';
79 }
80 /** @var Profiler */
81 self::$__instance = new $class( $wgProfiler );
82 } else {
83 self::$__instance = new ProfilerStub( array() );
84 }
85 }
86 return self::$__instance;
87 }
88
89 /**
90 * Replace the current profiler with $profiler if no non-stub profiler is set
91 *
92 * @param Profiler $profiler
93 * @throws MWException
94 * @since 1.25
95 */
96 final public static function replaceStubInstance( Profiler $profiler ) {
97 if ( self::$__instance && !( self::$__instance instanceof ProfilerStub ) ) {
98 throw new MWException( 'Could not replace non-stub profiler instance.' );
99 } else {
100 self::$__instance = $profiler;
101 }
102 }
103
104 /**
105 * @param string $id
106 */
107 public function setProfileID( $id ) {
108 $this->profileID = $id;
109 }
110
111 /**
112 * @return string
113 */
114 public function getProfileID() {
115 if ( $this->profileID === false ) {
116 return wfWikiID();
117 } else {
118 return $this->profileID;
119 }
120 }
121
122 /**
123 * Called by wfProfieIn()
124 *
125 * @param string $functionname
126 */
127 abstract public function profileIn( $functionname );
128
129 /**
130 * Called by wfProfieOut()
131 *
132 * @param string $functionname
133 */
134 abstract public function profileOut( $functionname );
135
136 /**
137 * Mark the start of a custom profiling frame (e.g. DB queries).
138 * The frame ends when the result of this method falls out of scope.
139 *
140 * @param string $section
141 * @return ScopedCallback|null
142 * @since 1.25
143 */
144 abstract public function scopedProfileIn( $section );
145
146 /**
147 * @param ScopedCallback $section
148 */
149 public function scopedProfileOut( ScopedCallback &$section ) {
150 $section = null;
151 }
152
153 /**
154 * @return TransactionProfiler
155 * @since 1.25
156 */
157 public function getTransactionProfiler() {
158 return $this->trxProfiler;
159 }
160
161 /**
162 * Close opened profiling sections
163 */
164 abstract public function close();
165
166 /**
167 * Log the data to some store or even the page output
168 *
169 * @throws MWException
170 * @since 1.25
171 */
172 public function logData() {
173 $output = isset( $this->params['output'] ) ? $this->params['output'] : null;
174
175 if ( !$output || $this instanceof ProfilerStub ) {
176 // return early when no output classes defined or we're a stub
177 return;
178 }
179
180 if ( !is_array( $output ) ) {
181 $output = array( $output );
182 }
183
184 foreach ( $output as $outType ) {
185 if ( !isset( self::$outputTypes[$outType] ) ) {
186 throw new MWException( "'$outType' is an invalid output type" );
187 }
188 $class = self::$outputTypes[$outType];
189
190 /** @var ProfilerOutput $profileOut */
191 $profileOut = new $class( $this, $this->params );
192 if ( $profileOut->canUse() ) {
193 $profileOut->log( $this->getFunctionStats() );
194 }
195 }
196 }
197
198 /**
199 * Get the content type sent out to the client.
200 * Used for profilers that output instead of store data.
201 * @return string
202 * @since 1.25
203 */
204 public function getContentType() {
205 foreach ( headers_list() as $header ) {
206 if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
207 return $m[1];
208 }
209 }
210 return null;
211 }
212
213 /**
214 * Mark this call as templated or not
215 *
216 * @param bool $t
217 */
218 public function setTemplated( $t ) {
219 $this->templated = $t;
220 }
221
222 /**
223 * Was this call as templated or not
224 *
225 * @return bool
226 */
227 public function getTemplated() {
228 return $this->templated;
229 }
230
231 /**
232 * Get the aggregated inclusive profiling data for each method
233 *
234 * The percent time for each time is based on the current "total" time
235 * used is based on all methods so far. This method can therefore be
236 * called several times in between several profiling calls without the
237 * delays in usage of the profiler skewing the results. A "-total" entry
238 * is always included in the results.
239 *
240 * When a call chain involves a method invoked within itself, any
241 * entries for the cyclic invocation should be be demarked with "@".
242 * This makes filtering them out easier and follows the xhprof style.
243 *
244 * @return array List of method entries arrays, each having:
245 * - name : method name
246 * - calls : the number of invoking calls
247 * - real : real time ellapsed (ms)
248 * - %real : percent real time
249 * - cpu : CPU time ellapsed (ms)
250 * - %cpu : percent CPU time
251 * - memory : memory used (bytes)
252 * - %memory : percent memory used
253 * @since 1.25
254 */
255 abstract public function getFunctionStats();
256
257 /**
258 * Returns a profiling output to be stored in debug file
259 *
260 * @return string
261 */
262 abstract public function getOutput();
263 }