using SQLInsight.Core.Data; namespace SQLInsight.Core.Tests; public class DeltaTests { private static readonly DateTime Start = new(2026, 9, 1, 8, 0, 0, DateTimeKind.Utc); private static readonly DateTimeOffset T0 = new(2026, 9, 29, 12, 0, 0, TimeSpan.Zero); private static WaitStatsSnapshot Waits(DateTimeOffset at, DateTime start, params WaitStat[] waits) => new() { CapturedAt = at, SqlServerStartTime = start, Waits = waits }; [Fact] public void Wait_delta_subtracts_and_drops_benign_waits() { var before = Waits(T0, Start, new WaitStat("LCK_M_X", 10, 1000, 10), new WaitStat("LAZYWRITER_SLEEP", 5, 5000, 0)); var after = Waits(T0.AddMinutes(5), Start, new WaitStat("LCK_M_X", 15, 4000, 20), new WaitStat("LAZYWRITER_SLEEP", 9, 90000, 0), new WaitStat("WRITELOG", 100, 300, 5)); var delta = WaitStatsDelta.Compute(before, after)!; Assert.Equal(TimeSpan.FromMinutes(5), delta.Interval); Assert.Equal([new WaitStat("LCK_M_X", 5, 3000, 10), new WaitStat("WRITELOG", 100, 300, 5)], delta.Waits); } [Fact] public void Wait_delta_is_null_after_restart() { var before = Waits(T0, Start, new WaitStat("LCK_M_X", 10, 1000, 10)); var after = Waits(T0.AddMinutes(5), Start.AddDays(1), new WaitStat("LCK_M_X", 1, 10, 0)); Assert.Null(WaitStatsDelta.Compute(before, after)); } [Fact] public void Wait_delta_is_null_when_stats_were_cleared() { var before = Waits(T0, Start, new WaitStat("LCK_M_X", 10, 1000, 10)); var after = Waits(T0.AddMinutes(5), Start, new WaitStat("LCK_M_X", 2, 50, 0)); Assert.Null(WaitStatsDelta.Compute(before, after)); } [Fact] public void First_sample_has_no_delta() { Assert.Null(WaitStatsDelta.Compute(null, Waits(T0, Start))); } [Theory] [InlineData("SLEEP_TASK", true)] [InlineData("QDS_PERSIST_TASK_MAIN_LOOP_SLEEP", true)] [InlineData("CXCONSUMER", true)] [InlineData("Total", true)] [InlineData("CXPACKET", false)] [InlineData("PAGEIOLATCH_SH", false)] public void Benign_waits(string waitType, bool benign) { Assert.Equal(benign, WaitCatalog.IsBenign(waitType)); } [Theory] [InlineData("LCK_M_IX", WaitCategory.Locks)] [InlineData("PAGEIOLATCH_EX", WaitCategory.BufferIo)] [InlineData("PAGELATCH_UP", WaitCategory.PageLatch)] [InlineData("THREADPOOL", WaitCategory.WorkerThreads)] [InlineData("WRITELOG", WaitCategory.TransactionLog)] [InlineData("HADR_SYNC_COMMIT", WaitCategory.AvailabilityGroups)] [InlineData("SOMETHING_NEW", WaitCategory.Other)] public void Wait_categories(string waitType, WaitCategory category) { Assert.Equal(category, WaitCatalog.Categorize(waitType)); } private static FileIoStat File(string db, int id, long reads, long stall) => new() { DatabaseName = db, FileId = id, FileType = "ROWS", PhysicalName = "x", Reads = reads, BytesRead = 0, ReadStallMs = stall, Writes = 0, BytesWritten = 0, WriteStallMs = 0, }; [Fact] public void File_io_delta_skips_files_that_restarted_from_zero() { var before = new FileIoSnapshot { CapturedAt = T0, SqlServerStartTime = Start, Files = [File("A", 1, 100, 1000), File("B", 1, 500, 500)] }; var after = new FileIoSnapshot { CapturedAt = T0.AddMinutes(5), SqlServerStartTime = Start, Files = [File("A", 1, 300, 5000), File("B", 1, 10, 10), File("C", 1, 50, 50)], // B restored, C new }; var delta = FileIoDelta.Compute(before, after)!; var a = Assert.Single(delta.Files); Assert.Equal(("A", 200L, 4000L), (a.DatabaseName, a.Reads, a.ReadStallMs)); Assert.Equal(20, a.AverageReadLatencyMs); } [Fact] public void Counter_rates() { var before = new PerformanceCounterSnapshot { CapturedAt = T0, SqlServerStartTime = Start, BatchRequests = 1000 }; var after = before with { CapturedAt = T0.AddSeconds(10), BatchRequests = 3000 }; Assert.Equal(200, PerformanceCounterRates.Compute(before, after)!.BatchRequestsPerSecond); } [Fact] public void Head_blockers_are_blockers_that_are_not_blocked() { var snapshot = new BlockingSnapshot { CapturedAt = T0, Sessions = [ new SessionInfo { SessionId = 51 }, new SessionInfo { SessionId = 60, BlockingSessionId = 51 }, new SessionInfo { SessionId = 61, BlockingSessionId = 60 }, new SessionInfo { SessionId = 70 }, new SessionInfo { SessionId = 71, BlockingSessionId = 70 }, ], }; Assert.Equal([51, 70], snapshot.HeadBlockers.Select(s => s.SessionId)); } }