MediaWiki REL1_28
Profiler.php
Go to the documentation of this file.
1<?php
24use Wikimedia\ScopedCallback;
25
32abstract class Profiler {
34 protected $profileID = false;
36 protected $templated = false;
38 protected $params = [];
40 protected $context = null;
42 protected $trxProfiler;
44 private static $instance = null;
45
49 public function __construct( array $params ) {
50 if ( isset( $params['profileID'] ) ) {
51 $this->profileID = $params['profileID'];
52 }
53 $this->params = $params;
54 $this->trxProfiler = new TransactionProfiler();
55 }
56
61 final public static function instance() {
62 if ( self::$instance === null ) {
64
65 $params = [
66 'class' => 'ProfilerStub',
67 'sampling' => 1,
68 'threshold' => $wgProfileLimit,
69 'output' => [],
70 ];
71 if ( is_array( $wgProfiler ) ) {
72 $params = array_merge( $params, $wgProfiler );
73 }
74
75 $inSample = mt_rand( 0, $params['sampling'] - 1 ) === 0;
76 if ( PHP_SAPI === 'cli' || !$inSample ) {
77 $params['class'] = 'ProfilerStub';
78 }
79
80 if ( !is_array( $params['output'] ) ) {
81 $params['output'] = [ $params['output'] ];
82 }
83
84 self::$instance = new $params['class']( $params );
85 }
86 return self::$instance;
87 }
88
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
107 public function setProfileID( $id ) {
108 $this->profileID = $id;
109 }
110
114 public function getProfileID() {
115 if ( $this->profileID === false ) {
116 return wfWikiID();
117 } else {
118 return $this->profileID;
119 }
120 }
121
128 public function setContext( $context ) {
129 $this->context = $context;
130 }
131
138 public function getContext() {
139 if ( $this->context ) {
140 return $this->context;
141 } else {
142 wfDebug( __METHOD__ . " called and \$context is null. " .
143 "Return RequestContext::getMain(); for sanity\n" );
145 }
146 }
147
148 // Kept BC for now, remove when possible
149 public function profileIn( $functionname ) {
150 }
151
152 public function profileOut( $functionname ) {
153 }
154
163 abstract public function scopedProfileIn( $section );
164
169 $section = null;
170 }
171
176 public function getTransactionProfiler() {
177 return $this->trxProfiler;
178 }
179
183 abstract public function close();
184
192 private function getOutputs() {
193 $outputs = [];
194 foreach ( $this->params['output'] as $outputType ) {
195 // The class may be specified as either the full class name (for
196 // example, 'ProfilerOutputStats') or (for backward compatibility)
197 // the trailing portion of the class name (for example, 'stats').
198 $outputClass = strpos( $outputType, 'ProfilerOutput' ) === false
199 ? 'ProfilerOutput' . ucfirst( $outputType )
200 : $outputType;
201 if ( !class_exists( $outputClass ) ) {
202 throw new MWException( "'$outputType' is an invalid output type" );
203 }
204 $outputInstance = new $outputClass( $this, $this->params );
205 if ( $outputInstance->canUse() ) {
206 $outputs[] = $outputInstance;
207 }
208 }
209 return $outputs;
210 }
211
217 public function logData() {
218 $request = $this->getContext()->getRequest();
219
220 $timeElapsed = $request->getElapsedTime();
221 $timeElapsedThreshold = $this->params['threshold'];
222 if ( $timeElapsed <= $timeElapsedThreshold ) {
223 return;
224 }
225
226 $outputs = $this->getOutputs();
227 if ( !$outputs ) {
228 return;
229 }
230
231 $stats = $this->getFunctionStats();
232 foreach ( $outputs as $output ) {
233 $output->log( $stats );
234 }
235 }
236
243 public function logDataPageOutputOnly() {
244 foreach ( $this->getOutputs() as $output ) {
245 if ( $output instanceof ProfilerOutputText ) {
246 $stats = $this->getFunctionStats();
247 $output->log( $stats );
248 }
249 }
250 }
251
258 public function getContentType() {
259 foreach ( headers_list() as $header ) {
260 if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
261 return $m[1];
262 }
263 }
264 return null;
265 }
266
272 public function setTemplated( $t ) {
273 $this->templated = $t;
274 }
275
281 public function getTemplated() {
282 return $this->templated;
283 }
284
311 abstract public function getFunctionStats();
312
318 abstract public function getOutput();
319}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$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
Definition WebStart.php:73
getRequest()
Get the WebRequest object.
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:32
static replaceStubInstance(Profiler $profiler)
Replace the current profiler with $profiler if no non-stub profiler is set.
Definition Profiler.php:96
setProfileID( $id)
Definition Profiler.php:107
bool $templated
Whether MediaWiki is in a SkinTemplate output context.
Definition Profiler.php:36
string bool $profileID
Profiler ID for bucketing data.
Definition Profiler.php:34
static Profiler $instance
Definition Profiler.php:44
getTransactionProfiler()
Definition Profiler.php:176
setContext( $context)
Sets the context for this Profiler.
Definition Profiler.php:128
setTemplated( $t)
Mark this call as templated or not.
Definition Profiler.php:272
IContextSource $context
Current request context.
Definition Profiler.php:40
getOutputs()
Get all usable outputs.
Definition Profiler.php:192
close()
Close opened profiling sections.
logData()
Log the data to some store or even the page output.
Definition Profiler.php:217
TransactionProfiler $trxProfiler
Definition Profiler.php:42
__construct(array $params)
Definition Profiler.php:49
profileOut( $functionname)
Definition Profiler.php:152
logDataPageOutputOnly()
Output current data to the page output if configured to do so.
Definition Profiler.php:243
getOutput()
Returns a profiling output to be stored in debug file.
static instance()
Singleton.
Definition Profiler.php:61
getContentType()
Get the content type sent out to the client.
Definition Profiler.php:258
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
getProfileID()
Definition Profiler.php:114
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
getTemplated()
Was this call as templated or not.
Definition Profiler.php:281
scopedProfileOut(SectionProfileCallback &$section=null)
Definition Profiler.php:168
array $params
All of the params passed from $wgProfiler.
Definition Profiler.php:38
profileIn( $functionname)
Definition Profiler.php:149
getContext()
Gets the context for this Profiler.
Definition Profiler.php:138
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.
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition hooks.txt:1102
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2685
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:2901
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface for objects which can provide a MediaWiki context on request.
$header