MediaWiki  master
Profiler.php
Go to the documentation of this file.
1 <?php
22 use Psr\Log\LoggerInterface;
24 use Wikimedia\ScopedCallback;
25 
36 abstract class Profiler {
38  protected $profileID = false;
40  protected $params = [];
42  protected $context = null;
44  protected $trxProfiler;
46  protected $logger;
48  private $allowOutput = false;
49 
51  private static $instance = null;
52 
56  public function __construct( array $params ) {
57  if ( isset( $params['profileID'] ) ) {
58  $this->profileID = $params['profileID'];
59  }
60  $this->params = $params;
61  $this->trxProfiler = new TransactionProfiler();
62  $this->logger = LoggerFactory::getInstance( 'profiler' );
63  }
64 
69  final public static function instance() {
70  if ( self::$instance === null ) {
71  global $wgProfiler;
72 
73  $params = $wgProfiler + [
74  'class' => ProfilerStub::class,
75  'sampling' => 1,
76  'threshold' => 0.0,
77  'output' => [],
78  ];
79 
80  $inSample = mt_rand( 0, $params['sampling'] - 1 ) === 0;
81  // wfIsCLI() is not available yet
82  if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' || !$inSample ) {
83  $params['class'] = ProfilerStub::class;
84  }
85 
86  if ( !is_array( $params['output'] ) ) {
87  $params['output'] = [ $params['output'] ];
88  }
89 
90  self::$instance = new $params['class']( $params );
91  }
92  return self::$instance;
93  }
94 
102  final public static function replaceStubInstance( Profiler $profiler ) {
103  if ( self::$instance && !( self::$instance instanceof ProfilerStub ) ) {
104  throw new MWException( 'Could not replace non-stub profiler instance.' );
105  } else {
106  self::$instance = $profiler;
107  }
108  }
109 
113  public function setProfileID( $id ) {
114  $this->profileID = $id;
115  }
116 
120  public function getProfileID() {
121  if ( $this->profileID === false ) {
122  return WikiMap::getCurrentWikiDbDomain()->getId();
123  } else {
124  return $this->profileID;
125  }
126  }
127 
133  public function setContext( $context ) {
134  wfDeprecated( __METHOD__, '1.38' );
135  $this->context = $context;
136  }
137 
143  public function getContext() {
144  wfDeprecated( __METHOD__, '1.38' );
145  return $this->context ?? RequestContext::getMain();
146  }
147 
148  public function profileIn( $functionname ) {
149  wfDeprecated( __METHOD__, '1.33' );
150  }
151 
152  public function profileOut( $functionname ) {
153  wfDeprecated( __METHOD__, '1.33' );
154  }
155 
164  abstract public function scopedProfileIn( $section );
165 
169  public function scopedProfileOut( SectionProfileCallback &$section = null ) {
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  if ( $this->params['threshold'] > 0.0 ) {
220  // Note, this is also valid for CLI processes.
221  $timeElapsed = microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT'];
222  if ( $timeElapsed <= $this->params['threshold'] ) {
223  return;
224  }
225  }
226 
227  $outputs = [];
228  foreach ( $this->getOutputs() as $output ) {
229  if ( !$output->logsToOutput() ) {
230  $outputs[] = $output;
231  }
232  }
233 
234  if ( $outputs ) {
235  $stats = $this->getFunctionStats();
236  foreach ( $outputs as $output ) {
237  $output->log( $stats );
238  }
239  }
240  }
241 
248  public function logDataPageOutputOnly() {
249  if ( !$this->allowOutput ) {
250  return;
251  }
252 
253  $outputs = [];
254  foreach ( $this->getOutputs() as $output ) {
255  if ( $output->logsToOutput() ) {
256  $outputs[] = $output;
257  }
258  }
259 
260  if ( $outputs ) {
261  $stats = $this->getFunctionStats();
262  foreach ( $outputs as $output ) {
263  $output->log( $stats );
264  }
265  }
266  }
267 
277  public function getContentType() {
278  if ( $this->allowOutput ) {
279  foreach ( headers_list() as $header ) {
280  if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
281  return $m[1];
282  }
283  }
284  }
285  return null;
286  }
287 
293  public function setAllowOutput() {
294  $this->allowOutput = true;
295  }
296 
303  public function getAllowOutput() {
304  return $this->allowOutput;
305  }
306 
333  abstract public function getFunctionStats();
334 
340  abstract public function getOutput();
341 }
Profiler\getTransactionProfiler
getTransactionProfiler()
Definition: Profiler.php:177
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
Profiler\replaceStubInstance
static replaceStubInstance(Profiler $profiler)
Replace the current profiler with $profiler if no non-stub profiler is set.
Definition: Profiler.php:102
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:69
Profiler\getOutput
getOutput()
Returns a profiling output to be stored in debug file.
Profiler\getContentType
getContentType()
Get the Content-Type for deciding how to format appended profile output.
Definition: Profiler.php:277
Profiler\$instance
static Profiler $instance
Definition: Profiler.php:51
ProfilerStub
Stub profiler that does nothing.
Definition: ProfilerStub.php:26
Profiler\$profileID
string bool $profileID
Profiler ID for bucketing data.
Definition: Profiler.php:38
Profiler\$context
IContextSource $context
Current request context.
Definition: Profiler.php:42
Profiler\scopedProfileIn
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
Profiler\getOutputs
getOutputs()
Get all usable outputs.
Definition: Profiler.php:193
Profiler\$trxProfiler
TransactionProfiler $trxProfiler
Definition: Profiler.php:44
SectionProfileCallback
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.
Definition: SectionProfileCallback.php:28
Profiler\profileOut
profileOut( $functionname)
Definition: Profiler.php:152
Profiler\$logger
LoggerInterface $logger
Definition: Profiler.php:46
Profiler\$allowOutput
bool $allowOutput
Definition: Profiler.php:48
MWException
MediaWiki exception.
Definition: MWException.php:29
$wgProfiler
$wgProfiler
Profiler configuration.
Definition: DefaultSettings.php:7523
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:989
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
Profiler\profileIn
profileIn( $functionname)
Definition: Profiler.php:148
Profiler\close
close()
Close opened profiling sections.
Profiler\setAllowOutput
setAllowOutput()
Enable appending profiles to standard output.
Definition: Profiler.php:293
Profiler
Profiler base class that defines the interface and some shared functionality.
Definition: Profiler.php:36
Profiler\__construct
__construct(array $params)
Definition: Profiler.php:56
Profiler\setContext
setContext( $context)
Definition: Profiler.php:133
$header
$header
Definition: updateCredits.php:37
Profiler\logData
logData()
Log the data to the backing store for all ProfilerOutput instances that have one.
Definition: Profiler.php:218
Profiler\setProfileID
setProfileID( $id)
Definition: Profiler.php:113
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:484
Profiler\getContext
getContext()
Definition: Profiler.php:143
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
Profiler\getFunctionStats
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
Profiler\logDataPageOutputOnly
logDataPageOutputOnly()
Log the data to the script/request output for all ProfilerOutput instances that do so.
Definition: Profiler.php:248
Profiler\getAllowOutput
getAllowOutput()
Whether appending profiles is allowed.
Definition: Profiler.php:303
Profiler\$params
array $params
All of the params passed from $wgProfiler.
Definition: Profiler.php:40
Wikimedia\Rdbms\TransactionProfiler
Detect high-contention DB queries via profiling calls.
Definition: TransactionProfiler.php:40
Profiler\getProfileID
getProfileID()
Definition: Profiler.php:120
Profiler\scopedProfileOut
scopedProfileOut(SectionProfileCallback &$section=null)
Definition: Profiler.php:169