Commit 97c09d9f authored by Alexander Butenko's avatar Alexander Butenko

Query execution tracing functions added

parent 6f810c66
...@@ -112,6 +112,15 @@ class MysqliDb ...@@ -112,6 +112,15 @@ class MysqliDb
*/ */
protected $isSubQuery = false; protected $isSubQuery = false;
/**
* Variables for query execution tracing
*
*/
protected $traceStartQ;
protected $traceEnabled;
protected $traceStripPrefix;
public $trace = array();
/** /**
* @param string $host * @param string $host
* @param string $username * @param string $username
...@@ -192,6 +201,9 @@ class MysqliDb ...@@ -192,6 +201,9 @@ class MysqliDb
*/ */
protected function reset() protected function reset()
{ {
if ($this->traceEnabled)
$this->trace[] = array ($this->_lastQuery, (microtime(true) - $this->traceStartQ) , $this->_traceGetCaller());
$this->_where = array(); $this->_where = array();
$this->_join = array(); $this->_join = array();
$this->_orderBy = array(); $this->_orderBy = array();
...@@ -968,6 +980,9 @@ class MysqliDb ...@@ -968,6 +980,9 @@ class MysqliDb
if (!$stmt = $this->_mysqli->prepare($this->_query)) { if (!$stmt = $this->_mysqli->prepare($this->_query)) {
trigger_error("Problem preparing query ($this->_query) " . $this->_mysqli->error, E_USER_ERROR); trigger_error("Problem preparing query ($this->_query) " . $this->_mysqli->error, E_USER_ERROR);
} }
if ($this->traceEnabled)
$this->traceStartQ = microtime (true);
return $stmt; return $stmt;
} }
...@@ -1204,5 +1219,31 @@ class MysqliDb ...@@ -1204,5 +1219,31 @@ class MysqliDb
return; return;
$this->rollback (); $this->rollback ();
} }
/**
* Query exection time tracking switch
*
* @param bool $enabled Enable execution time tracking
* @param string $stripPrefix Prefix to strip from the path in exec log
**/
public function setTrace ($enabled, $stripPrefix = null) {
$this->traceEnabled = $enabled;
$this->traceStripPrefix = $stripPrefix;
return $this;
}
/**
* Get where and what function was called for query stored in MysqliDB->trace
*
* @return string with information
*/
private function _traceGetCaller () {
$dd = debug_backtrace ();
$caller = next ($dd);
while (isset ($caller) && $caller["file"] == __FILE__ )
$caller = next($dd);
return __CLASS__ . "->" . $caller["function"] . "() >> file \"" .
str_replace ($this->traceStripPrefix, '', $caller["file"] ) . "\" line #" . $caller["line"] . " " ;
}
} // END class } // END class
?> ?>
...@@ -474,3 +474,31 @@ if (!$db->insert ('myTable', $insertData)) { ...@@ -474,3 +474,31 @@ if (!$db->insert ('myTable', $insertData)) {
$db->commit(); $db->commit();
} }
``` ```
### Query exectution time benchmarking
To track query execution time setTrace() function should be called.
```php
$db->setTrace (true);
// As a second parameter it is possible to define prefix of the path which should be striped from filename
// $db->setTrace (true, $_SERVER['SERVER_ROOT']);
$db->get("users");
$db->get("test");
print_r ($db->trace);
```
```
[0] => Array
(
[0] => SELECT * FROM t_users ORDER BY `id` ASC
[1] => 0.0010669231414795
[2] => MysqliDb->get() >> file "/avb/work/PHP-MySQLi-Database-Class/tests.php" line #151
)
[1] => Array
(
[0] => SELECT * FROM t_test
[1] => 0.00069189071655273
[2] => MysqliDb->get() >> file "/avb/work/PHP-MySQLi-Database-Class/tests.php" line #152
)
```
...@@ -5,6 +5,9 @@ error_reporting(E_ALL); ...@@ -5,6 +5,9 @@ error_reporting(E_ALL);
$db = new Mysqlidb('localhost', 'root', '', 'testdb'); $db = new Mysqlidb('localhost', 'root', '', 'testdb');
if(!$db) die("Database error"); if(!$db) die("Database error");
$mysqli = new mysqli ('localhost', 'root', '', 'testdb');
$db = new Mysqlidb($mysqli);
$db = new Mysqlidb(Array ( $db = new Mysqlidb(Array (
'host' => 'localhost', 'host' => 'localhost',
'username' => 'root', 'username' => 'root',
...@@ -13,11 +16,10 @@ $db = new Mysqlidb(Array ( ...@@ -13,11 +16,10 @@ $db = new Mysqlidb(Array (
'charset' => null)); 'charset' => null));
if(!$db) die("Database error"); if(!$db) die("Database error");
$mysqli = new mysqli ('localhost', 'root', '', 'testdb');
$db = new Mysqlidb($mysqli);
$prefix = 't_'; $prefix = 't_';
$db->setPrefix($prefix); $db->setPrefix($prefix);
$db->setTrace(true);
$tables = Array ( $tables = Array (
'users' => Array ( 'users' => Array (
...@@ -360,4 +362,5 @@ echo "All done"; ...@@ -360,4 +362,5 @@ echo "All done";
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test"))); //print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
print_r ($db->trace);
?> ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment