Skip to content

Commit 97c09d9

Browse files
committed
Query execution tracing functions added
1 parent 6f810c6 commit 97c09d9

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

MysqliDb.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ class MysqliDb
112112
*/
113113
protected $isSubQuery = false;
114114

115+
/**
116+
* Variables for query execution tracing
117+
*
118+
*/
119+
protected $traceStartQ;
120+
protected $traceEnabled;
121+
protected $traceStripPrefix;
122+
public $trace = array();
123+
115124
/**
116125
* @param string $host
117126
* @param string $username
@@ -192,6 +201,9 @@ public static function getInstance()
192201
*/
193202
protected function reset()
194203
{
204+
if ($this->traceEnabled)
205+
$this->trace[] = array ($this->_lastQuery, (microtime(true) - $this->traceStartQ) , $this->_traceGetCaller());
206+
195207
$this->_where = array();
196208
$this->_join = array();
197209
$this->_orderBy = array();
@@ -968,6 +980,9 @@ protected function _prepareQuery()
968980
if (!$stmt = $this->_mysqli->prepare($this->_query)) {
969981
trigger_error("Problem preparing query ($this->_query) " . $this->_mysqli->error, E_USER_ERROR);
970982
}
983+
if ($this->traceEnabled)
984+
$this->traceStartQ = microtime (true);
985+
971986
return $stmt;
972987
}
973988

@@ -1204,5 +1219,31 @@ public function _transaction_status_check () {
12041219
return;
12051220
$this->rollback ();
12061221
}
1222+
1223+
/**
1224+
* Query exection time tracking switch
1225+
*
1226+
* @param bool $enabled Enable execution time tracking
1227+
* @param string $stripPrefix Prefix to strip from the path in exec log
1228+
**/
1229+
public function setTrace ($enabled, $stripPrefix = null) {
1230+
$this->traceEnabled = $enabled;
1231+
$this->traceStripPrefix = $stripPrefix;
1232+
return $this;
1233+
}
1234+
/**
1235+
* Get where and what function was called for query stored in MysqliDB->trace
1236+
*
1237+
* @return string with information
1238+
*/
1239+
private function _traceGetCaller () {
1240+
$dd = debug_backtrace ();
1241+
$caller = next ($dd);
1242+
while (isset ($caller) && $caller["file"] == __FILE__ )
1243+
$caller = next($dd);
1244+
1245+
return __CLASS__ . "->" . $caller["function"] . "() >> file \"" .
1246+
str_replace ($this->traceStripPrefix, '', $caller["file"] ) . "\" line #" . $caller["line"] . " " ;
1247+
}
12071248
} // END class
12081249
?>

readme.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,31 @@ if (!$db->insert ('myTable', $insertData)) {
474474
$db->commit();
475475
}
476476
```
477+
478+
### Query exectution time benchmarking
479+
To track query execution time setTrace() function should be called.
480+
```php
481+
$db->setTrace (true);
482+
// As a second parameter it is possible to define prefix of the path which should be striped from filename
483+
// $db->setTrace (true, $_SERVER['SERVER_ROOT']);
484+
$db->get("users");
485+
$db->get("test");
486+
print_r ($db->trace);
487+
```
488+
489+
```
490+
[0] => Array
491+
(
492+
[0] => SELECT * FROM t_users ORDER BY `id` ASC
493+
[1] => 0.0010669231414795
494+
[2] => MysqliDb->get() >> file "/avb/work/PHP-MySQLi-Database-Class/tests.php" line #151
495+
)
496+
497+
[1] => Array
498+
(
499+
[0] => SELECT * FROM t_test
500+
[1] => 0.00069189071655273
501+
[2] => MysqliDb->get() >> file "/avb/work/PHP-MySQLi-Database-Class/tests.php" line #152
502+
)
503+
504+
```

tests.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
66
if(!$db) die("Database error");
77

8+
$mysqli = new mysqli ('localhost', 'root', '', 'testdb');
9+
$db = new Mysqlidb($mysqli);
10+
811
$db = new Mysqlidb(Array (
912
'host' => 'localhost',
1013
'username' => 'root',
@@ -13,11 +16,10 @@
1316
'charset' => null));
1417
if(!$db) die("Database error");
1518

16-
$mysqli = new mysqli ('localhost', 'root', '', 'testdb');
17-
$db = new Mysqlidb($mysqli);
1819

1920
$prefix = 't_';
2021
$db->setPrefix($prefix);
22+
$db->setTrace(true);
2123

2224
$tables = Array (
2325
'users' => Array (
@@ -360,4 +362,5 @@ function createTable ($name, $data) {
360362

361363
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
362364

365+
print_r ($db->trace);
363366
?>

0 commit comments

Comments
 (0)