// Improve Db class + add _db.php class in overrides for bench

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@7678 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
rMalie
2011-07-21 12:02:25 +00:00
parent 884e95f307
commit ff2df3e3b3
5 changed files with 135 additions and 242 deletions
+20 -7
View File
@@ -39,9 +39,6 @@ abstract class DbCore
/** @var string Database password (eg. can be empty !) */
protected $_password;
/** @var string Database type (MySQL, PgSQL) */
protected $_type;
/** @var string Database name */
protected $_database;
@@ -61,8 +58,19 @@ abstract class DbCore
// array('server' => '192.168.0.15', 'user' => 'rep', 'password' => '123456', 'database' => 'rep'),
// array('server' => '192.168.0.3', 'user' => 'myuser', 'password' => 'mypassword', 'database' => 'mydatabase'),
);
/**
* Store last executed query
*
* @var string
*/
protected $_lastQuery;
/**
* Last cached query
*
* @var string
*/
protected $_lastCached;
/**
@@ -161,8 +169,13 @@ abstract class DbCore
}
if (!isset(self::$_instance[$idServer]))
self::$_instance[$idServer] = new MySQL(self::$_servers[$idServer]['server'], self::$_servers[$idServer]['user'], self::$_servers[$idServer]['password'], self::$_servers[$idServer]['database']);
{
$class = _DB_TYPE_;
if (!class_exists($class))
$class = 'MySQL';
self::$_instance[$idServer] = new $class(self::$_servers[$idServer]['server'], self::$_servers[$idServer]['user'], self::$_servers[$idServer]['password'], self::$_servers[$idServer]['database']);
}
return self::$_instance[$idServer];
}
@@ -419,7 +432,7 @@ abstract class DbCore
$this->_lastQuery = $sql;
if ($use_cache AND _PS_CACHE_ENABLED_)
Cache::getInstance()->deleteQuery($sql);
return $result;
return $result;
}
/**
+1 -3
View File
@@ -53,9 +53,7 @@ class MySQLCore extends Db
*/
public function disconnect()
{
if ($this->_link)
@mysql_close($this->_link);
$this->_link = false;
mysql_close($this->_link);
}
/**
+102
View File
@@ -0,0 +1,102 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision$
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
abstract class Db extends DbCore
{
/**
* Add SQL_NO_CACHE in SELECT queries
*
* @var unknown_type
*/
public $disableCache = true;
/**
* Total of queries
*
* @var int
*/
public $count = 0;
/**
* List of queries
*
* @var array
*/
public $queries = array();
/**
* List of uniq queries (replace numbers by XX)
*
* @var array
*/
public $uniqQueries = array();
/**
* List of tables
*
* @var array
*/
public $tables = array();
/**
* Execute the query and log some informations
*
* @see DbCore::query()
*/
public function query($sql)
{
$uniqSql = preg_replace('/[0-9]+/', '<span style="color:blue">XX</span>', $sql);
if (!isset($this->uniqQueries[$uniqSql]))
$this->uniqQueries[$uniqSql] = 0;
$this->uniqQueries[$uniqSql]++;
// No cache for query
if ($this->disableCache)
$sql = preg_replace('/^select /i', 'SELECT SQL_NO_CACHE ', trim($sql));
// Get tables in quer
preg_match_all('/(from|join)\s+`?'._DB_PREFIX_.'([a-z0-9_-]+)/ui', $sql, $matches);
foreach ($matches[2] as $table)
{
if (!isset($this->tables[$table]))
$this->tables[$table] = 0;
$this->tables[$table]++;
}
// Execute query
$start = microtime(true);
$result = parent::query($sql);
$end = microtime(true);
$this->queries[] = array(
'query' => $sql,
'time' => ($end - $start)
);
return $result;
}
}
+12 -19
View File
@@ -243,9 +243,9 @@ class FrontController extends FrontControllerCore
$totalSize += filesize($file);
$totalQueryTime = 0;
foreach (Db::getInstance()->queriesTime as $time)
$totalQueryTime += $time;
foreach (Db::getInstance()->queries as $data)
$totalQueryTime += $data['time'];
$hooktime = Module::getHookTime();
arsort($hooktime);
$totalHookTime = 0;
@@ -302,17 +302,11 @@ class FrontController extends FrontControllerCore
<li>displayContent: '.$this->displayMemoryColor(($this->_memory[5] - $this->_memory[4])).'</li><li>displayFooter: '.$this->displayMemoryColor(($this->_memory[6] - $this->_memory[5])).'</li>
</ul>';
echo '</div>';
$countByTypes = '';
foreach (Db::getInstance()->countTypes as $type => $count)
if ($count)
$countByTypes .= '<li>'.$count.' x '.$type.'</li>';
$countByTypes = rtrim($countByTypes, ' |');
echo '
<div class="rte" style="text-align:left;padding:8px;float:left;margin-left:20px">
<b>SQL Queries</b>: '.$this->displaySQLQueries(Db::getInstance()->count).'
<ul>'.$countByTypes.'</ul>
<b>DB type</b>: '.get_class(Db::getInstance()).'
<br /><b>SQL Queries</b>: '.$this->displaySQLQueries(count(Db::getInstance()->queries)).'
<br /><b>Time spent querying</b>: '.$this->displayLoadTimeColor($totalQueryTime).'
</div>
<div class="rte" style="text-align:left;padding:8px;float:left;margin-left:20px">
@@ -336,15 +330,14 @@ class FrontController extends FrontControllerCore
</ul>
</div>
<div class="rte" style="text-align:left;padding:8px">
<h3><a name="stopwatch">Stopwatch (with SQL_NO_CACHE)</a></h3>';
$queries = Db::getInstance()->queriesTime;
arsort($queries);
foreach ($queries as $q => $time)
echo $hr.'<b '.$this->getTimeColor($time * 1000).'>'.round($time * 1000, 3).' ms</b> '.$q;
<h3><a name="stopwatch">Stopwatch (with SQL_NO_CACHE) (total = '.count(Db::getInstance()->queries).')</a></h3>';
$queries = Db::getInstance()->queries;
foreach ($queries as $data)
echo $hr.'<b '.$this->getTimeColor($data['time'] * 1000).'>'.round($data['time'] * 1000, 3).' ms</b> '.$data['query'];
echo '</div>
<div class="rte" style="text-align:left;padding:8px">
<h3><a name="doubles">Doubles (IDs replaced by "XX")</a></h3>';
$queries = Db::getInstance()->queries;
<h3><a name="doubles">Doubles (IDs replaced by "XX") (total = '.count(Db::getInstance()->uniqQueries).')</a></h3>';
$queries = Db::getInstance()->uniqQueries;
arsort($queries);
foreach ($queries as $q => $nb)
echo $hr.'<b '.$this->getQueryColor($nb).'>'.$nb.'</b> '.$q;
-213
View File
@@ -1,213 +0,0 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 6844 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
//
// IMPORTANT : don't forget to delete the underscore _ in the file name if you want to use it !
//
class MySQL extends MySQLCore
{
public $count = 0;
public $queries = array();
public $queriesTime = array();
public $tables = array();
public $countTypes = array('getRow' => 0, 'getValue' => 0, 'Execute' => 0, 'ExecuteS' => 0, 'delete' => 0, 'q' => 0);
private function disableCache($query)
{
return preg_replace('/^select /', 'SELECT SQL_NO_CACHE', trim($query));
}
public function getRow($query, $use_cache = 1)
{
$this->count++;
$this->countTypes['getRow']++;
$query2 = preg_replace('/[0-9]+/', '<span style="color:blue">XX</span>', $query);
if (!isset($this->queries[$query2]))
$this->queries[$query2] = 0;
$this->queries[$query2]++;
preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
foreach ($matches[2] as $table)
{
if (!isset($this->tables[$table]))
$this->tables[$table] = 0;
$this->tables[$table]++;
}
$query = $this->disableCache($query);
$t0 = microtime(true);
$return = parent::getRow($query, $use_cache);
if (!isset($this->queriesTime[$query]))
$this->queriesTime[$query] = microtime(true)-$t0;
return $return;
}
public function getValue($query, $use_cache = 1)
{
$this->count++;
$this->countTypes['getValue']++;
$query2 = preg_replace('/[0-9]+/', '<span style="color:blue">XX</span>', $query);
if (!isset($this->queries[$query2]))
$this->queries[$query2] = 0;
$this->queries[$query2]++;
preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
foreach ($matches[2] as $table)
{
if (!isset($this->tables[$table]))
$this->tables[$table] = 0;
$this->tables[$table]++;
}
$query = $this->disableCache($query);
$t0 = microtime(true);
$return = parent::getValue($query, $use_cache);
if (!isset($this->queriesTime[$query]))
$this->queriesTime[$query] = microtime(true)-$t0;
return $return;
}
public function Execute($query, $use_cache = 1)
{
$this->count++;
$this->countTypes['Execute']++;
$query2 = preg_replace('/[0-9]+/', '<span style="color:blue">XX</span>', $query);
if (!isset($this->queries[$query2]))
$this->queries[$query2] = 0;
$this->queries[$query2]++;
preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
foreach ($matches[2] as $table)
{
if (!isset($this->tables[$table]))
$this->tables[$table] = 0;
$this->tables[$table]++;
}
$query = $this->disableCache($query);
$t0 = microtime(true);
$return = parent::Execute($query, $use_cache);
if (!isset($this->queriesTime[$query]))
$this->queriesTime[$query] = microtime(true)-$t0;
return $return;
}
public function ExecuteS($query, $array = true, $use_cache = 1)
{
$this->count++;
$this->countTypes['ExecuteS']++;
$query2 = preg_replace('/[0-9]+/', '<span style="color:blue">XX</span>', $query);
if (!isset($this->queries[$query2]))
$this->queries[$query2] = 0;
$this->queries[$query2]++;
preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
foreach ($matches[2] as $table)
{
if (!isset($this->tables[$table]))
$this->tables[$table] = 0;
$this->tables[$table]++;
}
$query = $this->disableCache($query);
$t0 = microtime(true);
$return = parent::ExecuteS($query, $array, $use_cache);
if (!isset($this->queriesTime[$query]))
$this->queriesTime[$query] = microtime(true)-$t0;
return $return;
}
public function delete($table, $where = false, $limit = false, $use_cache = 1)
{
$this->_result = false;
if ($this->_link)
{
$query = 'DELETE FROM `'.pSQL($table).'`'.($where ? ' WHERE '.$where : '').($limit ? ' LIMIT '.(int)($limit) : '');
$this->count++;
$this->countTypes['delete']++;
$query2 = preg_replace('/[0-9]+/', '<span style="color:blue">XX</span>', $query);
if (!isset($this->queries[$query2]))
$this->queries[$query2] = 0;
$this->queries[$query2]++;
preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
foreach ($matches[2] as $table)
{
if (!isset($this->tables[$table]))
$this->tables[$table] = 0;
$this->tables[$table]++;
}
$query = $this->disableCache($query);
$t0 = microtime(true);
$return = parent::delete($table, $where, $limit, $use_cache);
if (!isset($this->queriesTime[$query]))
$this->queriesTime[$query] = microtime(true)-$t0;
return $return;
}
return false;
}
protected function q($query, $use_cache = 1)
{
$this->count++;
$this->countTypes['q']++;
$query2 = preg_replace('/[0-9]+/', '<span style="color:blue">XX</span>', $query);
if (!isset($this->queries[$query2]))
$this->queries[$query2] = 0;
$this->queries[$query2]++;
preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
foreach ($matches[2] as $table)
{
if (!isset($this->tables[$table]))
$this->tables[$table] = 0;
$this->tables[$table]++;
}
$query = $this->disableCache($query);
$t0 = microtime(true);
$return = parent::q($query, $use_cache);
if (!isset($this->queriesTime[$query]))
$this->queriesTime[$query] = microtime(true)-$t0;
return $return;
}
}