Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Analysis/QueryAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ public function analyze(array $queries): array

foreach ($queries as $query) {
$sql = (string) ($query['sql'] ?? '');
$normalized = $this->normalize($sql);
$fingerprint = $this->fingerprint($sql);
$durationMs = (float) ($query['duration_ms'] ?? 0);
$totalMs += $durationMs;

if ($this->isCacheLock($sql)) {
continue;
}

$normalized = $this->normalize($sql);
$fingerprint = $this->fingerprint($sql);

if (!isset($groups[$fingerprint])) {
$groups[$fingerprint] = [
'fingerprint' => $fingerprint,
Expand Down Expand Up @@ -116,6 +121,16 @@ private function finish(array $group): array
];
}

/**
* Magento's block cache lock (LockGuardedCacheLoader with the db lock provider) takes a
* GET_LOCK and RELEASE_LOCK pair for every uncached block. Matched on the SQL because
* the call site resolves to the logger proxy. Repeating is how it works, not a defect.
*/
private function isCacheLock(string $sql): bool
{
return preg_match('/^\s*SELECT\s+(GET_LOCK|RELEASE_LOCK)\s*\(/i', $sql) === 1;
}

/**
* What makes two queries the same one. Public because more than one thing needs to
* agree on it, and agreeing by having the same code twice is how they stop agreeing.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Query findings and repeat counts ignore Magento's block cache lock pairs (`GET_LOCK` and `RELEASE_LOCK`). They stay in the Queries tab.
- Repeated queries from known core code that only runs outside production mode are reported as one info
finding that says so, instead of an N+1 warning. The first is Magento's theme registration plugin, which saves
every theme before each admin request in developer mode. Real N+1 detection is unchanged, and more sources can
Expand Down
47 changes: 47 additions & 0 deletions Test/Unit/Analysis/Rule/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,53 @@ public function coreThemeRegistrationIsInfoAndARealNPlusOneStillWarns(): void
$this->assertStringContainsString('product', $byId['query.n_plus_one']['evidence']['sql']);
}

#[Test]
public function blockCacheLockPairsAreNeitherNPlusOneNorRepeated(): void
{
$items = [];
$callsite = [['file' => 'Logger/LoggerProxy.php', 'line' => 184, 'call' => 'proxy()']];

for ($i = 1; $i <= 20; $i++) {
$items[] = [
'sql' => 'SELECT GET_LOCK(?, ?)',
'bindings' => ['lock_' . $i, 10],
'duration_ms' => 0.1,
'callsite' => $callsite,
];
$items[] = [
'sql' => 'SELECT RELEASE_LOCK(?)',
'bindings' => ['lock_' . $i],
'duration_ms' => 0.1,
'callsite' => $callsite,
];
}

$findings = (new RepeatedQueryRule(new QueryAnalyzer()))->apply(
$this->profile(['queries' => ['payload' => ['items' => $items]]])
);

$this->assertSame([], $findings);
}

#[Test]
public function aRealRepeatedSelectIsStillFlaggedBesideLockPairs(): void
{
$items = [];
$callsite = [['file' => 'Loader.php', 'line' => 10, 'call' => 'x()']];

for ($i = 1; $i <= 4; $i++) {
$items[] = ['sql' => 'SELECT GET_LOCK(?, ?)', 'bindings' => ['l' . $i, 10], 'duration_ms' => 0.1, 'callsite' => $callsite];
$items[] = ['sql' => 'SELECT * FROM product WHERE id = ?', 'bindings' => [$i], 'duration_ms' => 1.0, 'callsite' => $callsite];
}

$findings = (new RepeatedQueryRule(new QueryAnalyzer()))->apply(
$this->profile(['queries' => ['payload' => ['items' => $items]]])
);

$this->assertCount(1, $findings);
$this->assertStringContainsString('product', $findings[0]->toArray()['evidence']['sql']);
}

#[Test]
public function aKnownSourceBelowTheRepeatThresholdSaysNothing(): void
{
Expand Down
Loading