22use Psr\Log\LoggerInterface;
24use Wikimedia\ScopedCallback;
38 protected $profileID =
false;
40 protected $params = [];
42 protected $context =
null;
48 private $allowOutput =
false;
51 private static $instance =
null;
57 if ( isset( $params[
'profileID'] ) ) {
58 $this->profileID = $params[
'profileID'];
60 $this->params = $params;
62 $this->logger = LoggerFactory::getInstance(
'profiler' );
70 if ( self::$instance ===
null ) {
74 'class' => ProfilerStub::class,
80 $inSample = mt_rand( 0, $params[
'sampling'] - 1 ) === 0;
82 if ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' || !$inSample ) {
83 $params[
'class'] = ProfilerStub::class;
89 if ( !is_array( $params[
'output'] ) ) {
90 $params[
'output'] = [ $params[
'output'] ];
93 self::$instance =
new $params[
'class']( $params );
95 return self::$instance;
106 if ( self::$instance && !( self::$instance instanceof
ProfilerStub ) ) {
107 throw new MWException(
'Could not replace non-stub profiler instance.' );
109 self::$instance = $profiler;
117 $this->profileID = $id;
124 if ( $this->profileID ===
false ) {
125 return WikiMap::getCurrentWikiDbDomain()->getId();
127 return $this->profileID;
138 $this->context = $context;
148 return $this->context ?? RequestContext::getMain();
173 return $this->trxProfiler;
188 private function getOutputs() {
190 foreach ( $this->params[
'output'] as $outputType ) {
194 $outputClass = strpos( $outputType,
'ProfilerOutput' ) ===
false
195 ?
'ProfilerOutput' . ucfirst( $outputType )
197 if ( !class_exists( $outputClass ) ) {
198 throw new MWException(
"'$outputType' is an invalid output type" );
200 $outputInstance =
new $outputClass( $this, $this->params );
201 if ( $outputInstance->canUse() ) {
202 $outputs[] = $outputInstance;
214 if ( $this->params[
'threshold'] > 0.0 ) {
216 $timeElapsed = microtime(
true ) - $_SERVER[
'REQUEST_TIME_FLOAT'];
217 if ( $timeElapsed <= $this->params[
'threshold'] ) {
223 foreach ( $this->getOutputs() as $output ) {
224 if ( !$output->logsToOutput() ) {
225 $outputs[] = $output;
231 foreach ( $outputs as $output ) {
232 $output->log( $stats );
244 if ( !$this->allowOutput ) {
249 foreach ( $this->getOutputs() as $output ) {
250 if ( $output->logsToOutput() ) {
251 $outputs[] = $output;
257 foreach ( $outputs as $output ) {
258 $output->log( $stats );
273 if ( $this->allowOutput ) {
274 foreach ( headers_list() as
$header ) {
275 if ( preg_match(
'#^content-type: (\w+/\w+);?#i',
$header, $m ) ) {
289 $this->allowOutput =
true;
299 return $this->allowOutput;
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Stub profiler that does nothing.
Profiler base class that defines the interface and some shared functionality.
setAllowOutput()
Enable appending profiles to standard output.
static replaceStubInstance(Profiler $profiler)
Replace the current profiler with $profiler if no non-stub profiler is set.
string bool $profileID
Profiler ID for bucketing data.
IContextSource $context
Current request context.
close()
Close opened profiling sections.
logData()
Log the data to the backing store for all ProfilerOutput instances that have one.
TransactionProfiler $trxProfiler
__construct(array $params)
logDataPageOutputOnly()
Log the data to the script/request output for all ProfilerOutput instances that do so.
getOutput()
Returns a profiling output to be stored in debug file.
static instance()
Singleton.
getContentType()
Get the Content-Type for deciding how to format appended profile output.
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
getAllowOutput()
Whether appending profiles is allowed.
scopedProfileOut(SectionProfileCallback &$section=null)
array $params
All of the params passed from $wgProfiler.
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.
$wgProfiler
Config variable stub for the Profiler setting, for use by phpdoc and IDEs.
Interface for objects which can provide a MediaWiki context on request.