perf(menubar): 减少进程表刷新排序并清理闲置代码

This commit is contained in:
2026-11-16 00:06:14 +08:00
parent 97c1bc3b7f
commit 3ee6b45eb4
7 changed files with 43 additions and 73 deletions
+19
View File
@@ -34,6 +34,25 @@ import Foundation
let sorted = ProcessTableProjection.rows([row, high, low, zero], query: "", sorting: [ProcessSort(key: .power, order: direction)], order: [])
precondition(sorted.last?.cpuPower == nil && sorted.first?.cpuPower == (direction == .forward ? 0 : 3))
}
// The selected process can exit while its PID is reused. The replacement
// joins the sorted newcomers, while retained rows keep fresh measurements.
let fresh = ProcessMetrics(id: large.id, name: large.name, path: nil, cpu: 1,
footprintBytes: nil, threads: 2, restricted: false)
let newcomer = ProcessMetrics(id: .init(pid: 44, seconds: 10, microseconds: 0),
name: "new", path: nil, cpu: 2, footprintBytes: nil,
threads: 1, restricted: false)
let held = ProcessTableProjection.rows([replacement, newcomer, fresh], query: "",
sorting: [ProcessSort(key: .cpu)], order: [id, large.id])
precondition(held.map(\.id) == [large.id, newcomer.id, reused])
precondition(held.first?.threads == 2 && held.first?.footprintBytes == nil)
let filteredHeld = ProcessTableProjection.rows([row, fresh], query: "large", sorting: [], order: [id, large.id])
precondition(filteredHeld.map(\.id) == [large.id])
let nearlyMax = ProcessMetrics(id: id, name: "a", path: nil, cpu: nil,
footprintBytes: UInt64.max - 1, threads: nil, restricted: false)
let max = ProcessMetrics(id: large.id, name: "z", path: nil, cpu: nil,
footprintBytes: UInt64.max, threads: nil, restricted: false)
precondition(ProcessSort(key: .memory).compare(nearlyMax, max) == .orderedDescending)
print("PASS: filtered/live selection, absent selection/snapshot, failed enumeration, actual exit and PID reuse")
}
}
-10
View File
@@ -8,16 +8,6 @@ import Foundation
precondition(ProcessMonitor.cpu(current: 1, previous: 0, elapsed: 0) == nil)
precondition(GPUMonitor.normalized(53) == 0.53)
precondition(GPUMonitor.normalized(101) == nil && GPUMonitor.normalized(.nan) == nil)
let processRows = [
ProcessMetrics(id: ProcessIdentity(pid: 1, seconds: 1, microseconds: 1), name: "unknown", path: nil, cpu: nil, footprintBytes: nil, threads: nil, restricted: true),
ProcessMetrics(id: ProcessIdentity(pid: 2, seconds: 1, microseconds: 2), name: "busy", path: nil, cpu: 2.5, footprintBytes: nil, threads: nil, restricted: false),
ProcessMetrics(id: ProcessIdentity(pid: 3, seconds: 1, microseconds: 3), name: "idle", path: nil, cpu: 0, footprintBytes: nil, threads: nil, restricted: false),
ProcessMetrics(id: ProcessIdentity(pid: 4, seconds: 1, microseconds: 4), name: "invalid", path: nil, cpu: -.infinity, footprintBytes: nil, threads: nil, restricted: false)
]
let processSnapshot = ProcessSnapshot(date: Date(), enumerated: 4, readable: 3, rows: processRows, failed: false)
precondition(processSnapshot.highestCPUProcess?.name == "busy")
precondition(processSnapshot.highestCPUProcesses(limit: 3).map(\.name) == ["busy", "idle"])
precondition(processSnapshot.highestCPUProcesses(limit: 0).isEmpty)
var snapshot = MetricsSnapshot()
snapshot.network = [NetworkInterfaceMetrics(name: "en0", received: 1, sent: 1, download: 12, upload: 8),
NetworkInterfaceMetrics(name: "utun0", received: 1, sent: 1, download: 99, upload: 99)]