Fixed running percents in SectionProfiler
[lhc/web/wiklou.git] / includes / profiler / Profiler.php
index f2bdc84..2be142f 100644 (file)
@@ -72,10 +72,10 @@ abstract class Profiler {
                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 {
@@ -86,11 +86,19 @@ abstract class Profiler {
        }
 
        /**
-        * 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
@@ -124,6 +132,23 @@ abstract class Profiler {
         */
        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
@@ -144,10 +169,9 @@ abstract class Profiler {
         * @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;
                }