database: Clean up profiling code in DatabaseBase
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 29 Aug 2016 17:41:59 +0000 (10:41 -0700)
committerKrinkle <krinklemail@gmail.com>
Mon, 29 Aug 2016 22:13:03 +0000 (22:13 +0000)
This cuts down on conditionals and ScopedCallback use.

Change-Id: Ie478c613b062e45120cdd626f9fb9de09594c638

includes/db/Database.php
tests/phpunit/includes/db/DatabaseMysqlBaseTest.php
tests/phpunit/includes/db/DatabaseTestHelper.php

index e07836b..874e9c4 100644 (file)
@@ -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)
index bc7542a..607f25c 100644 (file)
@@ -31,6 +31,8 @@
 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
        // From DatabaseBase
        function __construct() {
+               $this->profiler = new ProfilerStub( [] );
+               $this->trxProfiler = new TransactionProfiler();
        }
 
        protected function closeConnection() {
index aa8b8e8..d6ca596 100644 (file)
@@ -27,6 +27,9 @@ class DatabaseTestHelper extends DatabaseBase {
 
        public function __construct( $testName ) {
                $this->testName = $testName;
+
+               $this->profiler = new ProfilerStub( [] );
+               $this->trxProfiler = new TransactionProfiler();
        }
 
        /**