Skip to content

Commit

Permalink
Show queries memory usage (#1462)
Browse files Browse the repository at this point in the history
Co-authored-by: Barry vd. Heuvel <barryvdh@gmail.com>
  • Loading branch information
erikn69 and barryvdh authored Feb 9, 2024
1 parent 22333fd commit 04b2868
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/debugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
'hints' => false, // Show hints for common mistakes
'show_copy' => false, // Show copy button next to the query,
'slow_threshold' => false, // Only track queries that last longer than this time in ms
'memory_usage' => false, // Show queries memory usage
],
'mail' => [
'timeline' => false, // Add mails to the timeline
Expand Down
13 changes: 13 additions & 0 deletions src/DataCollector/QueryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class QueryCollector extends PDOCollector
{
protected $timeCollector;
protected $queries = [];
protected $lastMemoryUsage;
protected $renderSqlWithParams = false;
protected $findSource = false;
protected $middleware = [];
Expand Down Expand Up @@ -117,6 +118,10 @@ public function setExplainSource($enabled, $types)
// }
}

public function startMemoryUsage() {
$this->lastMemoryUsage = memory_get_usage(false);
}

/**
*
* @param string $query
Expand Down Expand Up @@ -189,6 +194,7 @@ public function addQuery($query, $bindings, $time, $connection)
'bindings' => $this->getDataFormatter()->escapeBindings($bindings),
'start' => $startTime,
'time' => $time,
'memory' => $this->lastMemoryUsage ? memory_get_usage(false) - $this->lastMemoryUsage : 0,
'source' => $source,
'explain' => $explainResults,
'connection' => $connection->getDatabaseName(),
Expand Down Expand Up @@ -451,6 +457,7 @@ public function collectTransactionEvent($event, $connection)
'bindings' => [],
'start' => microtime(true),
'time' => 0,
'memory' => 0,
'source' => $source,
'explain' => [],
'connection' => $connection->getDatabaseName(),
Expand All @@ -474,12 +481,14 @@ public function reset()
public function collect()
{
$totalTime = 0;
$totalMemory = 0;
$queries = $this->queries;

$statements = [];
foreach ($queries as $query) {
$source = reset($query['source']);
$totalTime += $query['time'];
$totalMemory += $query['memory'];

$statements[] = [
'sql' => $this->getDataFormatter()->formatSql($query['query']),
Expand All @@ -492,6 +501,8 @@ public function collect()
'start' => $query['start'] ?? null,
'duration' => $query['time'],
'duration_str' => ($query['type'] == 'transaction') ? '' : $this->formatDuration($query['time']),
'memory' => $query['memory'],
'memory_str' => $query['memory'] ? $this->getDataFormatter()->formatBytes($query['memory']) : null,
'stmt_id' => $this->getDataFormatter()->formatSource($source),
'xdebug_link' => is_object($source) ? $this->getXdebugLink($source->file ?: '', $source->line) : null,
'connection' => $query['connection'],
Expand Down Expand Up @@ -588,6 +599,8 @@ public function collect()
'nb_failed_statements' => 0,
'accumulated_duration' => $totalTime,
'accumulated_duration_str' => $this->formatDuration($totalTime),
'memory_usage' => $totalMemory,
'memory_usage_str' => $totalMemory ? $this->getDataFormatter()->formatBytes($totalMemory) : null,
'statements' => $statements
];
return $data;
Expand Down
12 changes: 12 additions & 0 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,18 @@ function ($event, $params) use ($queryCollector) {
$queryCollector->collectTransactionEvent('Rollback Transaction', $params[0]);
}
);

$db->getEventDispatcher()->listen(
function (\Illuminate\Database\Events\ConnectionEstablished $event) use ($queryCollector) {
$queryCollector->collectTransactionEvent('Connection Established', $event->connection);

if (app('config')->get('debugbar.options.db.memory_usage')) {
$event->connection->beforeExecuting(function () use ($queryCollector){
$queryCollector->startMemoryUsage();
});
}
}
);
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
Expand Down

0 comments on commit 04b2868

Please sign in to comment.