21use Wikimedia\ScopedCallback;
52 if ( isset( $params[
'profileID'] ) ) {
53 $this->profileID = $params[
'profileID'];
55 $this->params = $params;
64 if ( self::$instance ===
null ) {
68 'class' => ProfilerStub::class,
74 $inSample = mt_rand( 0,
$params[
'sampling'] - 1 ) === 0;
76 if ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' || !$inSample ) {
77 $params[
'class'] = ProfilerStub::class;
80 if ( !is_array(
$params[
'output'] ) ) {
86 return self::$instance;
97 if ( self::$instance && !( self::$instance instanceof
ProfilerStub ) ) {
98 throw new MWException(
'Could not replace non-stub profiler instance.' );
100 self::$instance = $profiler;
108 $this->profileID = $id;
115 if ( $this->profileID ===
false ) {
116 return WikiMap::getCurrentWikiDbDomain()->getId();
118 return $this->profileID;
139 if ( $this->context ) {
140 return $this->context;
142 wfDebug( __METHOD__ .
" called and \$context is null. " .
143 "Return RequestContext::getMain(); for sanity" );
144 return RequestContext::getMain();
178 return $this->trxProfiler;
195 foreach ( $this->params[
'output'] as $outputType ) {
199 $outputClass = strpos( $outputType,
'ProfilerOutput' ) ===
false
200 ?
'ProfilerOutput' . ucfirst( $outputType )
202 if ( !class_exists( $outputClass ) ) {
203 throw new MWException(
"'$outputType' is an invalid output type" );
205 $outputInstance =
new $outputClass( $this, $this->params );
206 if ( $outputInstance->canUse() ) {
207 $outputs[] = $outputInstance;
221 $timeElapsed = $request->getElapsedTime();
222 $timeElapsedThreshold = $this->params[
'threshold'];
223 if ( $timeElapsed <= $timeElapsedThreshold ) {
229 if ( !$output->logsToOutput() ) {
230 $outputs[] = $output;
236 foreach ( $outputs as $output ) {
237 $output->log( $stats );
249 if ( !$this->allowOutput ) {
255 if ( $output->logsToOutput() ) {
256 $outputs[] = $output;
262 foreach ( $outputs as $output ) {
263 $output->log( $stats );
278 if ( $this->allowOutput ) {
279 foreach ( headers_list() as
$header ) {
280 if ( preg_match(
'#^content-type: (\w+/\w+);?#i',
$header, $m ) ) {
296 $this->allowOutput = (
$t ===
true );
316 $this->allowOutput =
true;
326 return $this->allowOutput;
$wgProfiler
Profiler configuration.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
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.
setTemplated( $t)
Mark this call as templated or not.
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.
getTemplated()
Was this call as templated or not.
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.