perf(efficiency): 接入真实CPU能量并隔离展示与采样需求

This commit is contained in:
2026-11-04 01:11:24 +08:00
parent a96d188abe
commit 1c26fc18f3
37 changed files with 1249 additions and 179 deletions
+51 -8
View File
@@ -1,4 +1,5 @@
import Foundation
import Observation
@main struct SamplingIntervalChecks {
@MainActor static func main() async throws {
@@ -26,15 +27,15 @@ import Foundation
preferences.backgroundMilliseconds = 1_800_000
let store = MetricsStore(sampling: preferences)
store.setPanelVisible(true)
try await until { store.snapshot.processes != nil }
try await until { store.processSnapshot != nil }
precondition(store.effectiveMilliseconds == 500)
store.setPanelVisible(false)
precondition(store.snapshot.processes == nil && store.effectiveMilliseconds == 1_800_000)
precondition(store.processSnapshot == nil && store.effectiveMilliseconds == 1_800_000)
try await Task.sleep(for: .milliseconds(200))
let hiddenDate = store.snapshot.date
preferences.panelMilliseconds = 700
try await Task.sleep(for: .milliseconds(650))
precondition(store.snapshot.date == hiddenDate && store.snapshot.processes == nil)
precondition(store.snapshot.date == hiddenDate && store.processSnapshot == nil)
preferences.backgroundMilliseconds = 500
try await until { store.snapshot.date > hiddenDate }
precondition(store.history.samples.last?.breakBefore == false)
@@ -50,7 +51,7 @@ import Foundation
try await Task.sleep(for: .milliseconds(650))
precondition(store.isPaused && store.snapshot.date == paused)
store.togglePause()
try await until { store.snapshot.date > paused && store.snapshot.processes != nil }
try await until { store.snapshot.date > paused && store.processSnapshot != nil }
precondition(store.effectiveMilliseconds == 1_800_000)
precondition(store.history.samples.last?.breakBefore == true)
store.togglePause()
@@ -75,11 +76,14 @@ import Foundation
let changingStore = MetricsStore(sampling: changing, sampler: SystemSampler(processRead: { slowRead.read(&$0) }))
changingStore.setPanelVisible(true)
try await until { !changingStore.history.samples.isEmpty }
precondition(changingStore.snapshot.processes == nil)
precondition(changingStore.processSnapshot == nil)
let countBeforeChange = changingStore.history.samples.count
let resourceChanges = PublicationCount()
withObservationTracking { _ = changingStore.snapshot } onChange: { resourceChanges.increment() }
changing.panelMilliseconds = 1_800_000
try await until { changingStore.snapshot.processes != nil }
try await until { changingStore.processSnapshot != nil }
precondition(changingStore.history.samples.count == countBeforeChange, "Rate changes must preserve in-flight process delivery without an extra quick sample")
precondition(resourceChanges.value == 0, "Process completion must not invalidate resource snapshot observers")
changingStore.togglePause()
let dormantPreferences = SamplingPreferences(defaults: defaults)
dormantPreferences.backgroundEnabled = false
@@ -89,11 +93,11 @@ import Foundation
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 }
try await until { dormant.processSnapshot != 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)
precondition(dormant.history.samples.count == hiddenCount && dormant.processSnapshot == nil)
dormantPreferences.backgroundEnabled = true
dormantPreferences.backgroundMilliseconds = 500
try await until { dormant.history.samples.count > hiddenCount }
@@ -104,6 +108,38 @@ import Foundation
dormant.setPanelVisible(true)
try await until { dormant.history.samples.count > disabledCount }
precondition(dormant.history.samples.last?.breakBefore == true)
dormant.setDisplaySleeping(true)
let darkCount = dormant.history.samples.count
try await Task.sleep(for: .milliseconds(650))
precondition(dormant.history.samples.count == darkCount && dormant.processSnapshot == nil && dormant.energy == nil)
dormantPreferences.backgroundEnabled = true
dormantPreferences.backgroundMilliseconds = 500
try await until { dormant.history.samples.count > darkCount }
precondition(dormant.processSnapshot == nil && dormant.energy == nil,
"Explicit background trends may run with the display off, but process/energy scans must stay stopped")
dormantPreferences.backgroundEnabled = false
dormant.setDisplaySleeping(false)
try await until { dormant.processSnapshot != nil }
dormant.setSystemSleeping(true)
let sleepingCount = dormant.history.samples.count
try await Task.sleep(for: .milliseconds(650))
precondition(dormant.isSystemSleeping && dormant.history.samples.count == sleepingCount && dormant.energy == nil)
dormant.setSystemSleeping(false)
try await until { dormant.history.samples.count > sleepingCount }
precondition(dormant.history.samples.last?.breakBefore == true && dormant.snapshot.cpu == nil)
let staleProbe = DelayedProcessRead(delay: 0.01)
let stale = SystemSampler(processRead: { staleProbe.read(&$0) })
await stale.setProcessVisible(false, revision: 2)
_ = await stale.sample(includeProcesses: true, includeSlowMetrics: false, demandRevision: 1)
try await Task.sleep(for: .milliseconds(100))
precondition(staleProbe.count == 0, "A stale demand must not start any process work")
await stale.setProcessVisible(true, revision: 3)
await stale.setProcessVisible(false, revision: 2)
_ = await stale.sample(includeSlowMetrics: false, demandRevision: 3)
let liveDeadline = ContinuousClock.now.advanced(by: .seconds(2))
while staleProbe.count == 0 && ContinuousClock.now < liveDeadline { try await Task.sleep(for: .milliseconds(20)) }
precondition(staleProbe.count == 1, "A stale stop must not undo the newer visible demand")
await stale.setProcessVisible(false, revision: 4)
dormant.togglePause()
print("PASS: defaults, 100ms quantization/clamping, independent persistence, invalid defaults, active/background wake, pause precedence, slow-interval initial processes and history boundary")
}
@@ -135,3 +171,10 @@ private final class DelayedProcessRead: @unchecked Sendable {
return monitor.sample()
}
}
private final class PublicationCount: @unchecked Sendable {
private let lock = NSLock()
private var count = 0
var value: Int { lock.lock(); defer { lock.unlock() }; return count }
func increment() { lock.lock(); count += 1; lock.unlock() }
}