From b438db9e2c45fcfa923d8f0ae19cfab5cf583457 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 13 Mar 2019 11:11:18 -0700 Subject: [PATCH] rdbms: change "profiler" argument in Database::factory so it works again The Profiler::profileIn and Profiler::profileOut methods are just stubs. Use a callback to the Profiler::scopedProfileIn method instead. Change-Id: I16068bce583bb880250fe91235f2283453be5e4c --- includes/db/MWLBFactory.php | 4 +++- includes/libs/rdbms/database/Database.php | 21 +++++++------------ .../includes/db/DatabaseTestHelper.php | 2 +- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/includes/db/MWLBFactory.php b/includes/db/MWLBFactory.php index e50f855edb..cb1a69dfd4 100644 --- a/includes/db/MWLBFactory.php +++ b/includes/db/MWLBFactory.php @@ -54,7 +54,9 @@ abstract class MWLBFactory { $mainConfig->get( 'DBmwschema' ), $mainConfig->get( 'DBprefix' ) ), - 'profiler' => Profiler::instance(), + 'profiler' => function ( $section ) { + return Profiler::instance()->scopedProfileIn( $section ); + }, 'trxProfiler' => Profiler::instance()->getTransactionProfiler(), 'replLogger' => LoggerFactory::getInstance( 'DBReplication' ), 'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ), diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index 49b2792210..1600c26ee3 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -260,7 +260,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware /** @var int[] Prior flags member variable values */ private $priorFlags = []; - /** @var mixed Class name or object With profileIn/profileOut methods */ + /** @var callable|null */ protected $profiler; /** @var TransactionProfiler */ protected $trxProfiler; @@ -308,7 +308,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware $this->srvCache = $params['srvCache'] ?? new HashBagOStuff(); - $this->profiler = $params['profiler']; + $this->profiler = is_callable( $params['profiler'] ) ? $params['profiler'] : null; $this->trxProfiler = $params['trxProfiler']; $this->connLogger = $params['connLogger']; $this->queryLogger = $params['queryLogger']; @@ -408,9 +408,10 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware * used to adjust lock timeouts or encoding modes and the like. * - connLogger: Optional PSR-3 logger interface instance. * - queryLogger: Optional PSR-3 logger interface instance. - * - profiler: Optional class name or object with profileIn()/profileOut() methods. - * These will be called in query(), using a simplified version of the SQL that also - * includes the agent as a SQL comment. + * - profiler : Optional callback that takes a section name argument and returns + * a ScopedCallback instance that ends the profile section in its destructor. + * These will be called in query(), using a simplified version of the SQL that + * also includes the agent as a SQL comment. * - trxProfiler: Optional TransactionProfiler instance. * - errorLogger: Optional callback that takes an Exception and logs it. * - deprecationLogger: Optional callback that takes a string and logs it. @@ -1272,19 +1273,13 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware $queryProf .= $this->trxShortId ? " [TRX#{$this->trxShortId}]" : ""; $startTime = microtime( true ); - if ( $this->profiler ) { - $this->profiler->profileIn( $queryProf ); - } + $ps = $this->profiler ? ( $this->profiler )( $queryProf ) : null; $this->affectedRowCount = null; $ret = $this->doQuery( $commentedSql ); $this->affectedRowCount = $this->affectedRows(); - if ( $this->profiler ) { - $this->profiler->profileOut( $queryProf ); - } + unset( $ps ); // profile out (if set) $queryRuntime = max( microtime( true ) - $startTime, 0.0 ); - unset( $queryProfSection ); // profile out (if set) - if ( $ret !== false ) { $this->lastPing = $startTime; if ( $isWrite && $this->trxLevel ) { diff --git a/tests/phpunit/includes/db/DatabaseTestHelper.php b/tests/phpunit/includes/db/DatabaseTestHelper.php index 65b82abf0f..ff10fef962 100644 --- a/tests/phpunit/includes/db/DatabaseTestHelper.php +++ b/tests/phpunit/includes/db/DatabaseTestHelper.php @@ -45,7 +45,7 @@ class DatabaseTestHelper extends Database { public function __construct( $testName, array $opts = [] ) { $this->testName = $testName; - $this->profiler = new ProfilerStub( [] ); + $this->profiler = null; $this->trxProfiler = new TransactionProfiler(); $this->cliMode = $opts['cliMode'] ?? true; $this->connLogger = new \Psr\Log\NullLogger(); -- 2.20.1