Added a simple pendingWriteQueryDuration() DB method
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 22 Apr 2015 22:15:36 +0000 (15:15 -0700)
committerOri.livneh <ori@wikimedia.org>
Thu, 23 Apr 2015 03:13:40 +0000 (03:13 +0000)
Change-Id: I924cf78d8bb96d526a7ba7444f0532d7eb223bf2

includes/db/Database.php

index 0bef28a..8c1ebf9 100644 (file)
@@ -141,6 +141,13 @@ abstract class DatabaseBase implements IDatabase {
         */
        private $mTrxAutomaticAtomic = false;
 
+       /**
+        * Track the seconds spent in write queries for the current transaction
+        *
+        * @var float
+        */
+       private $mTrxWriteDuration = 0.0;
+
        /**
         * @since 1.21
         * @var resource File handle for upgrade
@@ -473,6 +480,18 @@ abstract class DatabaseBase implements IDatabase {
                );
        }
 
+       /**
+        * Get the time spend running write queries for this
+        *
+        * High times could be due to scanning, updates, locking, and such
+        *
+        * @return float|bool Returns false if not transaction is active
+        * @since 1.26
+        */
+       public function pendingWriteQueryDuration() {
+               return $this->mTrxLevel ? $this->mTrxWriteDuration : false;
+       }
+
        /**
         * Is a connection to the database open?
         * @return bool
@@ -1160,6 +1179,7 @@ abstract class DatabaseBase implements IDatabase {
                # Do the query and handle errors
                $startTime = microtime( true );
                $ret = $this->doQuery( $commentedSql );
+               $queryRuntime = microtime( true ) - $startTime;
                # Log the query time and feed it into the DB trx profiler
                $this->getTransactionProfiler()->recordQueryCompletion(
                        $queryProf, $startTime, $isWriteQuery, $this->affectedRows() );
@@ -1191,6 +1211,7 @@ abstract class DatabaseBase implements IDatabase {
                                        # Should be safe to silently retry (no trx and thus no callbacks)
                                        $startTime = microtime( true );
                                        $ret = $this->doQuery( $commentedSql );
+                                       $queryRuntime = microtime( true ) - $startTime;
                                        # Log the query time and feed it into the DB trx profiler
                                        $this->getTransactionProfiler()->recordQueryCompletion(
                                                $queryProf, $startTime, $isWriteQuery, $this->affectedRows() );
@@ -1211,6 +1232,10 @@ abstract class DatabaseBase implements IDatabase {
                $queryProfSection = false;
                $totalProfSection = false;
 
+               if ( $isWriteQuery && $this->mTrxLevel ) {
+                       $this->mTrxWriteDuration += $queryRuntime;
+               }
+
                return $res;
        }
 
@@ -3633,6 +3658,7 @@ abstract class DatabaseBase implements IDatabase {
                $this->mTrxIdleCallbacks = array();
                $this->mTrxPreCommitCallbacks = array();
                $this->mTrxShortId = wfRandomString( 12 );
+               $this->mTrxWriteDuration = 0.0;
        }
 
        /**