Make the percentile threshold for slow function stats configurable

Introduce $wgScribuntoSlowFunctionThreshold, which is a float value between 0
and 1 (defaults to 0.9), specifying the percentile threshold for slow function
invocation reporting.

Change-Id: I3bd862347c21ba68d2f2f0729a834c4f7be3cd43
This commit is contained in:
Ori Livneh 2015-10-29 15:53:10 -07:00 committed by Tim Starling
parent b5df651e1e
commit a5d97eaa90
2 changed files with 18 additions and 5 deletions

View file

@ -188,6 +188,13 @@ $wgScribuntoUseCodeEditor = false;
*/
$wgScribuntoGatherFunctionStats = false;
/**
* If $wgScribuntoGatherFunctionStats is true, this variable specifies
* the percentile threshold for slow function invocations. Should be
* a value between 0 and 1 (exclusive).
*/
$wgScribuntoSlowFunctionThreshold = 0.90;
define( 'NS_MODULE', 828 );
define( 'NS_MODULE_TALK', 829 );

View file

@ -176,12 +176,17 @@ class ScribuntoHooks {
* @param int $timing Function execution time in milliseconds.
*/
public static function reportTiming( $moduleName, $functionName, $timing ) {
global $wgScribuntoGatherFunctionStats;
global $wgScribuntoGatherFunctionStats, $wgScribuntoSlowFunctionThreshold;
if ( !$wgScribuntoGatherFunctionStats ) {
return;
}
$threshold = $wgScribuntoSlowFunctionThreshold;
if ( !( is_float( $threshold ) && $threshold > 0 && $threshold < 1 ) ) {
return;
}
static $cache;
if ( !$cache ) {
@ -189,19 +194,20 @@ class ScribuntoHooks {
}
// To control the sampling rate, we keep a compact histogram of
// observations in APC, and extract the 99th percentile. We need
// APC and \RunningStat\PSquare to do that.
// observations in APC, and extract the Nth percentile (specified
// via $wgScribuntoSlowFunctionThreshold; defaults to 0.90).
// We need APC and \RunningStat\PSquare to do that.
if ( !class_exists( '\RunningStat\PSquare' ) || $cache instanceof EmptyBagOStuff ) {
return;
}
$key = $cache->makeGlobalKey( __METHOD__ );
$key = $cache->makeGlobalKey( __METHOD__, $threshold );
// This is a classic "read-update-write" critical section with no
// mutual exclusion, but the only consequence is that some samples
// will be dropped. We only need enough samples to estimate the
// the shape of the data, so that's fine.
$ps = $cache->get( $key ) ?: new \RunningStat\PSquare( 0.99 );
$ps = $cache->get( $key ) ?: new \RunningStat\PSquare( $threshold );
$ps->addObservation( $timing );
$cache->set( $key, $ps, 60 );