61 lines
4.9 KiB
Swift
61 lines
4.9 KiB
Swift
import Foundation
|
|
|
|
@main struct ProcessEnergyChecks {
|
|
static let id = ProcessIdentity(pid: 42, seconds: 100, microseconds: 2)
|
|
static func reading(_ energy: UInt64?, at time: Double, id: ProcessIdentity = id,
|
|
birth: UInt64 = 1, status: EnergyReadStatus = .available) -> ProcessEnergyReading {
|
|
ProcessEnergyReading(id: id, birth: birth, uptime: time, nanojoules: energy, status: status)
|
|
}
|
|
static func main() {
|
|
let wire = ProcessEnergyReply(token: UUID(), capability: .verifiedCPU, readings: [reading(UInt64.max - 1, at: 10)])
|
|
let decoded = try! JSONDecoder().decode(ProcessEnergyReply.self, from: JSONEncoder().encode(wire))
|
|
precondition(decoded.readings[0].nanojoules == UInt64.max - 1 && decoded.token == wire.token)
|
|
let old = reading(UInt64.max - 2_000_000_000, at: 10)
|
|
let now = reading(UInt64.max - 1_000_000_000, at: 15)
|
|
precondition(ProcessEnergyMonitor.watts(current: now, previous: nil) == nil)
|
|
precondition(ProcessEnergyMonitor.watts(current: now, previous: old)?.0 == 0.2)
|
|
precondition(ProcessEnergyMonitor.watts(current: old, previous: now) == nil)
|
|
precondition(ProcessEnergyMonitor.watts(current: reading(5, at: 15), previous: reading(5, at: 10))?.0 == 0)
|
|
for invalid in [reading(nil, at: 15, status: .denied), reading(10, at: 10), reading(10, at: .nan),
|
|
reading(10, at: 50), reading(10, at: 15, birth: 2),
|
|
reading(10, at: 15, id: ProcessIdentity(pid: 42, seconds: 101, microseconds: 2))] {
|
|
precondition(ProcessEnergyMonitor.watts(current: invalid, previous: reading(5, at: 10)) == nil)
|
|
}
|
|
func row(_ id: ProcessIdentity, _ cpu: Double) -> ProcessMetrics {
|
|
ProcessMetrics(id: id, name: "fixture", path: nil, cpu: cpu, footprintBytes: nil, threads: nil, restricted: false)
|
|
}
|
|
let id2 = ProcessIdentity(pid: 43, seconds: 100, microseconds: 2)
|
|
let processes = ProcessSnapshot(date: Date(), enumerated: 3, readable: 2, rows: [row(id, 5), row(id2, 0.01)], failed: false)
|
|
var monitor = ProcessEnergyMonitor()
|
|
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)
|
|
_ = 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")
|
|
let unsupported = monitor.update(processes: processes, readings: [], capability: .unsupported)
|
|
precondition(unsupported.state == .unsupported && unsupported.rows.isEmpty)
|
|
let unverified = monitor.update(processes: processes, readings: [reading(0, at: 25)], capability: .unverified)
|
|
precondition(unverified.rows.isEmpty && unverified.readable == 0)
|
|
// Keep more than three measured processes and join by full identity, never PID alone.
|
|
let manyRows = (0..<12).map { row(ProcessIdentity(pid: Int32(100 + $0), seconds: 1, microseconds: 0), 0) }
|
|
let many = ProcessSnapshot(date: Date(), enumerated: 12, readable: 12, rows: manyRows, failed: false)
|
|
_ = monitor.update(processes: many, readings: manyRows.map { reading(0, at: 10, id: $0.id) }, capability: .verifiedCPU)
|
|
let all = monitor.update(processes: many, readings: manyRows.enumerated().map {
|
|
reading(UInt64($0.offset) * 1_000_000_000, at: 15, id: $0.element.id)
|
|
}, capability: .verifiedCPU)
|
|
precondition(all.rows.count == 12 && all.measured == 12 && all.state == .available)
|
|
let joined = many.withEnergy(all)
|
|
precondition(joined.rows[0].cpuPower == 0 && joined.rows[11].cpuPower == 2.2)
|
|
precondition(joined.rows[1].formattedPower == "200.0 mW" && joined.rows[11].formattedPower == "2.20 W")
|
|
var reused = manyRows[0]
|
|
reused = row(ProcessIdentity(pid: reused.id.pid, seconds: 2, microseconds: 0), 0)
|
|
let replacement = ProcessSnapshot(date: Date(), enumerated: 1, readable: 1, rows: [reused], failed: false)
|
|
precondition(replacement.withEnergy(all).rows[0].cpuPower == nil)
|
|
precondition(joined.withEnergy(nil).rows.allSatisfy { $0.cpuPower == nil && $0.powerWindow == nil })
|
|
print("PASS: exact large-integer deltas, first/zero/reset/reuse/gap/error/time boundaries, energy-vs-CPU ranking, partial coverage and no CPU fallback")
|
|
}
|
|
}
|