refactor(project): 清理闲置展示链路与重复采样状态

This commit is contained in:
2026-11-09 00:41:09 +08:00
parent 7b3abe37c5
commit b73d51c953
57 changed files with 210 additions and 2822 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ import Foundation
preferences.panelMilliseconds = 2_000
preferences.backgroundMilliseconds = 30_000
preferences.backgroundEnabled = true
let store = MetricsStore(sampling: preferences, energyEnabled: false)
let store = MetricsStore(sampling: preferences)
store.setPanelVisible(true)
try await until { store.history.samples.count >= 2 }
store.setPanelVisible(false)
+1 -1
View File
@@ -9,7 +9,7 @@ import Foundation
preferences.panelMilliseconds = 500
preferences.backgroundEnabled = false
let probe = ProcessProbe()
let store = MetricsStore(sampling: preferences, sampler: SystemSampler(processRead: { probe.read(&$0) }), energyEnabled: false)
let store = MetricsStore(sampling: preferences, sampler: SystemSampler(processRead: { probe.read(&$0) }))
store.setPanelVisible(true)
try await until { store.history.samples.count >= 2 }
precondition(probe.count == 0 && store.processSnapshot == nil && store.energy == nil,
+5 -20
View File
@@ -2,28 +2,13 @@ import Foundation
@main struct GPUStatisticsChecks {
static func main() {
func sample(_ time: Double, _ value: Double?, interval: Double = 2, boundary: Bool = false) -> TrendSample {
TrendSample(date: Date(timeIntervalSince1970: time), cpu: nil, gpu: value, memory: nil,
breakBefore: boundary, expectedInterval: interval)
}
let variable = GPUUtilizationSummary(samples: [sample(0, 0), sample(2, 1, interval: 8), sample(10, 1)])
precondition(abs(variable.average! - 0.9) < 0.00001 && variable.peak == 1)
let clipped = GPUUtilizationSummary(samples: [sample(0, 0, interval: 120), sample(120, 1, interval: 120)])
precondition(abs(clipped.average! - 0.75) < 0.00001 && clipped.coverage == 60)
precondition(GPUUtilizationSummary(samples: []).average == nil)
precondition(GPUUtilizationSummary(samples: [sample(0, 1)]).average == nil)
precondition(GPUUtilizationSummary(samples: [sample(0, 1), sample(2, nil), sample(4, 0)]).average == nil)
precondition(GPUUtilizationSummary(samples: [sample(0, 1), sample(2, 0, boundary: true)]).average == nil)
precondition(GPUUtilizationSummary(samples: [sample(0, 1), sample(60, 0)]).average == nil)
precondition(GPUUtilizationSummary(samples: [sample(0, 1), sample(8, 0, interval: 30)]).average == nil,
"A later slow cadence must not reconnect an earlier missed interval")
precondition(GPUMonitor.memoryBytes(NSNumber(value: -1)) == nil)
precondition(GPUMonitor.memoryBytes(NSNumber(value: Double.nan)) == nil)
precondition(GPUMonitor.memoryBytes(NSNumber(value: 0)) == 0)
precondition(GPUMonitor.normalized(101) == nil)
for value in [-1, 101, Double.nan, .infinity] { precondition(GPUMonitor.normalized(value) == nil) }
precondition(GPUMonitor.normalized(nil) == nil)
precondition(GPUMonitor.normalized(0) == 0 && GPUMonitor.normalized(100) == 1)
var snapshot = MetricsSnapshot()
precondition(snapshot.gpuUsage == nil)
snapshot.gpu = GPUMetrics(usage: 0.4, coreCount: 20, renderer: 0.3, tiler: 0.1)
snapshot.gpuUsage = snapshot.gpu?.usage
precondition(snapshot.gpuUsage == 0.4)
var history = MetricHistory()
history.append(snapshot)
precondition(history.samples.last?.gpuRenderer == 0.3 && history.samples.last?.gpuTiler == 0.1)
+1 -2
View File
@@ -36,8 +36,7 @@ import Foundation
let expected = ["en": "Fan", "zh-Hans": "风扇", "zh-Hant": "風扇"][language]!
precondition(title == "\(expected) \(number)", "Numbered fan must use the selected app locale")
}
let average = AppLocalization.string("gpu.recentAverage", locale: locale, bundle: bundle)
precondition(average == ["en": "Last-minute average", "zh-Hans": "近 1 分钟平均", "zh-Hant": "近 1 分鐘平均"][language])
}
print("PASS: system matching, explicit override, fallback, preference roundtrip, built three-language resources")
}
+4 -19
View File
@@ -13,8 +13,6 @@ import QuartzCore
window.contentView = root
let plot = NativePanelCurve.PlotView(); plot.frame = NSRect(x: 0, y: 0, width: 280, height: 60)
root.addSubview(plot)
let ring = NativeMetricRing.RingView(); ring.frame = NSRect(x: 0, y: 70, width: 28, height: 28)
root.addSubview(ring)
window.orderFrontRegardless()
try? await Task.sleep(for: .milliseconds(100))
let dates = (0..<4).map { Date(timeIntervalSinceReferenceDate: Double($0 * 2)) }
@@ -55,13 +53,6 @@ import QuartzCore
precondition(redPixels(bitmap(plot)) > 0)
plot.appearance = NSAppearance(named: .darkAqua)
precondition(redPixels(bitmap(plot)) > 0)
ring.configure(NativeMetricRing.Input(value: 0, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
precondition(redPixels(bitmap(ring), opaque: true) == 0, "Zero ring must not draw a colored arc")
ring.configure(NativeMetricRing.Input(value: 0.25, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
let quarter = redPixels(bitmap(ring), opaque: true)
ring.configure(NativeMetricRing.Input(value: 1, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
let full = redPixels(bitmap(ring), opaque: true)
precondition(quarter > 0 && full > quarter * 2, "Ring fraction must change the rendered arc")
// Sampling updates are final immediately, and hover keeps cached curve geometry.
let nextDates = dates + [Date(timeIntervalSinceReferenceDate: 8)]
let next = NativePanelCurve.Input(frame: PanelCurveFrame(dates: nextDates,
@@ -86,20 +77,14 @@ import QuartzCore
series: [Array(repeating: 0.8, count: 59) + [0.5]], intervals: fullFrame.intervals, boundaryDates: [])
plot.configure(.init(frame: appended, colors: [.red], scales: [1], maximumGap: 5))
precondition(plot.input?.frame.dates.count == 60, "Static history must not retain an outgoing animation point")
ring.configure(.init(value: 0.25, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
precondition(ring.arc.strokeEnd == 0.25, "The arc must immediately show the new value")
let updated = bitmap(ring)
try? await Task.sleep(for: .milliseconds(180))
precondition(pixels(updated) == pixels(bitmap(ring)), "A sampled ring must not interpolate later")
// Adding a series with an unchanged palette must color the new layer too.
plot.configure(.init(frame: PanelCurveFrame(dates: dates, series: [[nil,nil,nil,nil], [0.5,0.5,0.5,0.5]],
intervals: [2,2,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5))
precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) > 10)
assertStatic(plot.layer!)
assertStatic(ring.layer!)
plot.removeFromSuperview(); ring.removeFromSuperview()
plot.removeFromSuperview()
window.orderOut(nil); window.close()
print("PASS: static curve/ring pixels, immediate stable updates, no animation/material/gradient layers, real gaps, hover cache, palette growth, appearance, full history and unmount")
print("PASS: static curve pixels, immediate stable updates, no animation/material/gradient layers, real gaps, hover cache, palette growth, appearance, full history and unmount")
fflush(stdout); NSApp.terminate(nil)
}
NSApp.run()
@@ -125,12 +110,12 @@ import QuartzCore
view.layer!.render(in: context)
return result
}
private static func redPixels(_ image: NSBitmapImageRep, from: Double = 0, to: Double = 1, opaque: Bool = false) -> Int {
private static func redPixels(_ image: NSBitmapImageRep, from: Double = 0, to: Double = 1) -> Int {
var count = 0
for x in Int(Double(image.pixelsWide) * from)..<Int(Double(image.pixelsWide) * to) {
for y in 0..<image.pixelsHigh {
guard let color = image.colorAt(x: x, y: y)?.usingColorSpace(.deviceRGB) else { continue }
if color.redComponent > color.greenComponent + 0.15 && color.redComponent > color.blueComponent + 0.15 && color.alphaComponent > (opaque ? 0.8 : 0.01) { count += 1 }
if color.redComponent > color.greenComponent + 0.15 && color.redComponent > color.blueComponent + 0.15 && color.alphaComponent > 0.01 { count += 1 }
}
}
return count
+1 -18
View File
@@ -4,23 +4,6 @@ import CoreGraphics
@main struct PresentationChecks {
@MainActor static func main() {
let screen = CGRect(x: 0, y: 0, width: 1440, height: 900)
let parent = CGRect(x: 400, y: 400, width: 320, height: 240)
let anchor = CGRect(x: 410, y: 600, width: 300, height: 40)
let size = CGSize(width: 280, height: 180)
let right = DetailPlacement.frame(anchor: anchor, parent: parent, content: size, visible: screen)
precondition(right.minX == parent.maxX + 4 && right.maxY == anchor.maxY)
let edgeParent = CGRect(x: 1100, y: 400, width: 320, height: 240)
let left = DetailPlacement.frame(anchor: anchor, parent: edgeParent, content: size, visible: screen)
precondition(left.maxX == edgeParent.minX - 4)
for visible in [screen, CGRect(x: -1440, y: -200, width: 1440, height: 900), CGRect(x: 0, y: 0, width: 200, height: 100)] {
let result = DetailPlacement.frame(anchor: anchor, parent: parent, content: size, visible: visible)
precondition(visible.contains(result))
}
let highAnchor = CGRect(x: 400, y: 1100, width: 300, height: 40)
precondition(DetailPlacement.frame(anchor: highAnchor, parent: parent, content: size, visible: screen).maxY == screen.maxY)
let lowAnchor = CGRect(x: 400, y: -200, width: 300, height: 40)
precondition(DetailPlacement.frame(anchor: lowAnchor, parent: parent, content: size, visible: screen).minY == screen.minY)
precondition(AppAppearance.system.resolved(systemIsDark: true) == .dark)
precondition(AppAppearance.system.resolved(systemIsDark: false) == .light)
precondition(AppAppearance.light.resolved(systemIsDark: true) == .light)
@@ -42,6 +25,6 @@ import CoreGraphics
precondition(NSApp.appearance === global)
}
window.close()
print("PASS: outward placement, edge fallback, negative screen origins, vertical limits, explicit/system window appearance reset, independent system resolution")
print("PASS: explicit/system window appearance reset, independent system resolution")
}
}
+4 -2
View File
@@ -30,8 +30,10 @@ import Foundation
let first = monitor.update(processes: processes, readings: [reading(0, at: 10), reading(0, at: 10, id: id2)], capability: .verifiedCPU)
precondition(first.state == .waiting && first.rows.isEmpty)
let measured = monitor.update(processes: processes, readings: [reading(1_000_000_000, at: 15), reading(5_000_000_000, at: 15, id: id2)], capability: .verifiedCPU)
precondition(measured.state == .partial && measured.rows.first?.id == id2 && measured.measured == 2)
precondition(measured.rows.first?.watts == 1)
precondition(measured.state == .partial && measured.measured == 2)
let powered = processes.withEnergy(measured)
precondition(powered.rows.first { $0.id == id2 }?.cpuPower == 1)
precondition(powered.rows.first { $0.id == id }?.cpuPower == 0.2)
_ = monitor.update(processes: processes, readings: [], capability: .unavailable)
let recovered = monitor.update(processes: processes, readings: [reading(6_000_000_000, at: 20, id: id2)], capability: .verifiedCPU)
precondition(recovered.rows.isEmpty, "Failure must not bridge a missing interval")
-5
View File
@@ -1,5 +0,0 @@
# 独立检查程序
本目录包含独立 Swift 检查入口和本地化检查脚本,当前没有对应 Xcode 测试 Target。
执行命令、覆盖范围与验收边界统一见[测试与验证](../docs/testing.md)。
+1 -4
View File
@@ -29,12 +29,10 @@ import Foundation
precondition(MetricFormat.watts(12.34) == "12.3 W")
var extended = MetricsSnapshot()
extended.network = [NetworkInterfaceMetrics(name: "en0", received: 100, sent: 50, download: 20, upload: 10)]
extended.disk = DiskMetrics(total: 100, available: 40)
extended.battery = BatteryMetrics(charge: 0.8, charging: false, externalPower: true, power: 0)
var extendedHistory = MetricHistory()
extendedHistory.append(extended)
precondition(extendedHistory.samples.last?.download == 20)
precondition(extendedHistory.samples.last?.disk == 0.6)
precondition(extendedHistory.samples.last?.battery == 0.8)
precondition(extendedHistory.samples.last?.batteryPower == 0)
extended.battery?.power = 12.5
@@ -105,7 +103,6 @@ import Foundation
precondition(cpu.cores.count == ProcessInfo.processInfo.processorCount)
precondition(memory.total == ProcessInfo.processInfo.physicalMemory)
precondition(memory.used <= memory.total)
precondition(sampled.load.count == 3)
await sampler.reset()
let reset = await sampler.sample()
precondition(reset.cpu == nil)
@@ -130,7 +127,7 @@ import Foundation
precondition(store.snapshot.cpu != nil)
store.togglePause()
print("PASS: store publishes, pause freezes snapshot, resume resets baseline")
print("PASS: CPU deltas, wraparound, invalid data, first sample, reset, live CPU/memory/load")
print("PASS: CPU deltas, wraparound, invalid data, first sample, reset, live CPU/memory")
print("Live CPU: \(cpu.usage), memory: \(memory.used)/\(memory.total), swap: \(String(describing: memory.swapUsed)), pressure: \(String(describing: memory.pressure))")
}
@@ -1 +0,0 @@
enum PanelResourceAudit { static let omitted = "" }