From 5a6d1ee2d3f9cfed0413a07b3b364943c8991039 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Sat, 16 Apr 2011 19:00:54 +0000 Subject: [PATCH] More profiler cleanup: * Move autoloader up a little bit so the profiler classes can use it * Make Profiler into a singleton so it's lazy-constructed, $wgProfiler is now a configuration array (used 'visible' in ProfilerSimpleText as an example of other globals we can move into this array). If $wgProfiler is set to an object, it'll use that for back-compat * Maintenance: rather than setting up the profiler and then disabling it, just disable it from the start * Kill $wgProfiling -> now that ProfilerStub overrides profileIn() and profileOut(), it's not needed * dumpHTML needs some fixes still --- RELEASE-NOTES | 2 + StartProfiler.sample | 8 ++-- includes/WebStart.php | 9 ++-- includes/profiler/Profiler.php | 55 ++++++++++++++--------- includes/profiler/ProfilerSimple.php | 4 -- includes/profiler/ProfilerSimpleText.php | 18 ++++---- includes/profiler/ProfilerSimpleTrace.php | 4 -- includes/profiler/ProfilerSimpleUDP.php | 4 -- includes/profiler/ProfilerStub.php | 8 ---- maintenance/Maintenance.php | 4 +- maintenance/doMaintenance.php | 9 +--- 11 files changed, 57 insertions(+), 68 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 2ab580d6a1..dd6d5db62d 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -65,6 +65,8 @@ PHP if you have not done so prior to upgrading MediaWiki. math conversion after upgrading, obtain the Math extension from SVN or from http://www.mediawiki.org/wiki/Extension:Math and add to LocalSettings.php: require_once "$IP/extensions/Math/Math.php"; +* $wgProfiler is now a configuration array, see StartProfiler.sample for details +* $wgProfiling has been removed === New features in 1.18 === * (bug 8130) Query pages should limit to content namespaces, not just main diff --git a/StartProfiler.sample b/StartProfiler.sample index f91aeb9281..ce322d7cb4 100644 --- a/StartProfiler.sample +++ b/StartProfiler.sample @@ -6,15 +6,13 @@ require_once( dirname(__FILE__).'/includes/ProfilerStub.php' ); * To use a profiler, copy this file to StartProfiler.php, * delete the PHP line above, and add something like this: * - * require_once( dirname(__FILE__).'/includes/Profiler.php' ); - * $wgProfiler = new Profiler; + * $wgProfiler['class'] = 'Profiler'; * * Or for a sampling profiler: * if ( !mt_rand( 0, 100 ) ) { - * require_once( dirname(__FILE__).'/includes/Profiler.php' ); - * $wgProfiler = new Profiler; + * $wgProfiler['class'] = 'Profiler'; * } else { - * require_once( dirname(__FILE__).'/includes/ProfilerStub.php' ); + * $wgProfiler['class'] = 'ProfilerStub'; * } * * Configuration of the profiler output can be done in LocalSettings.php diff --git a/includes/WebStart.php b/includes/WebStart.php index adc5899968..fa92fc7213 100644 --- a/includes/WebStart.php +++ b/includes/WebStart.php @@ -89,19 +89,18 @@ if ( !defined( 'MW_COMPILED' ) ) { # Get MWInit class require_once( "$IP/includes/Init.php" ); + # Start the autoloader, so that extensions can derive classes from core files + require_once( "$IP/includes/AutoLoader.php" ); + # Start profiler # FIXME: rewrite wfProfileIn/wfProfileOut so that they can work in compiled mode + require_once( "$IP/includes/profiler/Profiler.php" ); if ( file_exists( "$IP/StartProfiler.php" ) ) { require_once( "$IP/StartProfiler.php" ); - } else { - require_once( "$IP/includes/profiler/ProfilerStub.php" ); } # Load up some global defines. require_once( "$IP/includes/Defines.php" ); - - # Start the autoloader, so that extensions can derive classes from core files - require_once( "$IP/includes/AutoLoader.php" ); } wfProfileIn( 'WebStart.php-conf' ); diff --git a/includes/profiler/Profiler.php b/includes/profiler/Profiler.php index ce2e5323ec..c219efa80d 100644 --- a/includes/profiler/Profiler.php +++ b/includes/profiler/Profiler.php @@ -7,16 +7,21 @@ * This file is only included if profiling is enabled */ -/** backward compatibility */ -$wgProfiling = true; +/** + * Default profiling configuration. Permitted keys are: + * class : Name of Profiler or subclass to use for profiling + * visible : Whether to output the profile info [ProfilerSimpleText only] + */ +$wgProfiler = array( + 'class' => 'ProfilerStub', +); /** * Begin profiling of a function * @param $functionname String: name of the function we will profile */ function wfProfileIn( $functionname ) { - global $wgProfiler; - $wgProfiler->profileIn( $functionname ); + Profiler::instance()->profileIn( $functionname ); } /** @@ -24,24 +29,21 @@ function wfProfileIn( $functionname ) { * @param $functionname String: name of the function we have profiled */ function wfProfileOut( $functionname = 'missing' ) { - global $wgProfiler; - $wgProfiler->profileOut( $functionname ); + Profiler::instance()->profileOut( $functionname ); } /** * Returns a profiling output to be stored in debug file */ function wfGetProfilingOutput() { - global $wgProfiler; - return $wgProfiler->getOutput(); + Profiler::instance()->getOutput(); } /** * Close opened profiling sections */ function wfProfileClose() { - global $wgProfiler; - $wgProfiler->close(); + Profiler::instance()->close(); } if (!function_exists('memory_get_usage')) { @@ -59,6 +61,7 @@ class Profiler { var $mStack = array (), $mWorkStack = array (), $mCollated = array (); var $mCalls = array (), $mTotals = array (); var $mTemplated = false; + private static $__instance = null; function __construct() { // Push an entry for the pre-profile setup time onto the stack @@ -71,14 +74,33 @@ class Profiler { } } + /** + * Singleton + * @return Profiler + */ + public static function instance() { + if( is_null( self::$__instance ) ) { + global $wgProfiler; + if( is_array( $wgProfiler ) ) { + $class = $wgProfiler['class']; + self::$__instance = new $class( $wgProfiler ); + } elseif( $wgProfiler instanceof Profiler ) { + self::$__instance = $wgProfiler; // back-compat + } else { + throw new MWException( '$wgProfiler set to bogus value' ); + } + + } + return self::$__instance; + } + /** * Called by wfProfieIn() * * @param $functionname String */ public function profileIn( $functionname ) { - global $wgDebugFunctionEntry, $wgProfiling; - if( !$wgProfiling ) return; + global $wgDebugFunctionEntry; if( $wgDebugFunctionEntry ){ $this->debug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering ' . $functionname . "\n" ); } @@ -92,8 +114,7 @@ class Profiler { * @param $functionname String */ public function profileOut($functionname) { - global $wgDebugFunctionEntry, $wgProfiling; - if( !$wgProfiling ) return; + global $wgDebugFunctionEntry; $memory = memory_get_usage(); $time = $this->getTime(); @@ -128,12 +149,6 @@ class Profiler { * called by wfProfileClose() */ public function close() { - global $wgProfiling; - - # Avoid infinite loop - if( !$wgProfiling ) - return; - while( count( $this->mWorkStack ) ){ $this->profileOut( 'close' ); } diff --git a/includes/profiler/ProfilerSimple.php b/includes/profiler/ProfilerSimple.php index 975b0ed837..d1b20bccc4 100644 --- a/includes/profiler/ProfilerSimple.php +++ b/includes/profiler/ProfilerSimple.php @@ -4,10 +4,6 @@ * @ingroup Profiler */ -if ( !class_exists( 'Profiler' ) ) { - require_once( dirname( __FILE__ ) . '/Profiler.php' ); -} - /** * Simple profiler base class. * @todo document methods (?) diff --git a/includes/profiler/ProfilerSimpleText.php b/includes/profiler/ProfilerSimpleText.php index 66b49b5d6c..5bdc0e3862 100644 --- a/includes/profiler/ProfilerSimpleText.php +++ b/includes/profiler/ProfilerSimpleText.php @@ -4,18 +4,13 @@ * @ingroup Profiler */ -if ( !class_exists( 'ProfilerSimple' ) ) { - require_once( dirname( __FILE__ ) . '/ProfilerSimple.php' ); -} - /** * The least sophisticated profiler output class possible, view your source! :) * - * Put the following 3 lines in StartProfiler.php: + * Put the following 2 lines in StartProfiler.php: * - * require_once( dirname( __FILE__ ) . '/includes/ProfilerSimpleText.php' ); - * $wgProfiler = new ProfilerSimpleText; - * $wgProfiler->visible=true; + * $wgProfiler['class'] = 'ProfilerSimpleText'; + * $wgProfiler['visible'] = true; * * @ingroup Profiler */ @@ -23,6 +18,13 @@ class ProfilerSimpleText extends ProfilerSimple { public $visible=false; /* Show as
 or