Browse Source

Always enable profiling for DB queries.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
d06e785e79
  1. 12
      yii/db/Command.php
  2. 34
      yii/db/Connection.php
  3. 18
      yii/logging/Logger.php

12
yii/db/Command.php

@ -283,22 +283,16 @@ class Command extends \yii\base\Component
try { try {
$token = "SQL: $sql"; $token = "SQL: $sql";
if ($this->db->enableProfiling) {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
}
$this->prepare(); $this->prepare();
$this->pdoStatement->execute(); $this->pdoStatement->execute();
$n = $this->pdoStatement->rowCount(); $n = $this->pdoStatement->rowCount();
if ($this->db->enableProfiling) {
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
}
return $n; return $n;
} catch (\Exception $e) { } catch (\Exception $e) {
if ($this->db->enableProfiling) {
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
}
$message = $e->getMessage(); $message = $e->getMessage();
Yii::error("$message\nFailed to execute SQL: $rawSql", __METHOD__); Yii::error("$message\nFailed to execute SQL: $rawSql", __METHOD__);
@ -411,9 +405,7 @@ class Command extends \yii\base\Component
try { try {
$token = "SQL: $sql"; $token = "SQL: $sql";
if ($db->enableProfiling) {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
}
$this->prepare(); $this->prepare();
$this->pdoStatement->execute(); $this->pdoStatement->execute();
@ -428,9 +420,7 @@ class Command extends \yii\base\Component
$this->pdoStatement->closeCursor(); $this->pdoStatement->closeCursor();
} }
if ($db->enableProfiling) {
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
}
if (isset($cache, $cacheKey) && $cache instanceof Cache) { if (isset($cache, $cacheKey) && $cache instanceof Cache) {
$cache->set($cacheKey, $result, $db->queryCacheDuration, $db->queryCacheDependency); $cache->set($cacheKey, $result, $db->queryCacheDuration, $db->queryCacheDependency);
@ -439,9 +429,7 @@ class Command extends \yii\base\Component
return $result; return $result;
} catch (\Exception $e) { } catch (\Exception $e) {
if ($db->enableProfiling) {
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
}
$message = $e->getMessage(); $message = $e->getMessage();
Yii::error("$message\nCommand::$method() failed: $rawSql", __METHOD__); Yii::error("$message\nCommand::$method() failed: $rawSql", __METHOD__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;

34
yii/db/Connection.php

@ -215,13 +215,6 @@ class Connection extends Component
*/ */
public $emulatePrepare; 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 * @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 * as `{{%TableName}}`, then the percentage character `%` will be replaced with this
* property value. For example, `{{%post}}` becomes `{{tbl_post}}` if this property is * property value. For example, `{{%post}}` becomes `{{tbl_post}}` if this property is
@ -314,12 +307,16 @@ class Connection extends Component
if (empty($this->dsn)) { if (empty($this->dsn)) {
throw new InvalidConfigException('Connection::dsn cannot be empty.'); throw new InvalidConfigException('Connection::dsn cannot be empty.');
} }
$token = 'Opening DB connection: ' . $this->dsn;
try { try {
Yii::trace('Opening DB connection: ' . $this->dsn, __METHOD__); Yii::trace($token, __METHOD__);
Yii::beginProfile($token, __METHOD__);
$this->pdo = $this->createPdoInstance(); $this->pdo = $this->createPdoInstance();
$this->initConnection(); $this->initConnection();
Yii::endProfile($token, __METHOD__);
} }
catch (\PDOException $e) { catch (\PDOException $e) {
Yii::endProfile($token, __METHOD__);
Yii::error("Failed to open DB connection ({$this->dsn}): " . $e->getMessage(), __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.'; $message = YII_DEBUG ? 'Failed to open DB connection: ' . $e->getMessage() : 'Failed to open DB connection.';
throw new Exception($message, $e->errorInfo, (int)$e->getCode()); 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)); 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);
}
} }

18
yii/logging/Logger.php

@ -225,6 +225,24 @@ class Logger extends Component
return array_values($timings); 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() private function calculateTimings()
{ {
$timings = array(); $timings = array();

Loading…
Cancel
Save