-
Notifications
You must be signed in to change notification settings - Fork 86
Split per-database collection timing into open + drain — the measurement the next QS fix needs (#2164) #2173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+148
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright (c) 2026 Erik Darling, Darling Data LLC | ||
| * | ||
| * This file is part of the SQL Server Performance Monitor. | ||
| * | ||
| * Licensed under the MIT License. See LICENSE file in the project root for full license information. | ||
| */ | ||
|
|
||
| using System; | ||
| using PerformanceMonitor.Collectors; | ||
| using Xunit; | ||
|
|
||
| namespace Darling.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Pins the open-vs-drain timing split (#2164). It exists because a single blended <c>sql:</c> number could | ||
| /// not answer the question a 5x payload cut raised on production: the byte budget moved bytes 5x and the | ||
| /// batch clock ~0%, so the cost is upstream of shipping — but WHICH statement was unprovable from the log, | ||
| /// and the next fix would have been a guess. Open time (everything before the first rowset) and drain time | ||
| /// (row streaming) have different fixes, so they must be separately visible. | ||
| /// </summary> | ||
| public sealed class StatementSplitTimingTests | ||
| { | ||
| private static CollectorContext NewContext() => new() | ||
| { | ||
| ServerId = 1, | ||
| ServerName = "s", | ||
| CollectionTime = new DateTime(2026, 8, 10, 0, 0, 0, DateTimeKind.Utc), | ||
| Deltas = new CollectorDeltaCalculator(), | ||
| }; | ||
|
|
||
| [Fact] | ||
| public void OpenMs_DefaultsToZero_SoAnUnmeasuredHostIsNotReadAsInstant() | ||
| { | ||
| /* Lite does not measure this today. Zero must mean "not measured", which is why the log only emits | ||
| the split when the value is positive rather than printing "open:0ms" and inviting the reader to | ||
| conclude the aggregate was free. */ | ||
| Assert.Equal(0, NewContext().PerItemOpenMs); | ||
| } | ||
|
|
||
| [Theory] | ||
| /* An aggregate-bound pass: nearly all the batch is spent before the first row arrives, so no client | ||
| byte budget can shorten it — the query_store shape measured on the field server. */ | ||
| [InlineData(100_000L, 0L, 98_000L, 2_000L)] | ||
| /* A drain-bound pass: rows are cheap to produce and expensive to move, where the budget IS the lever. */ | ||
| [InlineData(100_000L, 0L, 3_000L, 97_000L)] | ||
| /* The watermark phase is a STORE round trip the driver's stopwatch already started before. It must come | ||
| out of drain, not inflate it — the review catch this arithmetic exists to prevent. */ | ||
| [InlineData(100_000L, 40_000L, 55_000L, 5_000L)] | ||
| /* Degenerate: phases exceeding the batch total (skew across separate stopwatches) must clamp at zero | ||
| rather than print a negative drain, which would read as a measurement bug in the field. */ | ||
| [InlineData(5_000L, 3_000L, 6_000L, 0L)] | ||
| public void DrainExcludesWatermarkAndOpen_AndNeverGoesNegative(long sqlMs, long watermarkMs, long openMs, long expectedDrain) | ||
| { | ||
| var context = NewContext(); | ||
| context.PerItemWatermarkMs = watermarkMs; | ||
| context.PerItemOpenMs = openMs; | ||
|
|
||
| /* Calls the SHIPPED arithmetic (CollectorContext.DrainMsFrom) — the log line calls the same method, | ||
| so this cannot drift into pinning a copy of the formula the way the first cut did. */ | ||
| Assert.Equal(expectedDrain, context.DrainMsFrom(sqlMs)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EveryPhaseAccountedFor_ThePartsNeverExceedTheWhole() | ||
| { | ||
| /* The split's contract as a reader sees it: wm + open + drain == the sql: total, so nothing is | ||
| silently unattributed. Holds for any measurement where the phases fit inside the total. */ | ||
| var context = NewContext(); | ||
| context.PerItemWatermarkMs = 1_200; | ||
| context.PerItemOpenMs = 300_000; | ||
| const long sqlMs = 350_000; | ||
|
|
||
| Assert.Equal(sqlMs, context.PerItemWatermarkMs + context.PerItemOpenMs + context.DrainMsFrom(sqlMs)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test doesn't actually exercise
QueryStoreCollector's reset code — it manually setsPerItemTextBudgetExceeded/PerItemTextBytesShipped/PerItemShippedBoundaryback to their reset values itself, then assertsPerItemOpenMsis untouched. That only provesCollectorContextdoesn't do anything surprising to its own field; it would not catch a regression whereQueryStoreCollector.ReadRowsAsync(PerformanceMonitor.Collectors/QueryStoreCollector.cs:1096-1098) itself starts zeroingPerItemOpenMs— which is exactly the silent-zero regression the doc comment above (and this PR's description) says this test guards against.There's already a
FakeCollectorDataReader+ established pattern for this inLite.Tests/QueryStoreCollectorDefinitionTests.cs(ReadItemAsync_ResetsPerItemSignals_AndNormalRowsDoNotTripTheBudget, ~line 870), which pre-sets signals and then callsQueryStoreCollector.Instance.ReadItemAsync(...)for real. Doing the same here (pre-setPerItemOpenMs, call the realReadItemAsyncwith a fake reader, assert it survives) would pin the actual contract instead of a hand-mirrored copy of it.Same concern applies to
DrainIsTheRemainder_AndNeverNegativeabove — it recomputesMath.Max(0, sqlMs - context.PerItemOpenMs)inline rather than calling the runner's actual log-line arithmetic, so a refactor of that line inDarlingCollectorRunner.cscould drift from this test without either one failing.