MediaWiki REL1_30
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',
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 if ( PHP_SAPI === 'cli' || !$inSample ) {
78 $params['class'] = 'ProfilerStub';
79 }
80
81 if ( !is_array( $params['output'] ) ) {
82 $params['output'] = [ $params['output'] ];
83 }
84
85 self::$instance = new $params['class']( $params );
86 }
87 return self::$instance;
88 }
89
97 final public static function replaceStubInstance( Profiler $profiler ) {
98 if ( self::$instance && !( self::$instance instanceof ProfilerStub ) ) {
99 throw new MWException( 'Could not replace non-stub profiler instance.' );
100 } else {
101 self::$instance = $profiler;
102 }
103 }
104
108 public function setProfileID( $id ) {
109 $this->profileID = $id;
110 }
111
115 public function getProfileID() {
116 if ( $this->profileID === false ) {
117 return wfWikiID();
118 } else {
119 return $this->profileID;
120 }
121 }
122
129 public function setContext( $context ) {
130 $this->context = $context;
131 }
132
139 public function getContext() {
140 if ( $this->context ) {
141 return $this->context;
142 } else {
143 wfDebug( __METHOD__ . " called and \$context is null. " .
144 "Return RequestContext::getMain(); for sanity\n" );
146 }
147 }
148
149 // Kept BC for now, remove when possible
150 public function profileIn( $functionname ) {
151 }
152
153 public function profileOut( $functionname ) {
154 }
155
164 abstract public function scopedProfileIn( $section );
165
170 $section = null;
171 }
172
177 public function getTransactionProfiler() {
178 return $this->trxProfiler;
179 }
180
184 abstract public function close();
185
193 private function getOutputs() {
194 $outputs = [];
195 foreach ( $this->params['output'] as $outputType ) {
196 // The class may be specified as either the full class name (for
197 // example, 'ProfilerOutputStats') or (for backward compatibility)
198 // the trailing portion of the class name (for example, 'stats').
199 $outputClass = strpos( $outputType, 'ProfilerOutput' ) === false
200 ? 'ProfilerOutput' . ucfirst( $outputType )
201 : $outputType;
202 if ( !class_exists( $outputClass ) ) {
203 throw new MWException( "'$outputType' is an invalid output type" );
204 }
205 $outputInstance = new $outputClass( $this, $this->params );
206 if ( $outputInstance->canUse() ) {
207 $outputs[] = $outputInstance;
208 }
209 }
210 return $outputs;
211 }
212
218 public function logData() {
219 $request = $this->getContext()->getRequest();
220
221 $timeElapsed = $request->getElapsedTime();
222 $timeElapsedThreshold = $this->params['threshold'];
223 if ( $timeElapsed <= $timeElapsedThreshold ) {
224 return;
225 }
226
227 $outputs = $this->getOutputs();
228 if ( !$outputs ) {
229 return;
230 }
231
232 $stats = $this->getFunctionStats();
233 foreach ( $outputs as $output ) {
234 $output->log( $stats );
235 }
236 }
237
244 public function logDataPageOutputOnly() {
245 foreach ( $this->getOutputs() as $output ) {
246 if ( $output instanceof ProfilerOutputText ) {
247 $stats = $this->getFunctionStats();
248 $output->log( $stats );
249 }
250 }
251 }
252
259 public function getContentType() {
260 foreach ( headers_list() as $header ) {
261 if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
262 return $m[1];
263 }
264 }
265 return null;
266 }
267
273 public function setTemplated( $t ) {
274 $this->templated = $t;
275 }
276
282 public function getTemplated() {
283 return $this->templated;
284 }
285
312 abstract public function getFunctionStats();
313
319 abstract public function getOutput();
320}
$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.
$wgProfiler
getContext()
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:97
setProfileID( $id)
Definition Profiler.php:108
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:177
setContext( $context)
Sets the context for this Profiler.
Definition Profiler.php:129
setTemplated( $t)
Mark this call as templated or not.
Definition Profiler.php:273
getOutputs()
Get all usable outputs.
Definition Profiler.php:193
close()
Close opened profiling sections.
logData()
Log the data to some store or even the page output.
Definition Profiler.php:218
TransactionProfiler $trxProfiler
Definition Profiler.php:43
__construct(array $params)
Definition Profiler.php:50
profileOut( $functionname)
Definition Profiler.php:153
logDataPageOutputOnly()
Output current data to the page output if configured to do so.
Definition Profiler.php:244
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:259
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
getProfileID()
Definition Profiler.php:115
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
getTemplated()
Was this call as templated or not.
Definition Profiler.php:282
scopedProfileOut(SectionProfileCallback &$section=null)
Definition Profiler.php:169
profileIn( $functionname)
Definition Profiler.php:150
getContext()
Gets the context for this Profiler.
Definition Profiler.php:139
static getMain()
Static methods.
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:2775
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:2225
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:2780
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:2990
Interface for objects which can provide a MediaWiki context on request.
$params
$header