23use Psr\Log\LoggerInterface;
25use Wikimedia\ScopedCallback;
47 private $allowOutput =
false;
50 private static $instance =
null;
56 if ( isset(
$params[
'profileID'] ) ) {
57 $this->profileID =
$params[
'profileID'];
61 $this->logger = LoggerFactory::getInstance(
'profiler' );
68 final public static function init( array $profilerConf ): void {
78 $isCLI = ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' );
79 $inSample =
$params[
'sampling'] === 1 || mt_rand( 0,
$params[
'sampling'] - 1 ) === 0;
90 ( $isCLI && !
$params[
'cliEnable'] )
92 $params[
'class'] = ProfilerStub::class;
95 if ( !is_array(
$params[
'output'] ) ) {
106 if ( !self::$instance ) {
107 trigger_error(
'Called Profiler::instance before settings are loaded', E_USER_WARNING );
111 return self::$instance;
120 $this->profileID = $id;
127 if ( $this->profileID ===
false ) {
128 return WikiMap::getCurrentWikiDbDomain()->getId();
130 return $this->profileID;
156 return $this->trxProfiler;
171 private function getOutputs() {
173 foreach ( $this->params[
'output'] as $outputType ) {
177 $outputClass = strpos( $outputType,
'ProfilerOutput' ) ===
false
178 ?
'ProfilerOutput' . ucfirst( $outputType )
180 if ( !class_exists( $outputClass ) ) {
181 throw new MWException(
"'$outputType' is an invalid output type" );
183 $outputInstance =
new $outputClass( $this, $this->params );
184 if ( $outputInstance->canUse() ) {
185 $outputs[] = $outputInstance;
200 if ( $this->params[
'threshold'] > 0.0 ) {
202 $timeElapsed = microtime(
true ) - $_SERVER[
'REQUEST_TIME_FLOAT'];
203 if ( $timeElapsed <= $this->params[
'threshold'] ) {
209 foreach ( $this->getOutputs() as $output ) {
210 if ( !$output->logsToOutput() ) {
211 $outputs[] = $output;
216 $stats = $this->getFunctionStats();
217 foreach ( $outputs as $output ) {
218 $output->log( $stats );
230 if ( !$this->allowOutput ) {
235 foreach ( $this->getOutputs() as $output ) {
236 if ( $output->logsToOutput() ) {
237 $outputs[] = $output;
242 $stats = $this->getFunctionStats();
243 foreach ( $outputs as $output ) {
244 $output->log( $stats );
259 if ( $this->allowOutput ) {
260 foreach ( headers_list() as
$header ) {
261 if ( preg_match(
'#^content-type: (\w+/\w+);?#i',
$header, $m ) ) {
275 $this->allowOutput =
true;
288 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.
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)
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.