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 
156  abstract public function scopedProfileIn( $section );
157 
161  public function scopedProfileOut( SectionProfileCallback &$section = null ) {
162  $section = null;
163  }
164 
169  public function getTransactionProfiler() {
170  return $this->trxProfiler;
171  }
172 
176  abstract public function close();
177 
185  private function getOutputs() {
186  $outputs = [];
187  foreach ( $this->params['output'] as $outputType ) {
188  // The class may be specified as either the full class name (for
189  // example, 'ProfilerOutputStats') or (for backward compatibility)
190  // the trailing portion of the class name (for example, 'stats').
191  $outputClass = strpos( $outputType, 'ProfilerOutput' ) === false
192  ? 'ProfilerOutput' . ucfirst( $outputType )
193  : $outputType;
194  if ( !class_exists( $outputClass ) ) {
195  throw new MWException( "'$outputType' is an invalid output type" );
196  }
197  $outputInstance = new $outputClass( $this, $this->params );
198  if ( $outputInstance->canUse() ) {
199  $outputs[] = $outputInstance;
200  }
201  }
202  return $outputs;
203  }
204 
210  public function logData() {
211  if ( $this->params['threshold'] > 0.0 ) {
212  // Note, this is also valid for CLI processes.
213  $timeElapsed = microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT'];
214  if ( $timeElapsed <= $this->params['threshold'] ) {
215  return;
216  }
217  }
218 
219  $outputs = [];
220  foreach ( $this->getOutputs() as $output ) {
221  if ( !$output->logsToOutput() ) {
222  $outputs[] = $output;
223  }
224  }
225 
226  if ( $outputs ) {
227  $stats = $this->getFunctionStats();
228  foreach ( $outputs as $output ) {
229  $output->log( $stats );
230  }
231  }
232  }
233 
240  public function logDataPageOutputOnly() {
241  if ( !$this->allowOutput ) {
242  return;
243  }
244 
245  $outputs = [];
246  foreach ( $this->getOutputs() as $output ) {
247  if ( $output->logsToOutput() ) {
248  $outputs[] = $output;
249  }
250  }
251 
252  if ( $outputs ) {
253  $stats = $this->getFunctionStats();
254  foreach ( $outputs as $output ) {
255  $output->log( $stats );
256  }
257  }
258  }
259 
269  public function getContentType() {
270  if ( $this->allowOutput ) {
271  foreach ( headers_list() as $header ) {
272  if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
273  return $m[1];
274  }
275  }
276  }
277  return null;
278  }
279 
285  public function setAllowOutput() {
286  $this->allowOutput = true;
287  }
288 
295  public function getAllowOutput() {
296  return $this->allowOutput;
297  }
298 
325  abstract public function getFunctionStats();
326 
332  abstract public function getOutput();
333 }
Profiler\getTransactionProfiler
getTransactionProfiler()
Definition: Profiler.php:169
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:269
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:185
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\$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:7485
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
Profiler\close
close()
Close opened profiling sections.
Profiler\setAllowOutput
setAllowOutput()
Enable appending profiles to standard output.
Definition: Profiler.php:285
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:210
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:240
Profiler\getAllowOutput
getAllowOutput()
Whether appending profiles is allowed.
Definition: Profiler.php:295
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:161