1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-19 15:45:27 +01:00

numOfQueries and totalTime moved to profilers

This commit is contained in:
David Grudl 2012-02-06 18:15:52 +01:00
parent 06642ec9ee
commit 9b7b964696
5 changed files with 32 additions and 19 deletions

View File

@ -101,11 +101,13 @@ class DibiNettePanel extends DibiObject implements IBarPanel
*/
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" />'
. ($event ? $event->connection->numOfQueries : 0) . ' queries'
. ($event && $event->connection->totalTime ? ' / ' . sprintf('%0.1f', $event->connection->totalTime * 1000) . 'ms' : '')
. count($this->events) . ' queries'
. ($totalTime ? ' / ' . sprintf('%0.1f', $totalTime * 1000) . 'ms' : '')
. '</span>';
}
@ -117,18 +119,19 @@ class DibiNettePanel extends DibiObject implements IBarPanel
*/
public function getPanel()
{
$s = NULL;
$totalTime = $s = NULL;
$h = 'htmlSpecialChars';
foreach ($this->events as $event) {
$totalTime += $event->time;
$explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS()
if ($this->explain && $event->type === DibiEvent::SELECT) {
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;
$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);
} 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);
@ -157,7 +160,7 @@ class DibiNettePanel extends DibiObject implements IBarPanel
'<style> #nette-debug td.nette-DibiProfiler-sql { background: white !important }
#nette-debug .nette-DibiProfiler-source { color: #999 !important }
#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">
<table>
<tr><th>Time&nbsp;ms</th><th>SQL Statement</th><th>Rows</th><th>Connection</th></tr>' . $s . '

View File

@ -44,12 +44,6 @@ class DibiConnection extends DibiObject
/** @var DibiHashMap Substitutes for identifiers */
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();
$event = $this->onEvent ? new DibiEvent($this, DibiEvent::QUERY, $sql) : NULL;
$this->numOfQueries++;
dibi::$numOfQueries++;
dibi::$sql = $sql;
try {
$res = $this->driver->query($sql);

View File

@ -80,6 +80,8 @@ class DibiEvent
}
dibi::$elapsedTime = FALSE;
dibi::$numOfQueries++;
dibi::$sql = $sql;
}
@ -96,7 +98,6 @@ class DibiEvent
$this->time += microtime(TRUE);
dibi::$elapsedTime = $this->time;
dibi::$totalTime += $this->time;
$this->connection->totalTime += $this->time;
return $this;
}

View File

@ -28,6 +28,9 @@ class DibiFirePhpLogger extends DibiObject
/** @var int */
public $filter;
/** @var int Elapsed time for all queries */
public $totalTime = 0;
/** @var array */
private static $fireTable = array(array('Time', 'SQL Statement', 'Rows', 'Connection'));
@ -60,6 +63,7 @@ class DibiFirePhpLogger extends DibiObject
return;
}
$this->totalTime += $event->time;
self::$fireTable[] = array(
sprintf('%0.3f', $event->time * 1000),
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(
array(
'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,
));

View File

@ -18,6 +18,20 @@ require_once '../dibi/dibi.php';
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(
'driver' => 'sqlite',
'database' => 'data/sample.sdb',