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
+1 -16
View File
@@ -10,22 +10,7 @@ import Foundation
precondition(CPUTemperatureMonitor.decodeSensorNumber(type: 0, bytes: []) == nil)
precondition(FanMetrics.validRPM(0) == 0)
for value in [Double.nan, .infinity, -1, 30_001] { precondition(FanMetrics.validRPM(value) == nil) }
precondition(FanRotation.speed(rpm: 0, reducedMotion: false) == 0)
precondition(FanRotation.speed(rpm: nil, reducedMotion: false) == 0)
precondition(FanRotation.speed(rpm: .nan, reducedMotion: false) == 0)
precondition(FanRotation.speed(rpm: 6000, reducedMotion: true) == 0)
let tiers = [1000.0, 3000, 5000, 7000].map { FanRotation.speed(rpm: $0, reducedMotion: false) }
precondition(zip(tiers, tiers.dropFirst()).allSatisfy { $0 < $1 })
var rotation = FanRotation()
rotation.update(rpm: 1000, reducedMotion: false, at: 100)
let before = rotation.angle(at: 101)
rotation.update(rpm: 7000, reducedMotion: false, at: 101)
precondition(rotation.angle(at: 101) == before)
rotation.update(rpm: 0, reducedMotion: false, at: 102)
precondition(rotation.angle(at: 102) == rotation.angle(at: 200))
rotation.update(rpm: 7000, reducedMotion: true, at: 200)
precondition(rotation.angle(at: 200) == rotation.angle(at: 300))
print("PASS: fan decoding, invalid RPM, speed tiers, continuous phase and stop/reduced-motion")
print("PASS: fan decoding, invalid RPM and static indicator data")
print("Live fan RPM:", CPUTemperatureMonitor().sampleFans() as Any)
}
}
+1
View File
@@ -96,6 +96,7 @@ import Foundation
precondition(reset.cpu == nil)
let sampling = SamplingPreferences(defaults: defaults)
sampling.backgroundMilliseconds = 500
sampling.backgroundEnabled = true
let store = MetricsStore(sampling: sampling)
try await waitUntil { store.snapshot.cpu != nil }
precondition(store.snapshot.cpu != nil)
+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 {
+30
View File
@@ -0,0 +1,30 @@
import Foundation
@main struct SensorScheduleChecks {
static func main() {
var cpu = SensorSchedule()
var gpu = SensorSchedule()
precondition(cpu.isDue(at: 0) && gpu.isDue(at: 0))
cpu.complete(available: true, at: 0)
gpu.complete(available: false, at: 0)
precondition(!cpu.isDue(at: 9) && cpu.isDue(at: 10))
for now in stride(from: 10.0, through: 60, by: 10) {
cpu.complete(available: true, at: now)
if now < 60 { precondition(!gpu.isDue(at: now)) }
}
precondition(gpu.isDue(at: 60))
gpu.complete(available: false, at: 60)
precondition(!gpu.isDue(at: 179) && gpu.isDue(at: 180))
var now = 180.0
for delay in [240.0, 480, 600, 600, 600] {
gpu.complete(available: false, at: now)
precondition(gpu.deadline == now + delay)
now += delay
}
gpu.complete(available: true, at: now)
precondition(gpu.deadline == now + 10)
gpu.complete(available: false, at: now + 10)
precondition(gpu.deadline == now + 70)
print("PASS: independent sensor cadences, exponential failure backoff, cap and recovery")
}
}