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
This commit is contained in:
Chad Horohoe 2011-04-16 19:00:54 +00:00
parent c962c5c35c
commit 5a6d1ee2d3
11 changed files with 57 additions and 68 deletions

View file

@ -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 math conversion after upgrading, obtain the Math extension from SVN or from
http://www.mediawiki.org/wiki/Extension:Math and add to LocalSettings.php: http://www.mediawiki.org/wiki/Extension:Math and add to LocalSettings.php:
require_once "$IP/extensions/Math/Math.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 === === New features in 1.18 ===
* (bug 8130) Query pages should limit to content namespaces, not just main * (bug 8130) Query pages should limit to content namespaces, not just main

View file

@ -6,15 +6,13 @@ require_once( dirname(__FILE__).'/includes/ProfilerStub.php' );
* To use a profiler, copy this file to StartProfiler.php, * To use a profiler, copy this file to StartProfiler.php,
* delete the PHP line above, and add something like this: * delete the PHP line above, and add something like this:
* *
* require_once( dirname(__FILE__).'/includes/Profiler.php' ); * $wgProfiler['class'] = 'Profiler';
* $wgProfiler = new Profiler;
* *
* Or for a sampling profiler: * Or for a sampling profiler:
* if ( !mt_rand( 0, 100 ) ) { * if ( !mt_rand( 0, 100 ) ) {
* require_once( dirname(__FILE__).'/includes/Profiler.php' ); * $wgProfiler['class'] = 'Profiler';
* $wgProfiler = new Profiler;
* } else { * } else {
* require_once( dirname(__FILE__).'/includes/ProfilerStub.php' ); * $wgProfiler['class'] = 'ProfilerStub';
* } * }
* *
* Configuration of the profiler output can be done in LocalSettings.php * Configuration of the profiler output can be done in LocalSettings.php

View file

@ -89,19 +89,18 @@ if ( !defined( 'MW_COMPILED' ) ) {
# Get MWInit class # Get MWInit class
require_once( "$IP/includes/Init.php" ); 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 # Start profiler
# FIXME: rewrite wfProfileIn/wfProfileOut so that they can work in compiled mode # 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" ) ) { if ( file_exists( "$IP/StartProfiler.php" ) ) {
require_once( "$IP/StartProfiler.php" ); require_once( "$IP/StartProfiler.php" );
} else {
require_once( "$IP/includes/profiler/ProfilerStub.php" );
} }
# Load up some global defines. # Load up some global defines.
require_once( "$IP/includes/Defines.php" ); 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' ); wfProfileIn( 'WebStart.php-conf' );

View file

