Merge "Fix spelling mistakes in comments"
[lhc/web/wiklou.git] / includes / profiler / ProfilerXhprof.php
index 624433b..5f7fc00 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
- * ($wgProfiler['exclude']) containing an array of function names. The
- * blacklist functionality is built into HHVM and will completely exclude the
- * named functions from profiling collection. The whitelist is implemented by
- * Xhprof class which will filter the data collected by XHProf before reporting.
- * See documentation for the Xhprof class and the XHProf extension for
- * additional information.
+ * ($wgProfiler['exclude']) containing an array of function names.
+ * Shell-style patterns are also accepted.
  *
  * @author Bryan Davis <bd808@wikimedia.org>
  * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
@@ -80,36 +72,43 @@ class ProfilerXhprof extends Profiler {
                $this->sprofiler = new SectionProfiler();
        }
 
-       /**
-        * 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 ) {
+       public function scopedProfileIn( $section ) {
+               $key = 'section.' . ltrim( $section, '.' );
+               return $this->sprofiler->scopedProfileIn( $key );
        }
 
        /**
         * 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 ) {
-       }
-
-       public function scopedProfileIn( $section ) {
-               return $this->sprofiler->scopedProfileIn( $section );
+       public function close() {
        }
 
        /**
-        * No-op for xhprof profiling.
+        * Check if a function or section should be excluded from the output.
+        *
+        * @param string $name Function or section name.
+        * @return bool
         */
-       public function close() {
+       private function shouldExclude( $name ) {
+               if ( $name === '-total' ) {
+                       return true;
+               }
+               if ( !empty( $this->params['include'] ) ) {
+                       foreach ( $this->params['include'] as $pattern ) {
+                               if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
+                                       return false;
+                               }
+                       }
+                       return true;
+               }
+               if ( !empty( $this->params['exclude'] ) ) {
+                       foreach ( $this->params['exclude'] as $pattern ) {
+                               if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
+                                       return true;
+                               }
+                       }
+               }
+               return false;
        }
 
        public function getFunctionStats() {
@@ -118,7 +117,10 @@ class ProfilerXhprof extends Profiler {
 
                $main = null; // units in ms
                foreach ( $metrics as $fname => $stats ) {
-                       // Convert elapsed times from μs to ms to match ProfilerStandard
+                       if ( $this->shouldExclude( $fname ) ) {
+                               continue;
+                       }
+                       // Convert elapsed times from μs to ms to match interface
                        $entry = array(
                                'name' => $fname,
                                'calls' => $stats['ct'],
@@ -139,8 +141,7 @@ class ProfilerXhprof extends Profiler {
 
                // Merge in all of the custom profile sections
                foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
-                       if ( $stats['name'] === '-total' ) {
-                               // Discard section profiler running totals
+                       if ( $this->shouldExclude( $stats['name'] ) ) {
                                continue;
                        }
 
@@ -209,4 +210,12 @@ class ProfilerXhprof extends Profiler {
                }
                return implode( "\n", $out );
        }
+
+       /**
+        * Retrieve raw data from xhprof
+        * @return array
+        */
+       public function getRawData() {
+               return $this->xhprof->getRawData();
+       }
 }