MediaWiki  1.27.1
Profiler.php
Go to the documentation of this file.
1 <?php
31 abstract class Profiler {
33  protected $profileID = false;
35  protected $templated = false;
37  protected $params = [];
39  protected $context = null;
41  protected $trxProfiler;
43  private static $instance = null;
44 
48  public function __construct( array $params ) {
49  if ( isset( $params['profileID'] ) ) {
50  $this->profileID = $params['profileID'];
51  }
52  $this->params = $params;
53  $this->trxProfiler = new TransactionProfiler();
54  }
55 
60  final public static function instance() {
61  if ( self::$instance === null ) {
62  global $wgProfiler, $wgProfileLimit;
63 
64  $params = [
65  'class' => 'ProfilerStub',
66  'sampling' => 1,
67  'threshold' => $wgProfileLimit,
68  'output' => [],
69  ];
70  if ( is_array( $wgProfiler ) ) {
71  $params = array_merge( $params, $wgProfiler );
72  }
73 
74  $inSample = mt_rand( 0, $params['sampling'] - 1 ) === 0;
75  if ( PHP_SAPI === 'cli' || !$inSample ) {
76  $params['class'] = 'ProfilerStub';
77  }
78 
79  if ( !is_array( $params['output'] ) ) {
80  $params['output'] = [ $params['output'] ];
81  }
82 
83  self::$instance = new $params['class']( $params );
84  }
85  return self::$instance;
86  }
87 
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;
100  }
101  }
102 
106  public function setProfileID( $id ) {
107  $this->profileID = $id;
108  }
109 
113  public function getProfileID() {
114  if ( $this->profileID === false ) {
115  return wfWikiID();
116  } else {
117  return $this->profileID;
118  }
119  }
120 
127  public function setContext( $context ) {
128  $this->context = $context;
129  }
130 
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();
144  }
145  }
146 
147  // Kept BC for now, remove when possible
148  public function profileIn( $functionname ) {
149  }
150 
151  public function profileOut( $functionname ) {
152  }
153 
162  abstract public function scopedProfileIn( $section );
163 
167  public function scopedProfileOut( ScopedCallback &$section = null ) {
168  $section = null;
169  }
170 
175  public function getTransactionProfiler() {
176  return $this->trxProfiler;
177  }
178 
182  abstract public function close();
183 
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" );
202  }
203  $outputInstance = new $outputClass( $this, $this->params );
204  if ( $outputInstance->canUse() ) {
205  $outputs[] = $outputInstance;
206  }
207  }
208  return $outputs;
209  }
210 
216  public function logData() {
217  $request = $this->getContext()->getRequest();
218 
219  $timeElapsed = $request->getElapsedTime();
220  $timeElapsedThreshold = $this->params['threshold'];
221  if ( $timeElapsed <= $timeElapsedThreshold ) {
222  return;
223  }
224 
225  $outputs = $this->getOutputs();
226  if ( !$outputs ) {
227  return;
228  }
229 
230  $stats = $this->getFunctionStats();
231  foreach ( $outputs as $output ) {
232  $output->log( $stats );
233  }
234  }
235 
242  public function logDataPageOutputOnly() {
243  foreach ( $this->getOutputs() as $output ) {
244  if ( $output instanceof ProfilerOutputText ) {
245  $stats = $this->getFunctionStats();
246  $output->log( $stats );
247  }
248  }
249  }
250 
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];
261  }
262  }
263  return null;
264  }
265 
271  public function setTemplated( $t ) {
272  $this->templated = $t;
273  }
274 
280  public function getTemplated() {
281  return $this->templated;
282  }
283 
310  abstract public function getFunctionStats();
311 
317  abstract public function getOutput();
318 }
getContext()
Gets the context for this Profiler.
Definition: Profiler.php:137
string bool $profileID
Profiler ID for bucketing data.
Definition: Profiler.php:33
the array() calling protocol came about after MediaWiki 1.4rc1.
static Profiler $instance
Definition: Profiler.php:43
__construct(array $params)
Definition: Profiler.php:48
scopedProfileOut(ScopedCallback &$section=null)
Definition: Profiler.php:167
getOutput()
Returns a profiling output to be stored in debug file.
IContextSource $context
Current request context.
Definition: Profiler.php:39
static instance()
Singleton.
Definition: Profiler.php:60
$wgProfiler
Definition: WebStart.php:73
getProfileID()
Definition: Profiler.php:113
array $params
All of the params passed from $wgProfiler.
Definition: Profiler.php:37
Stub profiler that does nothing.
setTemplated($t)
Mark this call as templated or not.
Definition: Profiler.php:271
setContext($context)
Sets the context for this Profiler.
Definition: Profiler.php:127
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
The least sophisticated profiler output class possible, view your source! :)
profileOut($functionname)
Definition: Profiler.php:151
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
static replaceStubInstance(Profiler $profiler)
Replace the current profiler with $profiler if no non-stub profiler is set.
Definition: Profiler.php:95
bool $templated
Whether MediaWiki is in a SkinTemplate output context.
Definition: Profiler.php:35
Class for asserting that a callback happens when an dummy object leaves scope.
static getMain()
Static methods.
Helper class that detects high-contention DB queries via profiling calls.
getOutputs()
Get all usable outputs.
Definition: Profiler.php:191
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
close()
Close opened profiling sections.
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:2715
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:1004
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:280
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2418
logData()
Log the data to some store or even the page output.
Definition: Profiler.php:216
profileIn($functionname)
Definition: Profiler.php:148
logDataPageOutputOnly()
Output current data to the page output if configured to do so.
Definition: Profiler.php:242
setProfileID($id)
Definition: Profiler.php:106
scopedProfileIn($section)
Mark the start of a custom profiling frame (e.g.
getTransactionProfiler()
Definition: Profiler.php:175
TransactionProfiler $trxProfiler
Definition: Profiler.php:41
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
getContentType()
Get the content type sent out to the client.
Definition: Profiler.php:257
Profiler base class that defines the interface and some trivial functionality.
Definition: Profiler.php:31