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;
170 private function getOutputs() {
172 foreach ( $this->params[
'output'] as $outputType ) {
176 $outputClass = strpos( $outputType,
'ProfilerOutput' ) ===
false
177 ?
'ProfilerOutput' . ucfirst( $outputType )
179 if ( !class_exists( $outputClass ) ) {
180 throw new UnexpectedValueException(
"'$outputType' is an invalid output type" );
182 $outputInstance =
new $outputClass( $this, $this->params );
183 if ( $outputInstance->canUse() ) {
184 $outputs[] = $outputInstance;
199 if ( $this->params[
'threshold'] > 0.0 ) {
201 $timeElapsed = microtime(
true ) - $_SERVER[
'REQUEST_TIME_FLOAT'];
202 if ( $timeElapsed <= $this->params[
'threshold'] ) {
208 foreach ( $this->getOutputs() as $output ) {
209 if ( !$output->logsToOutput() ) {
210 $outputs[] = $output;
215 $stats = $this->getFunctionStats();
216 foreach ( $outputs as $output ) {
217 $output->log( $stats );
228 if ( !$this->allowOutput ) {
233 foreach ( $this->getOutputs() as $output ) {
234 if ( $output->logsToOutput() ) {
235 $outputs[] = $output;
240 $stats = $this->getFunctionStats();
241 foreach ( $outputs as $output ) {
242 $output->log( $stats );
257 if ( $this->allowOutput ) {
258 foreach ( headers_list() as
$header ) {
259 if ( preg_match(
'#^content-type: (\w+/\w+);?#i',
$header, $m ) ) {
273 $this->allowOutput =
true;
286 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.