perf(efficiency): 落实首批采样减负与发行包瘦身

This commit is contained in:
2026-11-03 23:19:52 +08:00
parent 935701fca6
commit def3b1e130
40 changed files with 483 additions and 291 deletions
+30 -2
View File
@@ -6,7 +6,7 @@ import Foundation
let defaults = UserDefaults(suiteName: suite)!
defer { defaults.removePersistentDomain(forName: suite) }
let preferences = SamplingPreferences(defaults: defaults)
precondition(preferences.panelMilliseconds == 2000 && preferences.backgroundMilliseconds == 5000)
precondition(preferences.panelMilliseconds == 2000 && preferences.backgroundMilliseconds == 30000 && !preferences.backgroundEnabled)
for (input, expected) in [(Int.min, 500), (549, 500), (550, 600), (1501, 1500), (Int.max, 1_800_000)] {
preferences.panelMilliseconds = input
precondition(preferences.panelMilliseconds == expected)
@@ -14,10 +14,14 @@ import Foundation
preferences.backgroundMilliseconds = 1799900
let restored = SamplingPreferences(defaults: defaults)
precondition(restored.panelMilliseconds == 1_800_000 && restored.backgroundMilliseconds == 1799900)
precondition(restored.backgroundEnabled, "Existing explicit intervals retain background history")
restored.backgroundEnabled = false
precondition(!SamplingPreferences(defaults: defaults).backgroundEnabled, "Explicit opt-out overrides migration")
defaults.set(Double.nan, forKey: "sampling.panelMilliseconds")
defaults.set(true, forKey: "sampling.backgroundMilliseconds")
let invalid = SamplingPreferences(defaults: defaults)
precondition(invalid.panelMilliseconds == 2000 && invalid.backgroundMilliseconds == 5000)
precondition(invalid.panelMilliseconds == 2000 && invalid.backgroundMilliseconds == 30000 && !invalid.backgroundEnabled)
preferences.backgroundEnabled = true
preferences.panelMilliseconds = 500
preferences.backgroundMilliseconds = 1_800_000
let store = MetricsStore(sampling: preferences)
@@ -77,6 +81,30 @@ import Foundation
try await until { changingStore.snapshot.processes != nil }
precondition(changingStore.history.samples.count == countBeforeChange, "Rate changes must preserve in-flight process delivery without an extra quick sample")
changingStore.togglePause()
let dormantPreferences = SamplingPreferences(defaults: defaults)
dormantPreferences.backgroundEnabled = false
dormantPreferences.panelMilliseconds = 500
let dormant = MetricsStore(sampling: dormantPreferences)
try await Task.sleep(for: .milliseconds(650))
precondition(dormant.history.samples.isEmpty && dormant.snapshot.memory == nil,
"Hidden without a consumer must not perform even an initial full sample")
dormant.setPanelVisible(true)
try await until { dormant.snapshot.processes != nil }
dormant.setPanelVisible(false)
let hiddenCount = dormant.history.samples.count
try await Task.sleep(for: .milliseconds(650))
precondition(dormant.history.samples.count == hiddenCount && dormant.snapshot.processes == nil)
dormantPreferences.backgroundEnabled = true
dormantPreferences.backgroundMilliseconds = 500
try await until { dormant.history.samples.count > hiddenCount }
dormantPreferences.backgroundEnabled = false
let disabledCount = dormant.history.samples.count
try await Task.sleep(for: .milliseconds(650))
precondition(dormant.history.samples.count == disabledCount)
dormant.setPanelVisible(true)
try await until { dormant.history.samples.count > disabledCount }
precondition(dormant.history.samples.last?.breakBefore == true)
dormant.togglePause()
print("PASS: defaults, 100ms quantization/clamping, independent persistence, invalid defaults, active/background wake, pause precedence, slow-interval initial processes and history boundary")
}
@MainActor private static func until(_ predicate: () -> Bool) async throws {