From: Aaron Schulz Date: Thu, 25 Apr 2013 06:56:04 +0000 (-0700) Subject: Created ProfileSection class to avoid wfProfileOut() whack-a-mole. X-Git-Tag: 1.31.0-rc.0~19838 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/?a=commitdiff_plain;h=32ac1518037adc66f78432108a306a4627d79be5;p=lhc%2Fweb%2Fwiklou.git Created ProfileSection class to avoid wfProfileOut() whack-a-mole. Change-Id: I9f7e0638edd99e1ac07b83054e8f7ef255179281 --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 4813d45aca..9cdf1a4759 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -819,6 +819,7 @@ $wgAutoloadLocalClasses = array( 'ProfilerSimpleTrace' => 'includes/profiler/ProfilerSimpleTrace.php', 'ProfilerSimpleUDP' => 'includes/profiler/ProfilerSimpleUDP.php', 'ProfilerStub' => 'includes/profiler/ProfilerStub.php', + 'ProfileSection' => 'includes/profiler/Profiler.php', # includes/resourceloader 'ResourceLoader' => 'includes/resourceloader/ResourceLoader.php', diff --git a/includes/profiler/Profiler.php b/includes/profiler/Profiler.php index 49d961ef16..c77fef5c69 100644 --- a/includes/profiler/Profiler.php +++ b/includes/profiler/Profiler.php @@ -48,6 +48,44 @@ function wfProfileOut( $functionname = 'missing' ) { } } +/** + * Class for handling function-scope profiling + * + * @since 1.22 + */ +class ProfileSection { + protected $name; // string; method name + protected $enabled = false; // boolean; whether profiling is enabled + + /** + * Begin profiling of a function and return an object that ends profiling of + * the function when that object leaves scope. As long as the object is not + * specifically linked to other objects, it will fall out of scope at the same + * moment that the function to be profiled terminates. + * + * This is typically called like: + * $section = new ProfileSection( __METHOD__ ); + * + * @param string $name Name of the function to profile + */ + public function __construct( $name ) { + $this->name = $name; + if ( Profiler::$__instance === null ) { // use this directly to reduce overhead + Profiler::instance(); + } + if ( Profiler::$__instance && !( Profiler::$__instance instanceof ProfilerStub ) ) { + $this->enabled = true; + Profiler::$__instance->profileIn( $this->name ); + } + } + + function __destruct() { + if ( $this->enabled ) { + Profiler::$__instance->profileOut( $this->name ); + } + } +} + /** * @ingroup Profiler * @todo document @@ -57,7 +95,9 @@ class Profiler { $mCalls = array(), $mTotals = array(); protected $mTimeMetric = 'wall'; protected $mProfileID = false, $mCollateDone = false, $mTemplated = false; - private static $__instance = null; + + /** @var Profiler */ + public static $__instance = null; // do not call this outside Profiler and ProfileSection function __construct( $params ) { if ( isset( $params['timeMetric'] ) ) {