23 use Psr\Log\LoggerInterface;
25 use Wikimedia\ScopedCallback;
49 private $allowOutput =
false;
52 private static $instance =
null;
58 if ( isset(
$params[
'profileID'] ) ) {
59 $this->profileID =
$params[
'profileID'];
63 $this->logger = LoggerFactory::getInstance(
'profiler' );
70 final public static function init( array $profilerConf ): void {
80 $isCLI = ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' );
81 $inSample =
$params[
'sampling'] === 1 || mt_rand( 0,
$params[
'sampling'] - 1 ) === 0;
92 ( $isCLI && !
$params[
'cliEnable'] )
94 $params[
'class'] = ProfilerStub::class;
97 if ( !is_array(
$params[
'output'] ) ) {
109 if ( !self::$instance ) {
110 trigger_error(
'Called Profiler::instance before settings are loaded', E_USER_WARNING );
114 return self::$instance;
121 $this->profileID = $id;
128 if ( $this->profileID ===
false ) {
129 return WikiMap::getCurrentWikiDbDomain()->getId();
131 return $this->profileID;
142 $this->context = $context;
177 return $this->trxProfiler;
192 private function getOutputs() {
194 foreach ( $this->params[
'output'] as $outputType ) {
198 $outputClass = strpos( $outputType,
'ProfilerOutput' ) ===
false
199 ?
'ProfilerOutput' . ucfirst( $outputType )
201 if ( !class_exists( $outputClass ) ) {
202 throw new MWException(
"'$outputType' is an invalid output type" );
204 $outputInstance =
new $outputClass( $this, $this->params );
205 if ( $outputInstance->canUse() ) {
206 $outputs[] = $outputInstance;
221 if ( $this->params[
'threshold'] > 0.0 ) {
223 $timeElapsed = microtime(
true ) - $_SERVER[
'REQUEST_TIME_FLOAT'];
224 if ( $timeElapsed <= $this->params[
'threshold'] ) {
230 foreach ( $this->getOutputs() as $output ) {
231 if ( !$output->logsToOutput() ) {
232 $outputs[] = $output;
237 $stats = $this->getFunctionStats();
238 foreach ( $outputs as $output ) {
239 $output->log( $stats );
251 if ( !$this->allowOutput ) {
256 foreach ( $this->getOutputs() as $output ) {
257 if ( $output->logsToOutput() ) {
258 $outputs[] = $output;
263 $stats = $this->getFunctionStats();
264 foreach ( $outputs as $output ) {
265 $output->log( $stats );
280 if ( $this->allowOutput ) {
281 foreach ( headers_list() as
$header ) {
282 if ( preg_match(
'#^content-type: (\w+/\w+);?#i',
$header, $m ) ) {
296 $this->allowOutput =
true;
306 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.
IContextSource $context
Current request context.
close()
Close opened profiling sections.
logData()
Log data to all the applicable backing stores.
TransactionProfiler $trxProfiler
__construct(array $params)
string false $profileID
Profiler ID for bucketing data.
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 init(array $profilerConf)
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.
static getMain()
Get the RequestContext object associated with the main request.
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.