Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ydenyshchenk/f79977269c3b9df44eef47170438870c to your computer and use it in GitHub Desktop.
Save ydenyshchenk/f79977269c3b9df44eef47170438870c to your computer and use it in GitHub Desktop.
diff --git a/index.php b/index.php
index bec6b29..fdce413 100644
--- a/index.php
+++ b/index.php
@@ -84,4 +84,46 @@ $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : ''
/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
+$totalLoadTimeStart = microtime(true);
Mage::run($mageRunCode, $mageRunType);
+
+$totalLoadTimeFinish = microtime(true);
+$totalLoadTime = $totalLoadTimeFinish - $totalLoadTimeStart;
+$SKIP_SINGLE_QUERIES = 1;
+$SKIP_TRACE = 0;
+if (isset($allDbQueries) && is_array($allDbQueries)) {
+ $topQueries = array();
+ $allDbQueriesTotalLoadTime = 0;
+ $URL = isset($_SERVER['REQUEST_URI']) ? 'URL: ' . $_SERVER['REQUEST_URI'] . ' ' : '';
+ foreach ($allDbQueries as $hash => $q) {
+ $allDbQueriesTotalLoadTime += (float)$q['tt'];
+ if ($q['c'] == 1 && $SKIP_SINGLE_QUERIES) {
+ continue;
+ }
+ $timeIndex = (int)(1000 * (float)$q['tt']);
+ $query = trim($q['q'], " \t\n\r\0\x0B;");
+ $time = isset($q['t']) ? ' - time: ' . sprintf('%f', $q['t']) . 's / total time: ' . (float)$q['tt'] : '';
+ $topQueries[$timeIndex . '_' . $hash] = "[{$q['c']}x{$time}]: {$query}; "
+ . ((isset($q['b']) && $q['b']) ? "bind: {$q['b']}" : '')
+ . (!$SKIP_TRACE && (isset($q['tr']) && $q['tr']) ? "\nTRACE:\n" . $q['tr'] : '');
+ }
+
+ krsort($topQueries);
+
+ $log = '========= ' . $URL . date("Y-m-d H:i:s") . ' count: ' . count($allDbQueries, 0) . " =========\n";
+ $log .= "========= all DB queries execution time: $allDbQueriesTotalLoadTime / total run time: $totalLoadTime =========\n";
+ if ($topQueries) {
+ $log .= implode("\n\n", $topQueries) . "\n\n";
+ }
+}
+
+$logFileDir = 'var/debug';
+if (!file_exists($logFileDir)) {
+ mkdir($logFileDir, 0777);
+ }
+$logFile = $logFileDir . DIRECTORY_SEPARATOR . 'queries.'. time() .'.log';
+if (is_writeable($logFileDir)) {
+ error_log($log, 3, $logFile);
+ } else {
+ error_log($log, 0);
+ }
\ No newline at end of file
diff --git a/lib/Zend/Db/Adapter/Pdo/Abstract.php b/lib/Zend/Db/Adapter/Pdo/Abstract.php
index ead0a78..76aadc5 100644
--- a/lib/Zend/Db/Adapter/Pdo/Abstract.php
+++ b/lib/Zend/Db/Adapter/Pdo/Abstract.php
@@ -220,6 +220,23 @@ abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
*/
public function query($sql, $bind = array())
{
+ //DEBUG
+ global $allDbQueries;
+ $bindJson = $bind ? json_encode($bind) : '';
+
+ $sqlHash = sha1($sql . $bindJson);
+ if (isset($allDbQueries[$sqlHash])) {
+ ++$allDbQueries[$sqlHash]['c'];
+ } else {
+ $allDbQueries[$sqlHash] = array(
+ 'c' => 1,
+ 'q' => $sql,
+ 'b' => $bindJson,
+ 'tr' => Varien_Debug::backtrace(true, false),
+ 'tt' => 0
+ );
+ }
+
if (empty($bind) && $sql instanceof Zend_Db_Select) {
$bind = $sql->getBind();
}
@@ -235,7 +252,17 @@ abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
}
try {
- return parent::query($sql, $bind);
+ $sqlTime = microtime(true);
+
+ $result = parent::query($sql, $bind);
+ $queryTime = microtime(true) - $sqlTime;
+ $allDbQueries[$sqlHash]['tt'] += $queryTime;
+
+ if ($allDbQueries[$sqlHash]['c'] == 1) {
+ $allDbQueries[$sqlHash]['t'] = $queryTime;
+ }
+
+ return $result;
} catch (PDOException $e) {
/**
* @see Zend_Db_Statement_Exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment