MediaWiki REL1_31
Profiler.php
Go to the documentation of this file.
1<?php
24use Wikimedia\ScopedCallback;
26
33abstract class Profiler {
35 protected $profileID = false;
37 protected $templated = false;
39 protected $params = [];
41 protected $context = null;
43 protected $trxProfiler;
45 private static $instance = null;
46
50 public function __construct( array $params ) {
51 if ( isset( $params['profileID'] ) ) {
52 $this->profileID = $params['profileID'];
53 }
54 $this->params = $params;
55 $this->trxProfiler = new TransactionProfiler();
56 }
57
62 final public static function instance() {
63 if ( self::$instance === null ) {
65
66 $params = [
67 'class' => ProfilerStub::class,
68 'sampling' => 1,
69 'threshold' => $wgProfileLimit,
70 'output' => [],
71 ];
72 if ( is_array( $wgProfiler ) ) {
73 $params = array_merge( $params, $wgProfiler );
74 }
75
76 $inSample = mt_rand( 0, $params['sampling'] - 1 ) === 0;
77 // wfIsCLI() is not available yet
78 if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' || !$inSample ) {
79 $params['class'] = ProfilerStub::class;
80 }
81
82 if ( !is_array( $params['output'] ) ) {
83 $params['output'] = [ $params['output'] ];
84 }
85
86 self::$instance = new $params['class']( $params );
87 }
88 return self::$instance;
89 }
90
98 final public static function replaceStubInstance( Profiler $profiler ) {
99 if ( self::$instance && !( self::$instance instanceof ProfilerStub ) ) {
100 throw new MWException( 'Could not replace non-stub profiler instance.' );
101 } else {
102 self::$instance = $profiler;
103 }
104 }
105
109 public function setProfileID( $id ) {
110 $this->profileID = $id;
111 }
112
116 public function getProfileID() {
117 if ( $this->profileID === false ) {
118 return wfWikiID();
119 } else {
120 return $this->profileID;
121 }
122 }
123
130 public function setContext( $context ) {
131 $this->context = $context;
132 }
133
140 public function getContext() {
141 if ( $this->context ) {
142 return $this->context;
143 } else {
144 wfDebug( __METHOD__ . " called and \$context is null. " .
145 "Return RequestContext::getMain(); for sanity\n" );
147 }
148 }
149
150 // Kept BC for now, remove when possible
151 public function profileIn( $functionname ) {
152 }
153
154 public function profileOut( $functionname ) {
155 }
156
165 abstract public function scopedProfileIn( $section );
166
171 $section = null;
172 }
173
178 public function getTransactionProfiler() {
179 return $this->trxProfiler;
180 }
181
185 abstract public function close();
186
194 private function getOutputs() {
195 $outputs = [];
196 foreach ( $this->params['output'] as $outputType ) {
197 // The class may be specified as either the full class name (for
198 // example, 'ProfilerOutputStats') or (for backward compatibility)
199 // the trailing portion of the class name (for example, 'stats').
200 $outputClass = strpos( $outputType, 'ProfilerOutput' ) === false
201 ? 'ProfilerOutput' . ucfirst( $outputType )
202 : $outputType;
203 if ( !class_exists( $outputClass ) ) {
204 throw new MWException( "'$outputType' is an invalid output type" );
205 }
206 $outputInstance = new $outputClass( $this, $this->params );
207 if ( $outputInstance->canUse() ) {
208 $outputs[] = $outputInstance;
209 }
210 }
211 return $outputs;
212 }
213
219 public function logData() {
220 $request = $this->getContext()->getRequest();
221
222 $timeElapsed = $request->getElapsedTime();
223 $timeElapsedThreshold = $this->params['threshold'];
224 if ( $timeElapsed <= $timeElapsedThreshold ) {
225 return;
226 }
227
228 $outputs = $this->getOutputs();
229 if ( !$outputs ) {
230 return;
231 }
232
233 $stats = $this->getFunctionStats();
234 foreach ( $outputs as $output ) {
235 $output->log( $stats );
236 }
237 }
238
245 public function logDataPageOutputOnly() {
246 foreach ( $this->getOutputs() as $output ) {
247 if ( $output instanceof ProfilerOutputText ) {
248 $stats = $this->getFunctionStats();
249 $output->log( $stats );
250 }
251 }
252 }
253
260 public function getContentType() {
261 foreach ( headers_list() as $header ) {
262 if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
263 return $m[1];
264 }
265 }
266 return null;
267 }
268
274 public function setTemplated( $t ) {
275 $this->templated = $t;
276 }
277
283 public function getTemplated() {
284 return $this->templated;
285 }
286
313 abstract public function getFunctionStats();
314
320 abstract public function getOutput();
321}
$wgProfileLimit
Only record profiling info for pages that took longer than this.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
getContext()
if(!defined( 'MEDIAWIKI')) $wgProfiler
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition Setup.php:41
MediaWiki exception.
The least sophisticated profiler output class possible, view your source! :)
Stub profiler that does nothing.
Profiler base class that defines the interface and some trivial functionality.
Definition Profiler.php:33
static replaceStubInstance(Profiler $profiler)
Replace the current profiler with $profiler if no non-stub profiler is set.
Definition Profiler.php:98
setProfileID( $id)
Definition Profiler.php:109
bool $templated
Whether MediaWiki is in a SkinTemplate output context.
Definition Profiler.php:37
string bool $profileID
Profiler ID for bucketing data.
Definition Profiler.php:35
static Profiler $instance
Definition Profiler.php:45
getTransactionProfiler()
Definition Profiler.php:178
setContext( $context)
Sets the context for this Profiler.
Definition Profiler.php:130
setTemplated( $t)
Mark this call as templated or not.
Definition Profiler.php:274
getOutputs()
Get all usable outputs.
Definition Profiler.php:194
close()
Close opened profiling sections.
logData()
Log the data to some store or even the page output.
Definition Profiler.php:219
TransactionProfiler $trxProfiler
Definition Profiler.php:43
__construct(array $params)
Definition Profiler.php:50
profileOut( $functionname)
Definition Profiler.php:154
logDataPageOutputOnly()
Output current data to the page output if configured to do so.
Definition Profiler.php:245
getOutput()
Returns a profiling output to be stored in debug file.
static instance()
Singleton.
Definition Profiler.php:62
getContentType()
Get the content type sent out to the client.
Definition Profiler.php:260
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
getProfileID()
Definition Profiler.php:116
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
getTemplated()
Was this call as templated or not.
Definition Profiler.php:283
scopedProfileOut(SectionProfileCallback &$section=null)
Definition Profiler.php:170
profileIn( $functionname)
Definition Profiler.php:151
getContext()
Gets the context for this Profiler.
Definition Profiler.php:140
static getMain()
Get the RequestContext object associated with the main request.
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.
Helper class that detects high-contention DB queries via profiling calls.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2806
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2255
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2811
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section
Definition hooks.txt:3022
Interface for objects which can provide a MediaWiki context on request.
$params
$header