Skip to content

Commit

Permalink
DibiProfiler: $tickets is now static, profiler sets dibi static varia…
Browse files Browse the repository at this point in the history
…bles
  • Loading branch information
dg committed Apr 3, 2010
1 parent 52f8128 commit 57b6813
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
10 changes: 1 addition & 9 deletions dibi/libs/DibiConnection.php
Expand Up @@ -314,22 +314,14 @@ final public function nativeQuery($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 =
$res = new DibiResult($res, $this->config);
} else {
$res = $this->driver->getAffectedRows();
}

$time += microtime(TRUE);
dibi::$elapsedTime = $time;
dibi::$totalTime += $time;

if (isset($ticket)) {
$this->profiler->after($ticket, $res);
}
Expand Down
34 changes: 20 additions & 14 deletions dibi/libs/DibiProfiler.php
Expand Up @@ -36,10 +36,10 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug
private $filter = self::ALL;

/** @var array */
public $tickets = array();
public static $tickets = array();

/** @var array */
public static $table = array(array('Time', 'SQL Statement', 'Rows', 'Connection'));
public static $fireTable = array(array('Time', 'SQL Statement', 'Rows', 'Connection'));



Expand Down Expand Up @@ -87,9 +87,11 @@ public function setFilter($filter)
*/
public function before(DibiConnection $connection, $event, $sql = NULL)
{
$this->tickets[] = array($connection, $event, $sql);
end($this->tickets);
return key($this->tickets);
if ($event & self::QUERY) dibi::$numOfQueries++;
dibi::$elapsedTime = FALSE;
self::$tickets[] = array($connection, $event, trim($sql), -microtime(TRUE), NULL);
end(self::$tickets);
return key(self::$tickets);
}


Expand All @@ -102,25 +104,29 @@ public function before(DibiConnection $connection, $event, $sql = NULL)
*/
public function after($ticket, $res = NULL)
{
if (!isset($this->tickets[$ticket])) {
if (!isset(self::$tickets[$ticket])) {
throw new InvalidArgumentException('Bad ticket number.');
}

list($connection, $event, $sql) = $this->tickets[$ticket];
$sql = trim($sql);
$ticket = & self::$tickets[$ticket];
$ticket[3] += microtime(TRUE);
list($connection, $event, $sql, $time) = $ticket;

dibi::$elapsedTime = $time;
dibi::$totalTime += $time;

if (($event & $this->filter) === 0) return;

if ($event & self::QUERY) {
try {
$count = $res instanceof DibiResult ? count($res) : '-';
$ticket[4] = $count = $res instanceof DibiResult ? count($res) : '-';
} catch (Exception $e) {
$count = '?';
}

if (count(self::$table) < self::$maxQueries) {
self::$table[] = array(
sprintf('%0.3f', dibi::$elapsedTime * 1000),
if (count(self::$fireTable) < self::$maxQueries) {
self::$fireTable[] = array(
sprintf('%0.3f', $time * 1000),
strlen($sql) > self::$maxLength ? substr($sql, 0, self::$maxLength) . '...' : $sql,
$count,
$connection->getConfig('driver') . '/' . $connection->getConfig('name')
Expand All @@ -136,7 +142,7 @@ public function after($ticket, $res = NULL)
'Type' => 'TABLE',
'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) {
$num++;
Expand All @@ -150,7 +156,7 @@ public function after($ticket, $res = NULL)
$this->writeFile(
"OK: " . $sql
. ($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-- " . date('Y-m-d H:i:s')
. "\n\n"
Expand Down

0 comments on commit 57b6813

Please sign in to comment.