9use Psr\Log\LoggerInterface;
32 private $allowOutput =
false;
35 private static $instance =
null;
41 if ( isset(
$params[
'profileID'] ) ) {
42 $this->profileID =
$params[
'profileID'];
46 $this->logger = LoggerFactory::getInstance(
'profiler' );
53 final public static function init( array $profilerConf ): void {
63 $isCLI = ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' );
64 $inSample =
$params[
'sampling'] === 1 || mt_rand( 1,
$params[
'sampling'] ) === 1;
75 ( $isCLI && !
$params[
'cliEnable'] )
77 $params[
'class'] = ProfilerStub::class;
80 if ( !is_array(
$params[
'output'] ) ) {
91 if ( !self::$instance ) {
92 trigger_error(
'Called Profiler::instance before settings are loaded', E_USER_WARNING );
96 return self::$instance;
105 $this->profileID = $id;
112 if ( $this->profileID ===
false ) {
113 return WikiMap::getCurrentWikiDbDomain()->getId();
115 return $this->profileID;
140 return $this->trxProfiler;
154 private function getOutputs() {
156 foreach ( $this->params[
'output'] as $outputType ) {
160 $outputClass = !str_contains( $outputType,
'ProfilerOutput' )
161 ?
'ProfilerOutput' . ucfirst( $outputType )
163 if ( !class_exists( $outputClass ) ) {
164 throw new UnexpectedValueException(
"'$outputType' is an invalid output type" );
166 $outputInstance =
new $outputClass( $this, $this->params );
167 if ( $outputInstance->canUse() ) {
168 $outputs[] = $outputInstance;
183 if ( $this->params[
'threshold'] > 0.0 ) {
185 $timeElapsed = microtime(
true ) - $_SERVER[
'REQUEST_TIME_FLOAT'];
186 if ( $timeElapsed <= $this->params[
'threshold'] ) {
192 foreach ( $this->getOutputs() as $output ) {
193 if ( !$output->logsToOutput() ) {
194 $outputs[] = $output;
199 $stats = $this->getFunctionStats();
200 foreach ( $outputs as $output ) {
201 $output->log( $stats );
212 if ( !$this->allowOutput ) {
217 foreach ( $this->getOutputs() as $output ) {
218 if ( $output->logsToOutput() ) {
219 $outputs[] = $output;
224 $stats = $this->getFunctionStats();
225 foreach ( $outputs as $output ) {
226 $output->log( $stats );
241 if ( $this->allowOutput ) {
242 foreach ( headers_list() as $header ) {
243 if ( preg_match(
'#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
257 $this->allowOutput =
true;
270 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.
scopedProfileOut(?SectionProfileCallback &$section=null)
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.
array $params
All of the params passed from $wgProfiler.
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.