* @todo document
*/
function wfLogProfilingData() {
- global $wgDebugLogGroups, $wgDebugRawPage, $wgProfileLimit;
+ global $wgDebugLogGroups, $wgDebugRawPage;
$context = RequestContext::getMain();
$request = $context->getRequest();
+
+ $profiler = Profiler::instance();
+ $profiler->setContext( $context );
+ $profiler->logData();
+
$config = $context->getConfig();
if ( $config->has( 'StatsdServer' ) ) {
$statsdServer = explode( ':', $config->get( 'StatsdServer' ) );
$statsdClient->send( $context->getStats()->getBuffer() );
}
- $profiler = Profiler::instance();
-
# Profiling must actually be enabled...
if ( $profiler instanceof ProfilerStub ) {
return;
}
- // Get total page request time and only show pages that longer than
- // $wgProfileLimit time (default is 0)
- $elapsed = $request->getElapsedTime();
- if ( $elapsed <= $wgProfileLimit ) {
- return;
- }
-
- $profiler->logData();
-
if ( isset( $wgDebugLogGroups['profileoutput'] )
&& $wgDebugLogGroups['profileoutput'] === false
) {
return;
}
- $ctx = array( 'elapsed' => $elapsed );
+ $ctx = array( 'elapsed' => $request->getElapsedTime() );
if ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ctx['forwarded_for'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
protected $templated = false;
/** @var array All of the params passed from $wgProfiler */
protected $params = array();
+ /** @var IContextSource Current request context */
+ protected $context = null;
/** @var TransactionProfiler */
protected $trxProfiler;
'text' => 'ProfilerOutputText',
'udp' => 'ProfilerOutputUdp',
'dump' => 'ProfilerOutputDump',
+ 'context' => 'ProfilerOutputStats',
);
/** @var Profiler */
*/
final public static function instance() {
if ( self::$instance === null ) {
- global $wgProfiler;
+ global $wgProfiler, $wgProfileLimit;
+
+ $params = array(
+ 'class' => 'ProfilerStub',
+ 'sampling' => 1,
+ 'threshold' => $wgProfileLimit,
+ 'output' => array(),
+ );
if ( is_array( $wgProfiler ) ) {
- $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';
- }
- self::$instance = new $class( $wgProfiler );
- } else {
- self::$instance = new ProfilerStub( array() );
+ $params = array_merge( $params, $wgProfiler );
+ }
+
+ $inSample = mt_rand( 0, $params['sampling'] - 1 ) === 0;
+ if ( PHP_SAPI === 'cli' || !$inSample ) {
+ $params['class'] = 'ProfilerStub';
+ }
+
+ if ( !is_array( $params['output'] ) ) {
+ $params['output'] = array( $params['output'] );
}
+
+ self::$instance = new $params['class']( $params );
}
return self::$instance;
}
}
}
+ /**
+ * Sets the context for this Profiler
+ *
+ * @param IContextSource $context
+ * @since 1.25
+ */
+ public function setContext( $context ) {
+ $this->context = $context;
+ }
+
+ /**
+ * Gets the context for this Profiler
+ *
+ * @return IContextSource
+ * @since 1.25
+ */
+ public function getContext() {
+ if ( $this->context ) {
+ return $this->context;
+ } else {
+ wfDebug( __METHOD__ . " called and \$context is null. " .
+ "Return RequestContext::getMain(); for sanity\n" );
+ return RequestContext::getMain();
+ }
+ }
+
// Kept BC for now, remove when possible
public function profileIn( $functionname ) {}
public function profileOut( $functionname ) {}
abstract public function close();
/**
- * Log the data to some store or even the page output
+ * Get all usable outputs.
*
* @throws MWException
+ * @return array Array of ProfilerOutput instances.
+ * @since 1.25
+ */
+ private function getOutputs() {
+ $outputs = array();
+ foreach ( $this->params['output'] as $outputType ) {
+ if ( !isset( self::$outputTypes[$outputType] ) ) {
+ throw new MWException( "'$outputType' is an invalid output type" );
+ }
+ $outputClass = self::$outputTypes[$outputType];
+ $outputInstance = new $outputClass( $this, $this->params );
+ if ( $outputInstance->canUse() ) {
+ $outputs[] = $outputInstance;
+ }
+ }
+ return $outputs;
+ }
+
+ /**
+ * Log the data to some store or even the page output
+ *
* @since 1.25
*/
public function logData() {
- $output = isset( $this->params['output'] ) ? $this->params['output'] : null;
+ $request = $this->getContext()->getRequest();
- if ( !$output || $this instanceof ProfilerStub ) {
- // return early when no output classes defined or we're a stub
+ $timeElapsed = $request->getElapsedTime();
+ $timeElapsedThreshold = $this->params['threshold'];
+ if ( $timeElapsed <= $timeElapsedThreshold ) {
return;
}
- if ( !is_array( $output ) ) {
- $output = array( $output );
+ $outputs = $this->getOutputs();
+ if ( !$outputs ) {
+ return;
}
- $stats = null;
- foreach ( $output as $outType ) {
- if ( !isset( self::$outputTypes[$outType] ) ) {
- throw new MWException( "'$outType' is an invalid output type" );
- }
- $class = self::$outputTypes[$outType];
- /** @var ProfilerOutput $profileOut */
- $profileOut = new $class( $this, $this->params );
- if ( $profileOut->canUse() ) {
- if ( is_null( $stats ) ) {
- $stats = $this->getFunctionStats();
- }
- $profileOut->log( $stats );
- }
+ $stats = $this->getFunctionStats();
+ foreach ( $outputs as $output ) {
+ $output->log( $stats );
}
}
--- /dev/null
+<?php
+/**
+ * ProfilerOutput class that flushes profiling data to the profiling
+ * context's stats buffer.
+ *
+ * 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
+ */
+
+/**
+ * ProfilerOutput class that flushes profiling data to the profiling
+ * context's stats buffer.
+ *
+ * @ingroup Profiler
+ * @since 1.25
+ */
+class ProfilerOutputStats extends ProfilerOutput {
+
+ /**
+ * Flush profiling data to the current profiling context's stats buffer.
+ *
+ * @param array $stats
+ */
+ public function log( array $stats ) {
+ $contextStats = $this->collector->getContext()->getStats();
+
+ foreach ( $stats as $stat ) {
+ // Sanitize the key
+ $key = str_replace( '::', '.', $stat['name'] );
+ $key = preg_replace( '/[^a-z.]+/i', '_', $key );
+ $key = trim( $key, '_.' );
+
+ // Convert fractional seconds to whole milliseconds
+ $cpu = round( $stat['cpu'] * 1000 );
+ $real = round( $stat['real'] * 1000 );
+
+ $contextStats->increment( "{$key}.calls" );
+ $contextStats->timing( "{$key}.cpu", $cpu );
+ $contextStats->timing( "{$key}.real", $real );
+ }
+ }
+}