Skip to content

Commit

Permalink
Test limits (#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh authored Nov 14, 2024
1 parent 02bdb05 commit 42f8d3a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
14 changes: 10 additions & 4 deletions src/DataCollector/QueryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class QueryCollector extends PDOCollector
protected $queries = [];
protected $queryCount = 0;
protected $transactionEventsCount = 0;
protected $infoStatements = 0;
protected $softLimit = null;
protected $hardLimit = null;
protected $lastMemoryUsage;
Expand Down Expand Up @@ -472,6 +473,7 @@ public function reset()
{
$this->queries = [];
$this->queryCount = 0;
$this->infoStatements = 0 ;
}

/**
Expand Down Expand Up @@ -562,6 +564,7 @@ public function collect()
'sql' => '... ' . ($this->queryCount - $this->hardLimit) . ' additional queries are executed but now shown because of Debugbar query limits. Limits can be raised in the config (debugbar.options.db.soft/hard_limit)',
'type' => 'info',
];
$this->infoStatements+= 2;
} elseif ($this->hardLimit && $this->queryCount > $this->hardLimit) {
array_unshift($statements, [
'sql' => '# Query hard limit for Debugbar is reached after ' . $this->hardLimit . ' queries, additional ' . ($this->queryCount - $this->hardLimit) . ' queries are not shown.. Limits can be raised in the config (debugbar.options.db.hard_limit)',
Expand All @@ -571,17 +574,20 @@ public function collect()
'sql' => '... ' . ($this->queryCount - $this->hardLimit) . ' additional queries are executed but now shown because of Debugbar query limits. Limits can be raised in the config (debugbar.options.db.hard_limit)',
'type' => 'info',
];
$this->infoStatements+= 2;
} elseif ($this->softLimit && $this->queryCount > $this->softLimit) {
array_unshift($statements, [
'sql' => '# Query soft limit for Debugbar is reached after ' . $this->softLimit . ' queries, additional ' . ($this->queryCount - $this->softLimit) . ' queries only show the query. Limit can be raised in the config. Limits can be raised in the config (debugbar.options.db.soft_limit)',
'type' => 'info',
]);
$this->infoStatements++;
}

$visibleStatements = count($statements) - $this->infoStatements;
$data = [
'nb_statements' => $this->queryCount,
'nb_visible_statements' => count($statements),
'nb_excluded_statements' => $this->queryCount + $this->transactionEventsCount - count($statements),
'nb_visible_statements' => $visibleStatements,
'nb_excluded_statements' => $this->queryCount + $this->transactionEventsCount - $visibleStatements,
'nb_failed_statements' => 0,
'accumulated_duration' => $totalTime,
'accumulated_duration_str' => $this->formatDuration($totalTime),
Expand Down Expand Up @@ -613,7 +619,7 @@ public function getWidgets()
"default" => "[]"
],
"queries:badge" => [
"map" => "queries.nb_visible_statements",
"map" => "queries.nb_statements",
"default" => 0
]
];
Expand All @@ -630,7 +636,7 @@ private function getSqlQueryToDisplay(array $query): string
// Continue using the old substitute
}
}

if ($query['type'] === 'query' && $this->renderSqlWithParams) {
$bindings = $this->getDataFormatter()->checkBindings($query['bindings']);
if (!empty($bindings)) {
Expand Down
53 changes: 48 additions & 5 deletions tests/DebugbarBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ protected function addWebRoutes(Router $router)
}
]);

$router->get('web/query', [
'uses' => function () {
$router->get('web/query/{num?}', [
'uses' => function ($num = 1) {
debugbar()->boot();

/** @var Connection $connection */
Expand All @@ -81,8 +81,10 @@ protected function addWebRoutes(Router $router)
],
);

$executedQuery = new QueryExecuted('SELECT * FROM users WHERE username = ?', ['debuguser'], 0, $connection);
event($executedQuery);
foreach (range(1, $num) as $i) {
$executedQuery = new QueryExecuted('SELECT * FROM users WHERE username = ?', ['debuguser' . $i], 0, $connection);
event($executedQuery);
}

return 'PONG';
}
Expand Down Expand Up @@ -182,7 +184,7 @@ public function testDatabaseCollectsQueries()
$browser->visit('web/query')
->waitFor('.phpdebugbar')
->click('.phpdebugbar-tab-history')
->assertSeeIn('.phpdebugbar-tab[data-collector="queries"] .phpdebugbar-badge', 2)
->waitForTextIn('.phpdebugbar-tab[data-collector="queries"] .phpdebugbar-badge', 1)
->click('.phpdebugbar-tab[data-collector="queries"]')
->screenshotElement('.phpdebugbar', 'queries-tab')
->waitForText('executed')
Expand All @@ -198,4 +200,45 @@ public function testDatabaseCollectsQueries()
->screenshotElement('.phpdebugbar', 'queries-expanded');
});
}


public function testDatabaseCollectsQueriesWithSoftLimit()
{
if (version_compare($this->app->version(), '10', '<')) {
$this->markTestSkipped('This test is not compatible with Laravel 9.x and below');
}

$this->browse(function (Browser $browser) {
$browser->visit('web/query/200')
->waitFor('.phpdebugbar')
->click('.phpdebugbar-tab-history')
->waitForTextIn('.phpdebugbar-tab[data-collector="queries"] .phpdebugbar-badge', 200, 30)
->click('.phpdebugbar-tab[data-collector="queries"]')
->screenshotElement('.phpdebugbar', 'queries-tab')
->waitForText('executed')
->waitForText('200 statements were executed (100 duplicates)')
->waitForText('Query soft limit for Debugbar is reached after 100 queries, additional 100 queries only show the query.')
->screenshotElement('.phpdebugbar', 'queries-expanded');
});
}

public function testDatabaseCollectsQueriesWithHardLimit()
{
if (version_compare($this->app->version(), '10', '<')) {
$this->markTestSkipped('This test is not compatible with Laravel 9.x and below');
}

$this->browse(function (Browser $browser) {
$browser->visit('web/query/600')
->waitFor('.phpdebugbar')
->click('.phpdebugbar-tab-history')
->waitForTextIn('.phpdebugbar-tab[data-collector="queries"] .phpdebugbar-badge', 600)
->click('.phpdebugbar-tab[data-collector="queries"]')
->screenshotElement('.phpdebugbar', 'queries-tab')
->waitForText('executed')
->waitForText('600 statements were executed, 100 have been excluded (400 duplicates)')
->waitForText('Query soft and hard limit for Debugbar are reached. Only the first 100 queries show details. Queries after the first 500 are ignored. ')
->screenshotElement('.phpdebugbar', 'queries-expanded');
});
}
}

0 comments on commit 42f8d3a

Please sign in to comment.