Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / profiler / Profiler.php
blob297dcb20a13a5e0ffc442deb0f99e540dfc67938
1 <?php
2 /**
3 * Base class for 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
22 * @defgroup Profiler Profiler
25 /**
26 * Profiler base class that defines the interface and some trivial
27 * functionality
29 * @ingroup Profiler
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 = [];
38 /** @var IContextSource Current request context */
39 protected $context = null;
40 /** @var TransactionProfiler */
41 protected $trxProfiler;
42 /** @var Profiler */
43 private static $instance = null;
45 /**
46 * @param array $params
48 public function __construct( array $params ) {
49 if ( isset( $params['profileID'] ) ) {
50 $this->profileID = $params['profileID'];
52 $this->params = $params;
53 $this->trxProfiler = new TransactionProfiler();
56 /**
57 * Singleton
58 * @return Profiler
60 final public static function instance() {
61 if ( self::$instance === null ) {
62 global $wgProfiler, $wgProfileLimit;
64 $params = [
65 'class' => 'ProfilerStub',
66 'sampling' => 1,
67 'threshold' => $wgProfileLimit,
68 'output' => [],
70 if ( is_array( $wgProfiler ) ) {
71 $params = array_merge( $params, $wgProfiler );
74 $inSample = mt_rand( 0, $params['sampling'] - 1 ) === 0;
75 if ( PHP_SAPI === 'cli' || !$inSample ) {
76 $params['class'] = 'ProfilerStub';
79 if ( !is_array( $params['output'] ) ) {
80 $params['output'] = [ $params['output'] ];
83 self::$instance = new $params['class']( $params );
85 return self::$instance;
88 /**
89 * Replace the current profiler with $profiler if no non-stub profiler is set
91 * @param Profiler $profiler
92 * @throws MWException
93 * @since 1.25
95 final public static function replaceStubInstance( Profiler $profiler ) {
96 if ( self::$instance && !( self::$instance instanceof ProfilerStub ) ) {
97 throw new MWException( 'Could not replace non-stub profiler instance.' );
98 } else {
99 self::$instance = $profiler;
104 * @param string $id
106 public function setProfileID( $id ) {
107 $this->profileID = $id;
111 * @return string
113 public function getProfileID() {
114 if ( $this->profileID === false ) {
115 return wfWikiID();
116 } else {
117 return $this->profileID;
122 * Sets the context for this Profiler
124 * @param IContextSource $context
125 * @since 1.25
127 public function setContext( $context ) {
128 $this->context = $context;
132 * Gets the context for this Profiler
134 * @return IContextSource
135 * @since 1.25
137 public function getContext() {
138 if ( $this->context ) {
139 return $this->context;
140 } else {
141 wfDebug( __METHOD__ . " called and \$context is null. " .
142 "Return RequestContext::getMain(); for sanity\n" );
143 return RequestContext::getMain();
147 // Kept BC for now, remove when possible
148 public function profileIn( $functionname ) {
151 public function profileOut( $functionname ) {
155 * Mark the start of a custom profiling frame (e.g. DB queries).
156 * The frame ends when the result of this method falls out of scope.
158 * @param string $section
159 * @return ScopedCallback|null
160 * @since 1.25
162 abstract public function scopedProfileIn( $section );
165 * @param ScopedCallback $section
167 public function scopedProfileOut( ScopedCallback &$section = null ) {
168 $section = null;
172 * @return TransactionProfiler
173 * @since 1.25
175 public function getTransactionProfiler() {
176 return $this->trxProfiler;
180 * Close opened profiling sections
182 abstract public function close();
185 * Get all usable outputs.
187 * @throws MWException
188 * @return array Array of ProfilerOutput instances.
189 * @since 1.25
191 private function getOutputs() {
192 $outputs = [];
193 foreach ( $this->params['output'] as $outputType ) {
194 // The class may be specified as either the full class name (for
195 // example, 'ProfilerOutputStats') or (for backward compatibility)
196 // the trailing portion of the class name (for example, 'stats').
197 $outputClass = strpos( $outputType, 'ProfilerOutput' ) === false
198 ? 'ProfilerOutput' . ucfirst( $outputType )
199 : $outputType;
200 if ( !class_exists( $outputClass ) ) {
201 throw new MWException( "'$outputType' is an invalid output type" );
203 $outputInstance = new $outputClass( $this, $this->params );
204 if ( $outputInstance->canUse() ) {
205 $outputs[] = $outputInstance;
208 return $outputs;
212 * Log the data to some store or even the page output
214 * @since 1.25
216 public function logData() {
217 $request = $this->getContext()->getRequest();
219 $timeElapsed = $request->getElapsedTime();
220 $timeElapsedThreshold = $this->params['threshold'];
221 if ( $timeElapsed <= $timeElapsedThreshold ) {
222 return;
225 $outputs = $this->getOutputs();
226 if ( !$outputs ) {
227 return;
230 $stats = $this->getFunctionStats();
231 foreach ( $outputs as $output ) {
232 $output->log( $stats );
237 * Output current data to the page output if configured to do so
239 * @throws MWException
240 * @since 1.26
242 public function logDataPageOutputOnly() {
243 foreach ( $this->getOutputs() as $output ) {
244 if ( $output instanceof ProfilerOutputText ) {
245 $stats = $this->getFunctionStats();
246 $output->log( $stats );
252 * Get the content type sent out to the client.
253 * Used for profilers that output instead of store data.
254 * @return string
255 * @since 1.25
257 public function getContentType() {
258 foreach ( headers_list() as $header ) {
259 if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
260 return $m[1];
263 return null;
267 * Mark this call as templated or not
269 * @param bool $t
271 public function setTemplated( $t ) {
272 $this->templated = $t;
276 * Was this call as templated or not
278 * @return bool
280 public function getTemplated() {
281 return $this->templated;
285 * Get the aggregated inclusive profiling data for each method
287 * The percent time for each time is based on the current "total" time
288 * used is based on all methods so far. This method can therefore be
289 * called several times in between several profiling calls without the
290 * delays in usage of the profiler skewing the results. A "-total" entry
291 * is always included in the results.
293 * When a call chain involves a method invoked within itself, any
294 * entries for the cyclic invocation should be be demarked with "@".
295 * This makes filtering them out easier and follows the xhprof style.
297 * @return array List of method entries arrays, each having:
298 * - name : method name
299 * - calls : the number of invoking calls
300 * - real : real time elapsed (ms)
301 * - %real : percent real time
302 * - cpu : CPU time elapsed (ms)
303 * - %cpu : percent CPU time
304 * - memory : memory used (bytes)
305 * - %memory : percent memory used
306 * - min_real : min real time in a call (ms)
307 * - max_real : max real time in a call (ms)
308 * @since 1.25
310 abstract public function getFunctionStats();
313 * Returns a profiling output to be stored in debug file
315 * @return string
317 abstract public function getOutput();