Allow dumping raw xhprof data for consumption by xhprof GUI

Change-Id: Iab90cef1c61b92ffc6d46a6bc93a03cf7bc2adb9
This commit is contained in:
Stanislav Malyshev 2015-03-03 15:17:02 -08:00 committed by BryanDavis
parent 0d604ca809
commit 750e4eb9d9
5 changed files with 70 additions and 3 deletions

View 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' );

View 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',

View 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 );
}
}
}

View 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();
}
}

View file

@ -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 ) );
}
}