diff --git a/Scribunto.php b/Scribunto.php index e9f83a52..49961cf7 100644 --- a/Scribunto.php +++ b/Scribunto.php @@ -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 ); diff --git a/common/Hooks.php b/common/Hooks.php index 508250e1..945aad39 100644 --- a/common/Hooks.php +++ b/common/Hooks.php @@ -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 );