Skip to content

Instantly share code, notes, and snippets.

@ydenyshchenk
Last active February 2, 2017 13:45
Show Gist options
  • Save ydenyshchenk/fbc7134528b7cb888117ffd1037c545c to your computer and use it in GitHub Desktop.
Save ydenyshchenk/fbc7134528b7cb888117ffd1037c545c to your computer and use it in GitHub Desktop.
--- vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php 2015-10-29 16:34:55.000000000 +0200
+++ vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php 2016-12-29 01:24:41.000000000 +0200
@@ -220,6 +220,23 @@
*/
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' => \Magento\Framework\Debug::backtrace(true, false),
+ 'tt' => 0
+ );
+ }
+
if (empty($bind) && $sql instanceof Zend_Db_Select) {
$bind = $sql->getBind();
}
@@ -235,7 +252,17 @@
}
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
--- index.php 2017-01-05 11:52:59.000000000 +0200
+++ index.php 2016-12-31 01:30:17.000000000 +0200
@@ -33,7 +33,50 @@
exit(1);
}
+$totalLoadTimeStart = microtime(true);
+
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);
+
+$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);
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment