From 4dd8b5cf45c9e1ffe9c6d17ecccbe125bf42c1bf Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 22 Apr 2015 15:15:36 -0700 Subject: [PATCH] Added a simple pendingWriteQueryDuration() DB method Change-Id: I924cf78d8bb96d526a7ba7444f0532d7eb223bf2 --- includes/db/Database.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/includes/db/Database.php b/includes/db/Database.php index 0bef28a1a8..8c1ebf91cc 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -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; } /** -- 2.20.1