22use Psr\Log\LoggerInterface;
24use Wikimedia\ScopedCallback;
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;
86 if ( !is_array(
$params[
'output'] ) ) {
92 return self::$instance;
103 if ( self::$instance && !( self::$instance instanceof
ProfilerStub ) ) {
104 throw new MWException(
'Could not replace non-stub profiler instance.' );
106 self::$instance = $profiler;
114 $this->profileID = $id;
121 if ( $this->profileID ===
false ) {
122 return WikiMap::getCurrentWikiDbDomain()->getId();
124 return $this->profileID;
145 if ( $this->context ) {
146 return $this->context;
148 $this->logger->warning( __METHOD__ .
" called before setContext, " .
149 "fallback to RequestContext::getMain()." );
150 return RequestContext::getMain();
184 return $this->trxProfiler;
201 foreach ( $this->params[
'output'] as $outputType ) {
205 $outputClass = strpos( $outputType,
'ProfilerOutput' ) ===
false
206 ?
'ProfilerOutput' . ucfirst( $outputType )
208 if ( !class_exists( $outputClass ) ) {
209 throw new MWException(
"'$outputType' is an invalid output type" );
211 $outputInstance =
new $outputClass( $this, $this->params );
212 if ( $outputInstance->canUse() ) {
213 $outputs[] = $outputInstance;
227 $timeElapsed = $request->getElapsedTime();
228 $timeElapsedThreshold = $this->params[
'threshold'];
229 if ( $timeElapsed <= $timeElapsedThreshold ) {
235 if ( !$output->logsToOutput() ) {
236 $outputs[] = $output;
242 foreach ( $outputs as $output ) {
243 $output->log( $stats );
255 if ( !$this->allowOutput ) {
261 if ( $output->logsToOutput() ) {
262 $outputs[] = $output;
268 foreach ( $outputs as $output ) {
269 $output->log( $stats );
284 if ( $this->allowOutput ) {
285 foreach ( headers_list() as
$header ) {
286 if ( preg_match(
'#^content-type: (\w+/\w+);?#i',
$header, $m ) ) {
300 $this->allowOutput =
true;
310 return $this->allowOutput;
$wgProfiler
Profiler configuration.
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.
static Profiler $instance
setContext( $context)
Sets the context for this Profiler.
IContextSource $context
Current request context.
getOutputs()
Get all usable outputs.
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)
profileOut( $functionname)
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.
profileIn( $functionname)
getContext()
Gets the context for this Profiler.
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.
Interface for objects which can provide a MediaWiki context on request.