From d06e785e79aee626eb90e2227d724a11299a6759 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 17 May 2013 11:48:31 -0400 Subject: [PATCH] Always enable profiling for DB queries. --- yii/db/Command.php | 24 ++++++------------------ yii/db/Connection.php | 34 +++++----------------------------- yii/logging/Logger.php | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 47 deletions(-) diff --git a/yii/db/Command.php b/yii/db/Command.php index a3e2994..e05bde7 100644 --- a/yii/db/Command.php +++ b/yii/db/Command.php @@ -283,22 +283,16 @@ class Command extends \yii\base\Component try { $token = "SQL: $sql"; - if ($this->db->enableProfiling) { - Yii::beginProfile($token, __METHOD__); - } + Yii::beginProfile($token, __METHOD__); $this->prepare(); $this->pdoStatement->execute(); $n = $this->pdoStatement->rowCount(); - if ($this->db->enableProfiling) { - Yii::endProfile($token, __METHOD__); - } + Yii::endProfile($token, __METHOD__); return $n; } catch (\Exception $e) { - if ($this->db->enableProfiling) { - Yii::endProfile($token, __METHOD__); - } + Yii::endProfile($token, __METHOD__); $message = $e->getMessage(); Yii::error("$message\nFailed to execute SQL: $rawSql", __METHOD__); @@ -411,9 +405,7 @@ class Command extends \yii\base\Component try { $token = "SQL: $sql"; - if ($db->enableProfiling) { - Yii::beginProfile($token, __METHOD__); - } + Yii::beginProfile($token, __METHOD__); $this->prepare(); $this->pdoStatement->execute(); @@ -428,9 +420,7 @@ class Command extends \yii\base\Component $this->pdoStatement->closeCursor(); } - if ($db->enableProfiling) { - Yii::endProfile($token, __METHOD__); - } + Yii::endProfile($token, __METHOD__); if (isset($cache, $cacheKey) && $cache instanceof Cache) { $cache->set($cacheKey, $result, $db->queryCacheDuration, $db->queryCacheDependency); @@ -439,9 +429,7 @@ class Command extends \yii\base\Component return $result; } catch (\Exception $e) { - if ($db->enableProfiling) { - Yii::endProfile($token, __METHOD__); - } + Yii::endProfile($token, __METHOD__); $message = $e->getMessage(); Yii::error("$message\nCommand::$method() failed: $rawSql", __METHOD__); $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; diff --git a/yii/db/Connection.php b/yii/db/Connection.php index 5aa222a..064c7ee 100644 --- a/yii/db/Connection.php +++ b/yii/db/Connection.php @@ -215,13 +215,6 @@ class Connection extends Component */ public $emulatePrepare; /** - * @var boolean whether to enable profiling for the SQL statements being executed. - * Defaults to false. This should be mainly enabled and used during development - * to find out the bottleneck of SQL executions. - * @see getStats - */ - public $enableProfiling = false; - /** * @var string the common prefix or suffix for table names. If a table name is given * as `{{%TableName}}`, then the percentage character `%` will be replaced with this * property value. For example, `{{%post}}` becomes `{{tbl_post}}` if this property is @@ -314,12 +307,16 @@ class Connection extends Component if (empty($this->dsn)) { throw new InvalidConfigException('Connection::dsn cannot be empty.'); } + $token = 'Opening DB connection: ' . $this->dsn; try { - Yii::trace('Opening DB connection: ' . $this->dsn, __METHOD__); + Yii::trace($token, __METHOD__); + Yii::beginProfile($token, __METHOD__); $this->pdo = $this->createPdoInstance(); $this->initConnection(); + Yii::endProfile($token, __METHOD__); } catch (\PDOException $e) { + Yii::endProfile($token, __METHOD__); Yii::error("Failed to open DB connection ({$this->dsn}): " . $e->getMessage(), __METHOD__); $message = YII_DEBUG ? 'Failed to open DB connection: ' . $e->getMessage() : 'Failed to open DB connection.'; throw new Exception($message, $e->errorInfo, (int)$e->getCode()); @@ -541,25 +538,4 @@ class Connection extends Component return strtolower($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME)); } } - - /** - * Returns the statistical results of SQL queries. - * The results returned include the number of SQL statements executed and - * the total time spent. - * In order to use this method, [[enableProfiling]] has to be set true. - * @return array the first element indicates the number of SQL statements executed, - * and the second element the total time spent in SQL execution. - * @see \yii\logging\Logger::getProfiling() - */ - public function getQuerySummary() - { - $logger = Yii::getLogger(); - $timings = $logger->getProfiling(array('yii\db\Command::query', 'yii\db\Command::execute')); - $count = count($timings); - $time = 0; - foreach ($timings as $timing) { - $time += $timing[1]; - } - return array($count, $time); - } } diff --git a/yii/logging/Logger.php b/yii/logging/Logger.php index 788d946..4bd6bcc 100644 --- a/yii/logging/Logger.php +++ b/yii/logging/Logger.php @@ -225,6 +225,24 @@ class Logger extends Component return array_values($timings); } + /** + * Returns the statistical results of DB queries. + * The results returned include the number of SQL statements executed and + * the total time spent. + * @return array the first element indicates the number of SQL statements executed, + * and the second element the total time spent in SQL execution. + */ + public function getDbProfiling() + { + $timings = $this->getProfiling(array('yii\db\Command::query', 'yii\db\Command::execute')); + $count = count($timings); + $time = 0; + foreach ($timings as $timing) { + $time += $timing[1]; + } + return array($count, $time); + } + private function calculateTimings() { $timings = array();