From 46b029e17c478cdc06799b573f91cdc42402e512 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 29 Aug 2016 10:41:59 -0700 Subject: [PATCH] database: Clean up profiling code in DatabaseBase This cuts down on conditionals and ScopedCallback use. Change-Id: Ie478c613b062e45120cdd626f9fb9de09594c638 --- includes/db/Database.php | 23 +++++++++---------- .../includes/db/DatabaseMysqlBaseTest.php | 2 ++ .../includes/db/DatabaseTestHelper.php | 3 +++ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index e07836b225..874e9c4cb5 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -201,6 +201,8 @@ abstract class DatabaseBase implements IDatabase { /** @var int[] Prior mFlags values */ private $priorFlags = []; + /** @var Profiler */ + protected $profiler; /** @var TransactionProfiler */ protected $trxProfiler; @@ -320,10 +322,6 @@ abstract class DatabaseBase implements IDatabase { * @return TransactionProfiler */ protected function getTransactionProfiler() { - if ( !$this->trxProfiler ) { - $this->trxProfiler = new TransactionProfiler(); - } - return $this->trxProfiler; } @@ -583,13 +581,17 @@ abstract class DatabaseBase implements IDatabase { $this->mForeign = $foreign; - if ( isset( $params['trxProfiler'] ) ) { - $this->trxProfiler = $params['trxProfiler']; // override - } + $this->profiler = isset( $params['profiler'] ) + ? $params['profiler'] + : Profiler::instance(); // @TODO: remove global state + $this->trxProfiler = isset( $params['trxProfiler'] ) + ? $params['trxProfiler'] + : new TransactionProfiler(); if ( $user ) { $this->open( $server, $user, $password, $dbName ); } + } /** @@ -939,13 +941,10 @@ abstract class DatabaseBase implements IDatabase { # Include query transaction state $queryProf .= $this->mTrxShortId ? " [TRX#{$this->mTrxShortId}]" : ""; - $profiler = Profiler::instance(); - if ( !( $profiler instanceof ProfilerStub ) ) { - $queryProfSection = $profiler->scopedProfileIn( $queryProf ); - } - $startTime = microtime( true ); + $this->profiler->profileIn( $queryProf ); $ret = $this->doQuery( $commentedSql ); + $this->profiler->profileOut( $queryProf ); $queryRuntime = microtime( true ) - $startTime; unset( $queryProfSection ); // profile out (if set) diff --git a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php index bc7542aded..607f25c36a 100644 --- a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php +++ b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php @@ -31,6 +31,8 @@ class FakeDatabaseMysqlBase extends DatabaseMysqlBase { // From DatabaseBase function __construct() { + $this->profiler = new ProfilerStub( [] ); + $this->trxProfiler = new TransactionProfiler(); } protected function closeConnection() { diff --git a/tests/phpunit/includes/db/DatabaseTestHelper.php b/tests/phpunit/includes/db/DatabaseTestHelper.php index aa8b8e8f0f..d6ca5964b8 100644 --- a/tests/phpunit/includes/db/DatabaseTestHelper.php +++ b/tests/phpunit/includes/db/DatabaseTestHelper.php @@ -27,6 +27,9 @@ class DatabaseTestHelper extends DatabaseBase { public function __construct( $testName ) { $this->testName = $testName; + + $this->profiler = new ProfilerStub( [] ); + $this->trxProfiler = new TransactionProfiler(); } /** -- 2.20.1