perf(efficiency): 接入真实CPU能量并隔离展示与采样需求
This commit is contained in:
@@ -5,7 +5,23 @@ import Darwin
|
||||
nonisolated enum SamplingDiagnostics {
|
||||
static let enabled = ProcessInfo.processInfo.environment["KYMO_DIAGNOSTICS"] == "1"
|
||||
|
||||
static func record(_ snapshot: MetricsSnapshot, event: String = "sample") {
|
||||
static func recordTextPresentation(processes: Int, cells: Int) {
|
||||
guard enabled else { return }
|
||||
let line = "{\"event\":\"textPresentation\",\"processes\":\(processes),\"createdCells\":\(cells)}\n"
|
||||
try? FileHandle.standardOutput.write(contentsOf: Data(line.utf8))
|
||||
}
|
||||
|
||||
static func recordEnergy(_ snapshot: ProcessEnergySnapshot) {
|
||||
guard enabled else { return }
|
||||
let payload: [String: Any] = ["event": "energy", "recordedAt": Date().timeIntervalSince1970,
|
||||
"state": String(describing: snapshot.state), "candidates": snapshot.candidates,
|
||||
"readable": snapshot.readable, "measured": snapshot.measured,
|
||||
"watts": snapshot.rows.map(\.watts), "intervals": snapshot.rows.map(\.seconds)]
|
||||
guard let data = try? JSONSerialization.data(withJSONObject: payload, options: [.sortedKeys]) else { return }
|
||||
try? FileHandle.standardOutput.write(contentsOf: data + Data([10]))
|
||||
}
|
||||
|
||||
static func record(_ snapshot: MetricsSnapshot, event: String = "sample", hiddenHostAlive: Bool? = nil) {
|
||||
guard enabled else { return }
|
||||
var payload: [String: Any] = [
|
||||
"event": event, "timestamp": snapshot.date.timeIntervalSince1970,
|
||||
@@ -14,6 +30,7 @@ nonisolated enum SamplingDiagnostics {
|
||||
"pid": ProcessInfo.processInfo.processIdentifier,
|
||||
"load": snapshot.load
|
||||
]
|
||||
payload["hiddenHostAlive"] = hiddenHostAlive
|
||||
payload["cpu"] = snapshot.cpu?.usage
|
||||
payload["cores"] = snapshot.cpu?.cores.count
|
||||
payload["cpuTemperature"] = snapshot.cpuTemperature
|
||||
|
||||
Reference in New Issue
Block a user