mirror of
https://github.com/dg/dibi.git
synced 2025-07-31 19:30:30 +02:00
numOfQueries and totalTime moved to profilers
This commit is contained in:
@@ -101,11 +101,13 @@ class DibiNettePanel extends DibiObject implements IBarPanel
|
|||||||
*/
|
*/
|
||||||
public function getTab()
|
public function getTab()
|
||||||
{
|
{
|
||||||
$event = reset($this->events);
|
$totalTime = 0;
|
||||||
|
foreach ($this->events as $event) {
|
||||||
|
$totalTime += $event->time;
|
||||||
|
}
|
||||||
return '<span title="dibi"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEYSURBVBgZBcHPio5hGAfg6/2+R980k6wmJgsJ5U/ZOAqbSc2GnXOwUg7BESgLUeIQ1GSjLFnMwsKGGg1qxJRmPM97/1zXFAAAAEADdlfZzr26miup2svnelq7d2aYgt3rebl585wN6+K3I1/9fJe7O/uIePP2SypJkiRJ0vMhr55FLCA3zgIAOK9uQ4MS361ZOSX+OrTvkgINSjS/HIvhjxNNFGgQsbSmabohKDNoUGLohsls6BaiQIMSs2FYmnXdUsygQYmumy3Nhi6igwalDEOJEjPKP7CA2aFNK8Bkyy3fdNCg7r9/fW3jgpVJbDmy5+PB2IYp4MXFelQ7izPrhkPHB+P5/PjhD5gCgCenx+VR/dODEwD+A3T7nqbxwf1HAAAAAElFTkSuQmCC" />'
|
return '<span title="dibi"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEYSURBVBgZBcHPio5hGAfg6/2+R980k6wmJgsJ5U/ZOAqbSc2GnXOwUg7BESgLUeIQ1GSjLFnMwsKGGg1qxJRmPM97/1zXFAAAAEADdlfZzr26miup2svnelq7d2aYgt3rebl585wN6+K3I1/9fJe7O/uIePP2SypJkiRJ0vMhr55FLCA3zgIAOK9uQ4MS361ZOSX+OrTvkgINSjS/HIvhjxNNFGgQsbSmabohKDNoUGLohsls6BaiQIMSs2FYmnXdUsygQYmumy3Nhi6igwalDEOJEjPKP7CA2aFNK8Bkyy3fdNCg7r9/fW3jgpVJbDmy5+PB2IYp4MXFelQ7izPrhkPHB+P5/PjhD5gCgCenx+VR/dODEwD+A3T7nqbxwf1HAAAAAElFTkSuQmCC" />'
|
||||||
. ($event ? $event->connection->numOfQueries : 0) . ' queries'
|
. count($this->events) . ' queries'
|
||||||
. ($event && $event->connection->totalTime ? ' / ' . sprintf('%0.1f', $event->connection->totalTime * 1000) . 'ms' : '')
|
. ($totalTime ? ' / ' . sprintf('%0.1f', $totalTime * 1000) . 'ms' : '')
|
||||||
. '</span>';
|
. '</span>';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,18 +119,19 @@ class DibiNettePanel extends DibiObject implements IBarPanel
|
|||||||
*/
|
*/
|
||||||
public function getPanel()
|
public function getPanel()
|
||||||
{
|
{
|
||||||
$s = NULL;
|
$totalTime = $s = NULL;
|
||||||
$h = 'htmlSpecialChars';
|
$h = 'htmlSpecialChars';
|
||||||
foreach ($this->events as $event) {
|
foreach ($this->events as $event) {
|
||||||
|
$totalTime += $event->time;
|
||||||
$explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS()
|
$explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS()
|
||||||
if ($this->explain && $event->type === DibiEvent::SELECT) {
|
if ($this->explain && $event->type === DibiEvent::SELECT) {
|
||||||
try {
|
try {
|
||||||
$backup = array($event->connection->onEvent, $event->connection->numOfQueries, $event->connection->totalTime, dibi::$numOfQueries, dibi::$totalTime);
|
$backup = array($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime);
|
||||||
$event->connection->onEvent = NULL;
|
$event->connection->onEvent = NULL;
|
||||||
$cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN' : 'EXPLAIN');
|
$cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN' : 'EXPLAIN');
|
||||||
$explain = dibi::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE);
|
$explain = dibi::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE);
|
||||||
} catch (DibiException $e) {}
|
} catch (DibiException $e) {}
|
||||||
list($event->connection->onEvent, $event->connection->numOfQueries, $event->connection->totalTime, dibi::$numOfQueries, dibi::$totalTime) = $backup;
|
list($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime) = $backup;
|
||||||
}
|
}
|
||||||
|
|
||||||
$s .= '<tr><td>' . sprintf('%0.3f', $event->time * 1000);
|
$s .= '<tr><td>' . sprintf('%0.3f', $event->time * 1000);
|
||||||
@@ -157,7 +160,7 @@ class DibiNettePanel extends DibiObject implements IBarPanel
|
|||||||
'<style> #nette-debug td.nette-DibiProfiler-sql { background: white !important }
|
'<style> #nette-debug td.nette-DibiProfiler-sql { background: white !important }
|
||||||
#nette-debug .nette-DibiProfiler-source { color: #999 !important }
|
#nette-debug .nette-DibiProfiler-source { color: #999 !important }
|
||||||
#nette-debug nette-DibiProfiler tr table { margin: 8px 0; max-height: 150px; overflow:auto } </style>
|
#nette-debug nette-DibiProfiler tr table { margin: 8px 0; max-height: 150px; overflow:auto } </style>
|
||||||
<h1>Queries: ' . (isset($event) ? $event->connection->numOfQueries : 0) . (isset($event) && $event->connection->totalTime === NULL ? '' : ', time: ' . sprintf('%0.3f', $event->connection->totalTime * 1000) . ' ms') . '</h1>
|
<h1>Queries: ' . count($this->events) . ($totalTime === NULL ? '' : ', time: ' . sprintf('%0.3f', $totalTime * 1000) . ' ms') . '</h1>
|
||||||
<div class="nette-inner nette-DibiProfiler">
|
<div class="nette-inner nette-DibiProfiler">
|
||||||
<table>
|
<table>
|
||||||
<tr><th>Time ms</th><th>SQL Statement</th><th>Rows</th><th>Connection</th></tr>' . $s . '
|
<tr><th>Time ms</th><th>SQL Statement</th><th>Rows</th><th>Connection</th></tr>' . $s . '
|
||||||
|
@@ -44,12 +44,6 @@ class DibiConnection extends DibiObject
|
|||||||
/** @var DibiHashMap Substitutes for identifiers */
|
/** @var DibiHashMap Substitutes for identifiers */
|
||||||
private $substitutes;
|
private $substitutes;
|
||||||
|
|
||||||
/** @var int Elapsed time for all queries */
|
|
||||||
public $totalTime = 0;
|
|
||||||
|
|
||||||
/** @var int Number or queries */
|
|
||||||
public $numOfQueries = 0;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -345,9 +339,6 @@ class DibiConnection extends DibiObject
|
|||||||
$this->connected || $this->connect();
|
$this->connected || $this->connect();
|
||||||
|
|
||||||
$event = $this->onEvent ? new DibiEvent($this, DibiEvent::QUERY, $sql) : NULL;
|
$event = $this->onEvent ? new DibiEvent($this, DibiEvent::QUERY, $sql) : NULL;
|
||||||
$this->numOfQueries++;
|
|
||||||
dibi::$numOfQueries++;
|
|
||||||
dibi::$sql = $sql;
|
|
||||||
try {
|
try {
|
||||||
$res = $this->driver->query($sql);
|
$res = $this->driver->query($sql);
|
||||||
|
|
||||||
|
@@ -80,6 +80,8 @@ class DibiEvent
|
|||||||
}
|
}
|
||||||
|
|
||||||
dibi::$elapsedTime = FALSE;
|
dibi::$elapsedTime = FALSE;
|
||||||
|
dibi::$numOfQueries++;
|
||||||
|
dibi::$sql = $sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -96,7 +98,6 @@ class DibiEvent
|
|||||||
$this->time += microtime(TRUE);
|
$this->time += microtime(TRUE);
|
||||||
dibi::$elapsedTime = $this->time;
|
dibi::$elapsedTime = $this->time;
|
||||||
dibi::$totalTime += $this->time;
|
dibi::$totalTime += $this->time;
|
||||||
$this->connection->totalTime += $this->time;
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -28,6 +28,9 @@ class DibiFirePhpLogger extends DibiObject
|
|||||||
/** @var int */
|
/** @var int */
|
||||||
public $filter;
|
public $filter;
|
||||||
|
|
||||||
|
/** @var int Elapsed time for all queries */
|
||||||
|
public $totalTime = 0;
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private static $fireTable = array(array('Time', 'SQL Statement', 'Rows', 'Connection'));
|
private static $fireTable = array(array('Time', 'SQL Statement', 'Rows', 'Connection'));
|
||||||
|
|
||||||
@@ -60,6 +63,7 @@ class DibiFirePhpLogger extends DibiObject
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->totalTime += $event->time;
|
||||||
self::$fireTable[] = array(
|
self::$fireTable[] = array(
|
||||||
sprintf('%0.3f', $event->time * 1000),
|
sprintf('%0.3f', $event->time * 1000),
|
||||||
strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql,
|
strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql,
|
||||||
@@ -74,7 +78,7 @@ class DibiFirePhpLogger extends DibiObject
|
|||||||
$payload = json_encode(array(
|
$payload = json_encode(array(
|
||||||
array(
|
array(
|
||||||
'Type' => 'TABLE',
|
'Type' => 'TABLE',
|
||||||
'Label' => 'dibi profiler (' . dibi::$numOfQueries . ' SQL queries took ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)',
|
'Label' => 'dibi profiler (' . count($this->events) . ' SQL queries took ' . sprintf('%0.3f', $this->totalTime * 1000) . ' ms)',
|
||||||
),
|
),
|
||||||
self::$fireTable,
|
self::$fireTable,
|
||||||
));
|
));
|
||||||
|
@@ -18,6 +18,20 @@ require_once '../dibi/dibi.php';
|
|||||||
ndebug();
|
ndebug();
|
||||||
|
|
||||||
|
|
||||||
|
dibi::connect(array(
|
||||||
|
'driver' => 'sqlite',
|
||||||
|
'database' => 'data/sample.sdb',
|
||||||
|
'profiler' => array(
|
||||||
|
'run' => TRUE,
|
||||||
|
)
|
||||||
|
));
|
||||||
|
|
||||||
|
|
||||||
|
// throws error because SQL is bad
|
||||||
|
dibi::query('SELECT * FROM customers WHERE customer_id < ?', 38);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
dibi::connect(array(
|
dibi::connect(array(
|
||||||
'driver' => 'sqlite',
|
'driver' => 'sqlite',
|
||||||
'database' => 'data/sample.sdb',
|
'database' => 'data/sample.sdb',
|
||||||
|
Reference in New Issue
Block a user