From a5d97eaa901b6c86ed17d7eb6949332549794f24 Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Thu, 29 Oct 2015 15:53:10 -0700 Subject: [PATCH] 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 --- Scribunto.php | 7 +++++++ common/Hooks.php | 16 +++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) 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 );