MediaWiki  master
Profiler.php
Go to the documentation of this file.
1 <?php
26 
33 abstract 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 WikiMap::getCurrentWikiDbDomain()->getId();
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" );
146  return RequestContext::getMain();
147  }
148  }
149 
150  public function profileIn( $functionname ) {
151  wfDeprecated( __METHOD__, '1.33' );
152  }
153 
154  public function profileOut( $functionname ) {
155  wfDeprecated( __METHOD__, '1.33' );
156  }
157 
166  abstract public function scopedProfileIn( $section );
167 
172  $section = null;
173  }
174 
179  public function getTransactionProfiler() {
180  return $this->trxProfiler;
181  }
182 
186  abstract public function close();
187 
195  private function getOutputs() {
196  $outputs = [];
197  foreach ( $this->params['output'] as $outputType ) {
198  // The class may be specified as either the full class name (for
199  // example, 'ProfilerOutputStats') or (for backward compatibility)
200  // the trailing portion of the class name (for example, 'stats').
201  $outputClass = strpos( $outputType, 'ProfilerOutput' ) === false
202  ? 'ProfilerOutput' . ucfirst( $outputType )
203  : $outputType;
204  if ( !class_exists( $outputClass ) ) {
205  throw new MWException( "'$outputType' is an invalid output type" );
206  }
207  $outputInstance = new $outputClass( $this, $this->params );
208  if ( $outputInstance->canUse() ) {
209  $outputs[] = $outputInstance;
210  }
211  }
212  return $outputs;
213  }
214 
220  public function logData() {
221  $request = $this->getContext()->getRequest();
222 
223  $timeElapsed = $request->getElapsedTime();
224  $timeElapsedThreshold = $this->params['threshold'];
225  if ( $timeElapsed <= $timeElapsedThreshold ) {
226  return;
227  }
228 
229  $outputs = [];
230  foreach ( $this->getOutputs() as $output ) {
231  if ( !$output->logsToOutput() ) {
232  $outputs[] = $output;
233  }
234  }
235 
236  if ( $outputs ) {
237  $stats = $this->getFunctionStats();
238  foreach ( $outputs as $output ) {
239  $output->log( $stats );
240  }
241  }
242  }
243 
250  public function logDataPageOutputOnly() {
251  $outputs = [];
252  foreach ( $this->getOutputs() as $output ) {
253  if ( $output->logsToOutput() ) {
254  $outputs[] = $output;
255  }
256  }
257 
258  if ( $outputs ) {
259  $stats = $this->getFunctionStats();
260  foreach ( $outputs as $output ) {
261  $output->log( $stats );
262  }
263  }
264  }
265 
272  public function getContentType() {
273  foreach ( headers_list() as $header ) {
274  if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
275  return $m[1];
276  }
277  }
278  return null;
279  }
280 
286  public function setTemplated( $t ) {
287  $this->templated = $t;
288  }
289 
295  public function getTemplated() {
296  return $this->templated;
297  }
298 
325  abstract public function getFunctionStats();
326 
332  abstract public function getOutput();
333 }
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
Helper class that detects high-contention DB queries via profiling calls.
getContext()
Gets the context for this Profiler.
Definition: Profiler.php:140
string bool $profileID
Profiler ID for bucketing data.
Definition: Profiler.php:35
static Profiler $instance
Definition: Profiler.php:45
__construct(array $params)
Definition: Profiler.php:50
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
getOutput()
Returns a profiling output to be stored in debug file.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
scopedProfileOut(SectionProfileCallback &$section=null)
Definition: Profiler.php:171
IContextSource $context
Current request context.
Definition: Profiler.php:41
static instance()
Singleton.
Definition: Profiler.php:62
getProfileID()
Definition: Profiler.php:116
array $params
All of the params passed from $wgProfiler.
Definition: Profiler.php:39
Stub profiler that does nothing.
profileOut( $functionname)
Definition: Profiler.php:154
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition: hooks.txt:2217
static replaceStubInstance(Profiler $profiler)
Replace the current profiler with $profiler if no non-stub profiler is set.
Definition: Profiler.php:98
bool $templated
Whether MediaWiki is in a SkinTemplate output context.
Definition: Profiler.php:37
static getMain()
Get the RequestContext object associated with the main request.
getOutputs()
Get all usable outputs.
Definition: Profiler.php:195
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
setContext( $context)
Sets the context for this Profiler.
Definition: Profiler.php:130
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
close()
Close opened profiling sections.
$header
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
Definition: distributors.txt:9
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:3050
setProfileID( $id)
Definition: Profiler.php:109
profileIn( $functionname)
Definition: Profiler.php:150
static getCurrentWikiDbDomain()
Definition: WikiMap.php:292
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:35
getTemplated()
Was this call as templated or not.
Definition: Profiler.php:295
logData()
Log the data to the backing store for all ProfilerOutput instances that have one. ...
Definition: Profiler.php:220
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.
$wgProfileLimit
Only record profiling info for pages that took longer than this.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
logDataPageOutputOnly()
Log the data to the script/request output for all ProfilerOutput instances that do so...
Definition: Profiler.php:250
getTransactionProfiler()
Definition: Profiler.php:179
$wgProfiler
Profiler configuration.
TransactionProfiler $trxProfiler
Definition: Profiler.php:43
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:2633
setTemplated( $t)
Mark this call as templated or not.
Definition: Profiler.php:286
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
getContentType()
Get the content type sent out to the client.
Definition: Profiler.php:272