Allow dumping raw xhprof data for consumption by xhprof GUI
authorStanislav Malyshev <smalyshev@gmail.com>
Tue, 3 Mar 2015 23:17:02 +0000 (15:17 -0800)
committerBryanDavis <bdavis@wikimedia.org>
Wed, 4 Mar 2015 01:29:02 +0000 (01:29 +0000)
Change-Id: Iab90cef1c61b92ffc6d46a6bc93a03cf7bc2adb9

StartProfiler.sample
autoload.php
includes/profiler/Profiler.php
includes/profiler/ProfilerXhprof.php
includes/profiler/output/ProfilerOutputDump.php [new file with mode: 0644]

index 4721a9d..6681b87 100644 (file)
@@ -6,7 +6,7 @@
  *
  * For output, add:
  *  $wgProfiler['output'] = array( 'text' );
- *    'text' can be one (or more) of 'text' 'udp' or 'db'
+ *    'text' can be one (or more) of 'text' 'udp' 'db' or 'dump'
  *    'db' requires creating the profiling table, see patch-profiling.sql
  *
  * The 'text' output will be added to the output page in a comment approriate
@@ -18,6 +18,9 @@
  * The 'db' output expects a database table that can be created by applying
  * maintenance/archives/patch-profiling.sql to your database.
  *
+ * The 'dump' output expects a $wgProfiler['outputDir'] telling it where to
+ * write dump files. The files produced are compatible with the XHProf gui.
+ *
  * For a rudimentary sampling profiler:
  *   $wgProfiler['class'] = 'ProfilerXhprof';
  *   $wgProfiler['output'] = array( 'db' );
index dbc47ab..998deb9 100644 (file)
@@ -907,6 +907,7 @@ $wgAutoloadLocalClasses = array(
        'Profiler' => __DIR__ . '/includes/profiler/Profiler.php',
        'ProfilerOutput' => __DIR__ . '/includes/profiler/output/ProfilerOutput.php',
        'ProfilerOutputDb' => __DIR__ . '/includes/profiler/output/ProfilerOutputDb.php',
+       'ProfilerOutputDump' => __DIR__ . '/includes/profiler/output/ProfilerOutputDump.php',
        'ProfilerOutputText' => __DIR__ . '/includes/profiler/output/ProfilerOutputText.php',
        'ProfilerOutputUdp' => __DIR__ . '/includes/profiler/output/ProfilerOutputUdp.php',
        'ProfilerSectionOnly' => __DIR__ . '/includes/profiler/ProfilerSectionOnly.php',
index 4b74206..69470fd 100644 (file)
@@ -46,6 +46,7 @@ abstract class Profiler {
                'db' => 'ProfilerOutputDb',
                'text' => 'ProfilerOutputText',
                'udp' => 'ProfilerOutputUdp',
+               'dump' => 'ProfilerOutputDump',
        );
 
        /** @var Profiler */
@@ -167,7 +168,7 @@ abstract class Profiler {
                if ( !is_array( $output ) ) {
                        $output = array( $output );
                }
-
+               $stats = null;
                foreach ( $output as $outType ) {
                        if ( !isset( self::$outputTypes[$outType] ) ) {
                                throw new MWException( "'$outType' is an invalid output type" );
@@ -177,7 +178,10 @@ abstract class Profiler {
                        /** @var ProfilerOutput $profileOut */
                        $profileOut = new $class( $this, $this->params );
                        if ( $profileOut->canUse() ) {
-                               $profileOut->log( $this->getFunctionStats() );
+                               if ( is_null( $stats ) ) {
+                                       $stats = $this->getFunctionStats();
+                               }
+                               $profileOut->log( $stats );
                        }
                }
        }
index 7a50497..f36cdc1 100644 (file)
@@ -183,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();
+       }
 }
diff --git a/includes/profiler/output/ProfilerOutputDump.php b/includes/profiler/output/ProfilerOutputDump.php
new file mode 100644 (file)
index 0000000..bf4b85c
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+/**
+ * Profiler dumping output in xhprof dump file
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Profiler
+ */
+
+/**
+ * Profiler dumping output in xhprof dump file
+ * @ingroup Profiler
+ *
+ * @since 1.25
+ */
+class ProfilerOutputDump extends ProfilerOutput {
+
+       protected $suffix = ".xhprof";
+
+       /**
+        * Can this output type be used?
+        *
+        * @return bool
+        */
+       public function canUse() {
+               if ( empty( $this->params['outputDir'] ) ) {
+                       return false;
+               }
+               return true;
+       }
+
+       public function log( array $stats ) {
+               $data = $this->collector->getRawData();
+               $filename = sprintf( "%s/%s.%s%s", $this->params['outputDir'], uniqid(), $this->collector->getProfileID(), $this->suffix );
+               file_put_contents( $filename, serialize( $data ) );
+       }
+}