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
+53 -1
View File
@@ -2,13 +2,26 @@ import AppKit
/// Explicit local measurement only. Never schedules work during normal launches.
@MainActor enum PanelResourceAudit {
#if DEBUG || KYMO_AUDIT
static let enabled = ProcessInfo.processInfo.environment["KYMO_RESOURCE_AUDIT"] == "1"
static let omitted = enabled ? ProcessInfo.processInfo.environment["KYMO_AUDIT_OMIT"] ?? "" : ""
static func run(controller: MenuPanelController, metrics: MetricsStore) {
guard enabled else { return }
Task { @MainActor in
@MainActor func mark(_ event: String) { SamplingDiagnostics.record(metrics.snapshot, event: "audit." + event) }
controller.panel.animationBehavior = .none
if ProcessInfo.processInfo.environment["KYMO_XPC_AUDIT"] == "1" {
await checkRequestCompletion()
SamplingDiagnostics.record(metrics.snapshot, event: "audit.xpcChecksPassed")
controller.shutdown()
NSApp.terminate(nil)
return
}
@MainActor func mark(_ event: String) {
var report = metrics.snapshot
report.processes = metrics.processSnapshot
SamplingDiagnostics.record(report, event: "audit." + event, hiddenHostAlive: controller.lastHiddenContent != nil)
}
func wait(_ seconds: Int) async { try? await Task.sleep(for: .seconds(seconds)) }
// Pin the test window so unrelated focus changes cannot invalidate a measurement.
controller.presentation.isPinned = true
@@ -36,4 +49,43 @@ import AppKit
NSApp.terminate(nil)
}
}
/// Uses real connection lifecycle but injects local callbacks; peer auth is tested separately by RPCs.
private static func checkRequestCompletion() async {
for _ in 0..<20 {
let result: Int? = await ServiceRequest.perform(timeout: 0.2) { _, finish in
finish(1); finish(2)
}
precondition(result == 1, "A reply must finish exactly once")
}
for _ in 0..<20 {
let result: Int? = await ServiceRequest.perform(timeout: 0.2) { _, finish in
DispatchQueue.concurrentPerform(iterations: 2) { finish($0) }
}
precondition(result == 0 || result == 1)
}
let timeoutStart = ContinuousClock.now
let timedOut: Int? = await ServiceRequest.perform(timeout: 0.1) { _, _ in }
let timeoutElapsed = timeoutStart.duration(to: .now)
precondition(timedOut == nil && timeoutElapsed >= .milliseconds(80) && timeoutElapsed < .seconds(2))
let cancellationStart = ContinuousClock.now
let (started, signal) = AsyncStream<Void>.makeStream()
let cancelled = Task<Int?, Never> {
await ServiceRequest.perform(timeout: 10) { _, _ in signal.yield(()); signal.finish() }
}
for await _ in started { break }
cancelled.cancel()
let cancelledResult = await cancelled.value
precondition(cancelledResult == nil && cancellationStart.duration(to: .now) < .seconds(2))
let early = Task<Int?, Never> { await ServiceRequest.perform(timeout: 10) { _, _ in } }
early.cancel()
let earlyResult = await early.value
precondition(earlyResult == nil)
}
#else
static let enabled = false
static let omitted = ""
static func run(controller: MenuPanelController, metrics: MetricsStore) {}
#endif
}