if ( self::$__instance === null ) {
global $wgProfiler;
if ( is_array( $wgProfiler ) ) {
- if ( !isset( $wgProfiler['class'] ) ) {
+ $class = isset( $wgProfiler['class'] ) ? $wgProfiler['class'] : 'ProfilerStub';
+ $factor = isset( $wgProfiler['sampling'] ) ? $wgProfiler['sampling'] : 1;
+ if ( PHP_SAPI === 'cli' || mt_rand( 0, $factor - 1 ) != 0 ) {
$class = 'ProfilerStub';
- } else {
- $class = $wgProfiler['class'];
}
self::$__instance = new $class( $wgProfiler );
} else {
}
/**
- * Return whether this a stub profiler
+ * Replace the current profiler with $profiler if no non-stub profiler is set
*
- * @return bool
+ * @param Profiler $profiler
+ * @throws MWException
+ * @since 1.25
*/
- abstract public function isStub();
+ final public static function replaceStubInstance( Profiler $profiler ) {
+ if ( self::$__instance && !( self::$__instance instanceof ProfilerStub ) ) {
+ throw new MWException( 'Could not replace non-stub profiler instance.' );
+ } else {
+ self::$__instance = $profiler;
+ }
+ }
/**
* @param string $id
*/
abstract public function profileOut( $functionname );
+ /**
+ * Mark the start of a custom profiling frame (e.g. DB queries).
+ * The frame ends when the result of this method falls out of scope.
+ *
+ * @param string $section
+ * @return ScopedCallback|null
+ * @since 1.25
+ */
+ abstract public function scopedProfileIn( $section );
+
+ /**
+ * @param ScopedCallback $section
+ */
+ public function scopedProfileOut( ScopedCallback &$section ) {
+ $section = null;
+ }
+
/**
* @return TransactionProfiler
* @since 1.25
* @since 1.25
*/
public function logData() {
- $output = isset( $this->params['output'] ) ?
- $this->params['output'] : null;
+ $output = isset( $this->params['output'] ) ? $this->params['output'] : null;
- if ( !$output || $this->isStub() ) {
+ if ( !$output || $this instanceof ProfilerStub ) {
// return early when no output classes defined or we're a stub
return;
}