mirror of
https://github.com/dg/dibi.git
synced 2025-08-07 06:36:44 +02:00
DibiProfiler: $tickets is now static, profiler sets dibi static variables
This commit is contained in:
@@ -314,22 +314,14 @@ class DibiConnection extends DibiObject
|
|||||||
}
|
}
|
||||||
$ticket = $this->profiler->before($this, $event, $sql);
|
$ticket = $this->profiler->before($this, $event, $sql);
|
||||||
}
|
}
|
||||||
// TODO: move to profiler?
|
|
||||||
dibi::$numOfQueries++;
|
|
||||||
dibi::$sql = $sql;
|
|
||||||
dibi::$elapsedTime = FALSE;
|
|
||||||
$time = -microtime(TRUE);
|
|
||||||
|
|
||||||
|
dibi::$sql = $sql;
|
||||||
if ($res = $this->driver->query($sql)) { // intentionally =
|
if ($res = $this->driver->query($sql)) { // intentionally =
|
||||||
$res = new DibiResult($res, $this->config);
|
$res = new DibiResult($res, $this->config);
|
||||||
} else {
|
} else {
|
||||||
$res = $this->driver->getAffectedRows();
|
$res = $this->driver->getAffectedRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
$time += microtime(TRUE);
|
|
||||||
dibi::$elapsedTime = $time;
|
|
||||||
dibi::$totalTime += $time;
|
|
||||||
|
|
||||||
if (isset($ticket)) {
|
if (isset($ticket)) {
|
||||||
$this->profiler->after($ticket, $res);
|
$this->profiler->after($ticket, $res);
|
||||||
}
|
}
|
||||||
|
@@ -36,10 +36,10 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug
|
|||||||
private $filter = self::ALL;
|
private $filter = self::ALL;
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
public $tickets = array();
|
public static $tickets = array();
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
public static $table = array(array('Time', 'SQL Statement', 'Rows', 'Connection'));
|
public static $fireTable = array(array('Time', 'SQL Statement', 'Rows', 'Connection'));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -87,9 +87,11 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug
|
|||||||
*/
|
*/
|
||||||
public function before(DibiConnection $connection, $event, $sql = NULL)
|
public function before(DibiConnection $connection, $event, $sql = NULL)
|
||||||
{
|
{
|
||||||
$this->tickets[] = array($connection, $event, $sql);
|
if ($event & self::QUERY) dibi::$numOfQueries++;
|
||||||
end($this->tickets);
|
dibi::$elapsedTime = FALSE;
|
||||||
return key($this->tickets);
|
self::$tickets[] = array($connection, $event, trim($sql), -microtime(TRUE), NULL);
|
||||||
|
end(self::$tickets);
|
||||||
|
return key(self::$tickets);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -102,25 +104,29 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug
|
|||||||
*/
|
*/
|
||||||
public function after($ticket, $res = NULL)
|
public function after($ticket, $res = NULL)
|
||||||
{
|
{
|
||||||
if (!isset($this->tickets[$ticket])) {
|
if (!isset(self::$tickets[$ticket])) {
|
||||||
throw new InvalidArgumentException('Bad ticket number.');
|
throw new InvalidArgumentException('Bad ticket number.');
|
||||||
}
|
}
|
||||||
|
|
||||||
list($connection, $event, $sql) = $this->tickets[$ticket];
|
$ticket = & self::$tickets[$ticket];
|
||||||
$sql = trim($sql);
|
$ticket[3] += microtime(TRUE);
|
||||||
|
list($connection, $event, $sql, $time) = $ticket;
|
||||||
|
|
||||||
|
dibi::$elapsedTime = $time;
|
||||||
|
dibi::$totalTime += $time;
|
||||||
|
|
||||||
if (($event & $this->filter) === 0) return;
|
if (($event & $this->filter) === 0) return;
|
||||||
|
|
||||||
if ($event & self::QUERY) {
|
if ($event & self::QUERY) {
|
||||||
try {
|
try {
|
||||||
$count = $res instanceof DibiResult ? count($res) : '-';
|
$ticket[4] = $count = $res instanceof DibiResult ? count($res) : '-';
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$count = '?';
|
$count = '?';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count(self::$table) < self::$maxQueries) {
|
if (count(self::$fireTable) < self::$maxQueries) {
|
||||||
self::$table[] = array(
|
self::$fireTable[] = array(
|
||||||
sprintf('%0.3f', dibi::$elapsedTime * 1000),
|
sprintf('%0.3f', $time * 1000),
|
||||||
strlen($sql) > self::$maxLength ? substr($sql, 0, self::$maxLength) . '...' : $sql,
|
strlen($sql) > self::$maxLength ? substr($sql, 0, self::$maxLength) . '...' : $sql,
|
||||||
$count,
|
$count,
|
||||||
$connection->getConfig('driver') . '/' . $connection->getConfig('name')
|
$connection->getConfig('driver') . '/' . $connection->getConfig('name')
|
||||||
@@ -136,7 +142,7 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug
|
|||||||
'Type' => 'TABLE',
|
'Type' => 'TABLE',
|
||||||
'Label' => 'dibi profiler (' . dibi::$numOfQueries . ' SQL queries took ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)',
|
'Label' => 'dibi profiler (' . dibi::$numOfQueries . ' SQL queries took ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)',
|
||||||
),
|
),
|
||||||
self::$table,
|
self::$fireTable,
|
||||||
));
|
));
|
||||||
foreach (str_split($payload, 4990) as $num => $s) {
|
foreach (str_split($payload, 4990) as $num => $s) {
|
||||||
$num++;
|
$num++;
|
||||||
@@ -150,7 +156,7 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug
|
|||||||
$this->writeFile(
|
$this->writeFile(
|
||||||
"OK: " . $sql
|
"OK: " . $sql
|
||||||
. ($res instanceof DibiResult ? ";\n-- rows: " . $count : '')
|
. ($res instanceof DibiResult ? ";\n-- rows: " . $count : '')
|
||||||
. "\n-- takes: " . sprintf('%0.3f', dibi::$elapsedTime * 1000) . ' ms'
|
. "\n-- takes: " . sprintf('%0.3f', $time * 1000) . ' ms'
|
||||||
. "\n-- driver: " . $connection->getConfig('driver') . '/' . $connection->getConfig('name')
|
. "\n-- driver: " . $connection->getConfig('driver') . '/' . $connection->getConfig('name')
|
||||||
. "\n-- " . date('Y-m-d H:i:s')
|
. "\n-- " . date('Y-m-d H:i:s')
|
||||||
. "\n\n"
|
. "\n\n"
|
||||||
|
Reference in New Issue
Block a user