fix(monitors): 修复短暂隐藏重开后的趋势误断层

This commit is contained in:
2026-11-07 14:53:41 +08:00
parent 80706fa52e
commit 050ee30f56
17 changed files with 160 additions and 12 deletions
+62
View File
@@ -0,0 +1,62 @@
import Foundation
@main struct BackgroundTrendChecks {
@MainActor static func main() async throws {
let suite = "kymo-background-checks-" + UUID().uuidString
let defaults = UserDefaults(suiteName: suite)!
defer { defaults.removePersistentDomain(forName: suite) }
let preferences = SamplingPreferences(defaults: defaults)
preferences.panelMilliseconds = 2_000
preferences.backgroundMilliseconds = 30_000
preferences.backgroundEnabled = true
let store = MetricsStore(sampling: preferences, energyEnabled: false)
store.setPanelVisible(true)
try await until { store.history.samples.count >= 2 }
store.setPanelVisible(false)
let hiddenCount = store.history.samples.count
try await Task.sleep(for: .seconds(6))
precondition(store.history.samples.count == hiddenCount, "The 30-second background deadline has not arrived")
precondition(store.processSnapshot == nil && store.energy == nil)
store.setPanelVisible(true)
try await until { store.history.samples.count > hiddenCount }
let samples = store.history.samples
let runs = PanelCurveGeometry.runs(dates: samples.map(\.date), values: samples.map(\.memory),
boundaryDates: Set(samples.filter(\.breakBefore).map(\.date)), intervals: samples.map(\.expectedInterval))
precondition(runs.count == 1, "Short hides with background recording enabled must keep a continuous curve")
print("PASS: six-second hide before the first 30-second background sample remains continuous")
// A full background interval must still collect real resource samples without process scans.
preferences.backgroundMilliseconds = 1_000
store.setPanelVisible(false)
let backgroundCount = store.history.samples.count
try await until { store.history.samples.count >= backgroundCount + 2 }
precondition(store.processSnapshot == nil && store.energy == nil)
store.setPanelVisible(true)
let lastBackgroundDate = store.snapshot.date
try await until { store.snapshot.date > lastBackgroundDate }
let recorded = store.history.samples
precondition(PanelCurveGeometry.runs(dates: recorded.map(\.date), values: recorded.map(\.memory),
boundaryDates: Set(recorded.filter(\.breakBefore).map(\.date)), intervals: recorded.map(\.expectedInterval)).count == 1)
print("PASS: hidden resource samples are recorded and reconnect on reopen")
preferences.backgroundEnabled = false
store.setPanelVisible(false)
let stoppedCount = store.history.samples.count
try await Task.sleep(for: .milliseconds(650))
precondition(store.history.samples.count == stoppedCount)
store.setPanelVisible(true)
try await until { store.history.samples.count > stoppedCount }
precondition(store.history.samples.last?.breakBefore == true)
let stopped = store.history.samples
precondition(PanelCurveGeometry.runs(dates: stopped.map(\.date), values: stopped.map(\.memory),
boundaryDates: Set(stopped.filter(\.breakBefore).map(\.date)), intervals: stopped.map(\.expectedInterval)).count == 2)
store.togglePause()
print("PASS: disabling background recording still stops sampling and preserves the real gap")
}
@MainActor private static func until(_ predicate: () -> Bool) async throws {
let deadline = ContinuousClock.now.advanced(by: .seconds(4))
while !predicate() && ContinuousClock.now < deadline { try await Task.sleep(for: .milliseconds(20)) }
precondition(predicate(), "Timed out waiting for a resource sample")
}
}
+3 -1
View File
@@ -6,7 +6,7 @@ import Foundation
TrendSample(date: Date(timeIntervalSince1970: time), cpu: nil, gpu: value, memory: nil,
breakBefore: boundary, expectedInterval: interval)
}
let variable = GPUUtilizationSummary(samples: [sample(0, 0), sample(2, 1), sample(10, 1, interval: 8)])
let variable = GPUUtilizationSummary(samples: [sample(0, 0), sample(2, 1, interval: 8), sample(10, 1)])
precondition(abs(variable.average! - 0.9) < 0.00001 && variable.peak == 1)
let clipped = GPUUtilizationSummary(samples: [sample(0, 0, interval: 120), sample(120, 1, interval: 120)])
precondition(abs(clipped.average! - 0.75) < 0.00001 && clipped.coverage == 60)
@@ -15,6 +15,8 @@ import Foundation
precondition(GPUUtilizationSummary(samples: [sample(0, 1), sample(2, nil), sample(4, 0)]).average == nil)
precondition(GPUUtilizationSummary(samples: [sample(0, 1), sample(2, 0, boundary: true)]).average == nil)
precondition(GPUUtilizationSummary(samples: [sample(0, 1), sample(60, 0)]).average == nil)
precondition(GPUUtilizationSummary(samples: [sample(0, 1), sample(8, 0, interval: 30)]).average == nil,
"A later slow cadence must not reconnect an earlier missed interval")
precondition(GPUMonitor.memoryBytes(NSNumber(value: -1)) == nil)
precondition(GPUMonitor.memoryBytes(NSNumber(value: Double.nan)) == nil)
precondition(GPUMonitor.memoryBytes(NSNumber(value: 0)) == 0)
@@ -31,6 +31,15 @@ import QuartzCore
precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) == 0, "Missing values must leave a real gap")
plot.configure(input([nil,nil,nil,nil]))
precondition(redPixels(bitmap(plot)) == 0)
let changedDates = [0.0, 2, 8, 10].map { Date(timeIntervalSinceReferenceDate: $0) }
plot.configure(.init(frame: .init(dates: changedDates, series: [[0.8,0.8,0.8,0.8]],
intervals: [2,30,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5))
precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) > 10,
"Reopening before the first background deadline must keep the curve connected")
plot.configure(.init(frame: .init(dates: [changedDates[0], changedDates[2]], series: [[0.8,0.8]],
intervals: [2,30], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5))
precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) == 0,
"A later slow cadence must not reconnect an earlier missed sample")
var hovered = continuous; hovered.hoveredIndex = 1
plot.configure(hovered)
precondition(redPixels(bitmap(plot)) > 0)
+4
View File
@@ -13,6 +13,10 @@ import Foundation
precondition(PanelCurveGeometry.runs(dates: slow, values: [0.1, 0.2, 0.3], maximumGap: 3600, boundaryDates: [slow[1]]).count == 2)
precondition(PanelCurveGeometry.runs(dates: slow, values: [0.1, 0.2, 0.3], maximumGap: 5, intervals: [1800, 1800, 0.5]).count == 1)
precondition(PanelCurveGeometry.runs(dates: slow, values: [0.1, 0.2, 0.3], maximumGap: 5, boundaryDates: [slow[2]], intervals: [1800, 1800, 0.5]).count == 2)
let changed = [0.0, 8, 10, 18].map { Date(timeIntervalSinceReferenceDate: $0) }
precondition(PanelCurveGeometry.runs(dates: changed, values: [0.1, 0.2, 0.3, 0.4],
intervals: [2, 30, 2, 2]).count == 3,
"A slow cadence applies only to its outgoing segment; earlier and later real gaps remain gaps")
let frame = PanelCurveFrame(dates: dates, series: [Array(repeating: 0.3, count: 60)], intervals: Array(repeating: 2, count: 60), boundaryDates: [])
precondition(frame.dates.count == 60)
let domain = PanelCurveFrame.domain(begin: dates.first!, end: dates.last!)
+15
View File
@@ -49,6 +49,21 @@ import Foundation
extended.network = nil
precondition(extended.downloadRate == nil)
var scheduledHistory = MetricHistory()
scheduledHistory.recordScheduledInterval(30)
extended.network = [NetworkInterfaceMetrics(name: "en0", received: 100, sent: 50, download: 20, upload: 10)]
scheduledHistory.append(extended)
scheduledHistory.recordScheduledInterval(30)
scheduledHistory.recordScheduledInterval(2)
scheduledHistory.recordScheduledInterval(.nan)
scheduledHistory.recordScheduledInterval(-1)
precondition(scheduledHistory.samples.last?.expectedInterval == 30)
precondition(scheduledHistory.interfaces.values.allSatisfy { $0.last?.expectedInterval == 30 })
scheduledHistory.append(extended)
precondition(scheduledHistory.samples.map(\.expectedInterval) == [30, 2])
precondition(scheduledHistory.interfaces.values.allSatisfy { $0.map(\.expectedInterval) == [30, 2] },
"Every interface must preserve only the outgoing slow interval, then resume its normal cadence")
var history = MetricHistory()
for index in 0..<100 {
var item = MetricsSnapshot()