@ -7,16 +7,21 @@
* This file is only included if profiling is enabled * 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 * Begin profiling of a function
* @param $functionname String: name of the function we will profile * @param $functionname String: name of the function we will profile
*/ */
function wfProfileIn( $functionname ) { function wfProfileIn( $functionname ) {
global $wgProfiler; Profiler::instance()->profileIn( $functionname );
$wgProfiler->profileIn( $functionname );
} }
/** /**
@ -24,24 +29,21 @@ function wfProfileIn( $functionname ) {
* @param $functionname String: name of the function we have profiled * @param $functionname String: name of the function we have profiled
*/ */
function wfProfileOut( $functionname = 'missing' ) { function wfProfileOut( $functionname = 'missing' ) {
global $wgProfiler; Profiler::instance()->profileOut( $functionname );
$wgProfiler->profileOut( $functionname );
} }
/** /**
* Returns a profiling output to be stored in debug file * Returns a profiling output to be stored in debug file
*/ */
function wfGetProfilingOutput() { function wfGetProfilingOutput() {
global $wgProfiler; Profiler::instance()->getOutput();
return $wgProfiler->getOutput();
} }
/** /**
* Close opened profiling sections * Close opened profiling sections
*/ */
function wfProfileClose() { function wfProfileClose() {
global $wgProfiler; Profiler::instance()->close();
$wgProfiler->close();
} }
if (!function_exists('memory_get_usage')) { if (!function_exists('memory_get_usage')) {
@ -59,6 +61,7 @@ class Profiler {
var $mStack = array (), $mWorkStack = array (), $mCollated = array (); var $mStack = array (), $mWorkStack = array (), $mCollated = array ();
var $mCalls = array (), $mTotals = array (); var $mCalls = array (), $mTotals = array ();
var $mTemplated = false; var $mTemplated = false;
private static $__instance = null;
function __construct() { function __construct() {
// Push an entry for the pre-profile setup time onto the stack // 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() * Called by wfProfieIn()
* *
* @param $functionname String * @param $functionname String
*/ */
public function profileIn( $functionname ) { public function profileIn( $functionname ) {
global $wgDebugFunctionEntry, $wgProfiling; global $wgDebugFunctionEntry;
if( !$wgProfiling ) return;
if( $wgDebugFunctionEntry ){ if( $wgDebugFunctionEntry ){
$this->debug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering ' . $functionname . "\n" ); $this->debug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering ' . $functionname . "\n" );
} }
@ -92,8 +114,7 @@ class Profiler {
* @param $functionname String * @param $functionname String
*/ */
public function profileOut($functionname) { public function profileOut($functionname) {
global $wgDebugFunctionEntry, $wgProfiling; global $wgDebugFunctionEntry;
if( !$wgProfiling ) return;
$memory = memory_get_usage(); $memory = memory_get_usage();
$time = $this->getTime(); $time = $this->getTime();
@ -128,12 +149,6 @@ class Profiler {
* called by wfProfileClose() * called by wfProfileClose()
*/ */
public function close() { public function close() {
global $wgProfiling;
# Avoid infinite loop
if( !$wgProfiling )
return;
while( count( $this->mWorkStack ) ){ while( count( $this->mWorkStack ) ){
$this->profileOut( 'close' ); $this->profileOut( 'close' );
} }

View file

@ -4,10 +4,6 @@
* @ingroup Profiler * @ingroup Profiler
*/ */
if ( !class_exists( 'Profiler' ) ) {
require_once( dirname( __FILE__ ) . '/Profiler.php' );
}
/** /**
* Simple profiler base class. * Simple profiler base class.
* @todo document methods (?) * @todo document methods (?)

View file

@ -4,18 +4,13 @@
* @ingroup Profiler * @ingroup Profiler
*/ */
if ( !class_exists( 'ProfilerSimple' ) ) {
require_once( dirname( __FILE__ ) . '/ProfilerSimple.php' );
}
/** /**
* The least sophisticated profiler output class possible, view your source! :) * 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['class'] = 'ProfilerSimpleText';
* $wgProfiler = new ProfilerSimpleText; * $wgProfiler['visible'] = true;
* $wgProfiler->visible=true;
* *
* @ingroup Profiler * @ingroup Profiler
*/ */
@ -23,6 +18,13 @@ class ProfilerSimpleText extends ProfilerSimple {
public $visible=false; /* Show as <PRE> or <!-- ? */ public $visible=false; /* Show as <PRE> or <!-- ? */
static private $out; static private $out;
public function __construct( $profileConfig ) {
if( isset( $profileConfig['visible'] ) && $profileConfig['visible'] ) {
$this->visible = true;
}
parent::__construct();
}
function getFunctionReport() { function getFunctionReport() {
if($this->mTemplated) { if($this->mTemplated) {
uasort($this->mCollated,array('self','sort')); uasort($this->mCollated,array('self','sort'));

View file

@ -4,10 +4,6 @@
* @ingroup Profiler * @ingroup Profiler
*/ */
if ( !class_exists( 'ProfilerSimple' ) ) {
require_once( dirname( __FILE__ ) . '/ProfilerSimple.php' );
}
/** /**
* Execution trace * Execution trace
* @todo document methods (?) * @todo document methods (?)

View file

@ -4,10 +4,6 @@
* @ingroup Profiler * @ingroup Profiler
*/ */
if ( !class_exists( 'ProfilerSimple' ) ) {
require_once( dirname( __FILE__ ) . '/ProfilerSimple.php' );
}
/** /**
* ProfilerSimpleUDP class, that sends out messages for 'udpprofile' daemon * ProfilerSimpleUDP class, that sends out messages for 'udpprofile' daemon
* (the one from mediawiki/trunk/udpprofile SVN ) * (the one from mediawiki/trunk/udpprofile SVN )

View file

@ -4,10 +4,6 @@
* @file * @file
* @ingroup Profiler * @ingroup Profiler
*/ */
if ( !class_exists( 'Profiler' ) ) {
require_once( dirname( __FILE__ ) . '/Profiler.php' );
}
class ProfilerStub extends Profiler { class ProfilerStub extends Profiler {
/** /**
@ -59,7 +55,3 @@ class ProfilerStub extends Profiler {
public function getOutput() {} public function getOutput() {}
public function close() {} public function close() {}
} }
/** backward compatibility */
$wgProfiling = false;
$wgProfiler = new ProfilerStub();

View file

@ -803,7 +803,7 @@ abstract class Maintenance {
*/ */
public function finalSetup() { public function finalSetup() {
global $wgCommandLineMode, $wgShowSQLErrors, $wgServer; global $wgCommandLineMode, $wgShowSQLErrors, $wgServer;
global $wgProfiling, $wgDBadminuser, $wgDBadminpassword; global $wgDBadminuser, $wgDBadminpassword;
global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf; global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
# Turn off output buffering again, it might have been turned on in the settings files # Turn off output buffering again, it might have been turned on in the settings files
@ -848,8 +848,6 @@ abstract class Maintenance {
$wgShowSQLErrors = true; $wgShowSQLErrors = true;
@set_time_limit( 0 ); @set_time_limit( 0 );
$this->adjustMemoryLimit(); $this->adjustMemoryLimit();
$wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
} }
/** /**

View file

@ -69,13 +69,8 @@ if ( !defined( 'MW_COMPILED' ) ) {
require_once( "$IP/includes/Init.php" ); require_once( "$IP/includes/Init.php" );
} }
# Setup the profiler # Stub the profiler
global $IP; require_once( MWInit::compiledPath( 'includes/profiler/Profiler.php' ) );
if ( !defined( 'MW_COMPILED' ) && file_exists( "$IP/StartProfiler.php" ) ) {
require_once( "$IP/StartProfiler.php" );
} else {
require_once( MWInit::compiledPath( 'includes/profiler/ProfilerStub.php' ) );
}
// Some other requires // Some other requires
if ( !defined( 'MW_COMPILED' ) ) { if ( !defined( 'MW_COMPILED' ) ) {