22 use Psr\Log\LoggerInterface;
24 use Wikimedia\ScopedCallback;
48 private $allowOutput =
false;
51 private static $instance =
null;
57 if ( isset(
$params[
'profileID'] ) ) {
58 $this->profileID =
$params[
'profileID'];
62 $this->logger = LoggerFactory::getInstance(
'profiler' );
69 final public static function init( array $profilerConf ): void {
79 $isCLI = ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' );
80 $inSample =
$params[
'sampling'] === 1 ?
true : ( mt_rand( 0,
$params[
'sampling'] - 1 ) === 0 );
91 ( $isCLI && !
$params[
'cliEnable'] )
93 $params[
'class'] = ProfilerStub::class;
96 if ( !is_array(
$params[
'output'] ) ) {
108 if ( !self::$instance ) {
109 trigger_error(
'Called Profiler::instance before settings are loaded', E_USER_WARNING );
113 return self::$instance;
120 $this->profileID = $id;
127 if ( $this->profileID ===
false ) {
130 return $this->profileID;
141 $this->context = $context;
176 return $this->trxProfiler;
191 private function getOutputs() {
193 foreach ( $this->params[
'output'] as $outputType ) {
197 $outputClass = strpos( $outputType,
'ProfilerOutput' ) ===
false
198 ?
'ProfilerOutput' . ucfirst( $outputType )
200 if ( !class_exists( $outputClass ) ) {
201 throw new MWException(
"'$outputType' is an invalid output type" );
203 $outputInstance =
new $outputClass( $this, $this->params );
204 if ( $outputInstance->canUse() ) {
205 $outputs[] = $outputInstance;
220 if ( $this->params[
'threshold'] > 0.0 ) {
222 $timeElapsed = microtime(
true ) - $_SERVER[
'REQUEST_TIME_FLOAT'];
223 if ( $timeElapsed <= $this->params[
'threshold'] ) {
229 foreach ( $this->getOutputs() as $output ) {
230 if ( !$output->logsToOutput() ) {
231 $outputs[] = $output;
236 $stats = $this->getFunctionStats();
237 foreach ( $outputs as $output ) {
238 $output->log( $stats );
250 if ( !$this->allowOutput ) {
255 foreach ( $this->getOutputs() as $output ) {
256 if ( $output->logsToOutput() ) {
257 $outputs[] = $output;
262 $stats = $this->getFunctionStats();
263 foreach ( $outputs as $output ) {
264 $output->log( $stats );
279 if ( $this->allowOutput ) {
280 foreach ( headers_list() as
$header ) {
281 if ( preg_match(
'#^content-type: (\w+/\w+);?#i',
$header, $m ) ) {
295 $this->allowOutput =
true;
305 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.
static getCurrentWikiDbDomain()