Merge "mw.ui: button: Update focus state"
[lhc/web/wiklou.git] / includes / profiler / ProfilerXhprof.php
index 2fef011..f36cdc1 100644 (file)
@@ -21,9 +21,6 @@
 /**
  * Profiler wrapper for XHProf extension.
  *
- * Mimics the output of ProfilerStandard using data collected via the XHProf
- * PHP extension.
- *
  * @code
  * $wgProfiler['class'] = 'ProfilerXhprof';
  * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
  * $wgProfiler['output'] = 'udp';
  * @endcode
  *
- * Rather than obeying wfProfileIn() and wfProfileOut() calls placed in the
- * application code, ProfilerXhprof profiles all functions using the XHProf
- * PHP extenstion. For PHP5 users, this extension can be installed via PECL or
- * your operating system's package manager. XHProf support is built into HHVM.
+ * ProfilerXhprof profiles all functions using the XHProf PHP extenstion.
+ * For PHP5 users, this extension can be installed via PECL or your operating
+ * system's package manager. XHProf support is built into HHVM.
  *
  * To restrict the functions for which profiling data is collected, you can
  * use either a whitelist ($wgProfiler['include']) or a blacklist
  * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
  */
 class ProfilerXhprof extends Profiler {
-
        /**
         * @var Xhprof $xhprof
         */
        protected $xhprof;
 
        /**
-        * Type of report to send when logData() is called.
-        * @var string $logType
-        */
-       protected $logType;
-
-       /**
-        * Should profile report sent to in page content be visible?
-        * @var bool $visible
+        * Profiler for explicit, arbitrary, frame labels
+        * @var SectionProfiler
         */
-       protected $visible;
+       protected $sprofiler;
 
        /**
         * @param array $params
         * @see Xhprof::__construct()
         */
        public function __construct( array $params = array() ) {
-               $params = array_merge(
-                       array(
-                               'log' => 'text',
-                               'visible' => false
-                       ),
-                       $params
-               );
                parent::__construct( $params );
-               $this->logType = $params['log'];
-               $this->visible = $params['visible'];
                $this->xhprof = new Xhprof( $params );
-       }
-
-       /**
-        * No-op for xhprof profiling.
-        *
-        * Use the 'include' configuration key instead if you need to constrain
-        * the functions that are profiled.
-        *
-        * @param string $functionname
-        */
-       public function profileIn( $functionname ) {
-       }
-
-       /**
-        * No-op for xhprof profiling.
-        *
-        * Use the 'include' configuration key instead if you need to constrain
-        * the functions that are profiled.
-        *
-        * @param string $functionname
-        */
-       public function profileOut( $functionname ) {
+               $this->sprofiler = new SectionProfiler();
        }
 
        public function scopedProfileIn( $section ) {
-               static $exists = null;
-               // Only HHVM supports this, not the standard PECL extension
-               if ( $exists === null ) {
-                       $exists = function_exists( 'xhprof_frame_begin' );
-               }
-
-               if ( $exists ) {
-                       xhprof_frame_begin( $section );
-                       return new ScopedCallback( function () {
-                               xhprof_frame_end();
-                       } );
-               }
-
-               return new ScopedCallback( function () {
-                       // no-op
-               } );
+               return $this->sprofiler->scopedProfileIn( $section );
        }
 
        /**
@@ -146,9 +90,10 @@ class ProfilerXhprof extends Profiler {
                $metrics = $this->xhprof->getCompleteMetrics();
                $profile = array();
 
+               $main = null; // units in ms
                foreach ( $metrics as $fname => $stats ) {
-                       // Convert elapsed times from μs to ms to match ProfilerStandard
-                       $profile[] = array(
+                       // Convert elapsed times from μs to ms to match interface
+                       $entry = array(
                                'name' => $fname,
                                'calls' => $stats['ct'],
                                'real' => $stats['wt']['total'] / 1000,
@@ -160,6 +105,24 @@ class ProfilerXhprof extends Profiler {
                                'min_real' => $stats['wt']['min'] / 1000,
                                'max_real' => $stats['wt']['max'] / 1000
                        );
+                       $profile[] = $entry;
+                       if ( $fname === 'main()' ) {
+                               $main = $entry;
+                       }
+               }
+
+               // Merge in all of the custom profile sections
+               foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
+                       if ( $stats['name'] === '-total' ) {
+                               // Discard section profiler running totals
+                               continue;
+                       }
+
+                       // @note: getFunctionStats() values already in ms
+                       $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
+                       $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
+                       $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
+                       $profile[] = $stats; // assume no section names collide with $metrics
                }
 
                return $profile;
@@ -220,4 +183,12 @@ class ProfilerXhprof extends Profiler {
                }
                return implode( "\n", $out );
        }
+
+       /**
+        * Retrieve raw data from xhprof
+        * @return array
+        */
+       public function getRawData() {
+               return $this->xhprof->getRawData();
+       }
 }