feat: add on-demand OpenCode system monitor
Add a Desktop dashboard and text status command for disk space, RAM, CPU and Intel integrated graphics. Include visibility-driven sampling, tests, development tooling, and installation documentation.
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
import { test } from "node:test"
|
||||
import assert from "node:assert/strict"
|
||||
import { createRoot, createSignal } from "solid-js"
|
||||
import { createMonitorClient } from "../dist/client.js"
|
||||
|
||||
const flush = () => new Promise(resolve => setImmediate(resolve))
|
||||
|
||||
function harness(t, customLoad) {
|
||||
let time = 1_800_000_000_000
|
||||
let sequence = 0
|
||||
let calls = 0
|
||||
const timers = new Map()
|
||||
const addTimer = (callback, ms, repeat) => {
|
||||
const id = ++sequence
|
||||
timers.set(id, { callback, at: time + ms, repeat: repeat ? ms : 0 })
|
||||
return id
|
||||
}
|
||||
t.mock.method(globalThis, "setTimeout", (callback, ms) => addTimer(callback, ms, false))
|
||||
t.mock.method(globalThis, "setInterval", (callback, ms) => addTimer(callback, ms, true))
|
||||
t.mock.method(globalThis, "clearTimeout", id => timers.delete(id))
|
||||
t.mock.method(globalThis, "clearInterval", id => timers.delete(id))
|
||||
t.mock.method(Date, "now", () => time)
|
||||
const original = globalThis.document
|
||||
const page = new EventTarget()
|
||||
page.visibilityState = "visible"
|
||||
globalThis.document = page
|
||||
const sample = () => ({
|
||||
sampledAt: time, hostname: "test", platform: "win32", uptime: 1,
|
||||
cpu: { name: "CPU", cores: 1, percent: 10, perCore: [10] },
|
||||
ram: { total: 100, used: 50, free: 50, percent: 50 },
|
||||
disks: { sampledAt: time, items: [], error: null },
|
||||
gpu: { sampledAt: time, adapter: null, error: null },
|
||||
})
|
||||
const state = createRoot(dispose => {
|
||||
const [visible, setVisible] = createSignal(false)
|
||||
const [interval, setInterval] = createSignal(3000)
|
||||
const client = createMonitorClient(() => {
|
||||
calls++
|
||||
return customLoad ? customLoad(sample) : Promise.resolve(sample())
|
||||
}, interval, visible)
|
||||
return { client, setVisible, setInterval, dispose }
|
||||
})
|
||||
t.after(() => { state.dispose(); globalThis.document = original })
|
||||
return {
|
||||
...state, timers, sample, calls: () => calls,
|
||||
windowVisible(value) {
|
||||
page.visibilityState = value ? "visible" : "hidden"
|
||||
page.dispatchEvent(new Event("visibilitychange"))
|
||||
},
|
||||
async advance(ms) {
|
||||
const target = time + ms
|
||||
for (;;) {
|
||||
const next = [...timers].filter(([, timer]) => timer.at <= target).sort((a, b) => a[1].at - b[1].at)[0]
|
||||
if (!next) break
|
||||
const [id, timer] = next
|
||||
time = timer.at
|
||||
if (timer.repeat) timer.at += timer.repeat
|
||||
else timers.delete(id)
|
||||
timer.callback()
|
||||
await flush()
|
||||
}
|
||||
time = target
|
||||
await flush()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
test("No requests or timers until a dashboard opens; closing it stops all polling", async t => {
|
||||
const app = harness(t)
|
||||
await flush()
|
||||
assert.equal(app.calls(), 0)
|
||||
assert.equal(app.timers.size, 0)
|
||||
await app.advance(60_000)
|
||||
await app.client.refresh()
|
||||
assert.equal(app.calls(), 0)
|
||||
|
||||
app.setVisible(true)
|
||||
await flush()
|
||||
assert.equal(app.calls(), 1)
|
||||
assert.equal(app.client.active(), true)
|
||||
await app.advance(3000)
|
||||
assert.equal(app.calls(), 2)
|
||||
app.setVisible(false)
|
||||
assert.equal(app.client.active(), false)
|
||||
assert.equal(app.timers.size, 0)
|
||||
const cached = app.client.snapshot()
|
||||
await app.advance(120_000)
|
||||
assert.equal(app.calls(), 2)
|
||||
assert.equal(app.client.snapshot(), cached)
|
||||
|
||||
app.setVisible(true)
|
||||
await flush()
|
||||
assert.equal(app.calls(), 3)
|
||||
})
|
||||
|
||||
test("Minimizing the window removes timers and showing it resumes only with an open dashboard", async t => {
|
||||
const app = harness(t)
|
||||
app.setVisible(true)
|
||||
await flush()
|
||||
app.windowVisible(false)
|
||||
assert.equal(app.timers.size, 0)
|
||||
await app.advance(30_000)
|
||||
assert.equal(app.calls(), 1)
|
||||
app.windowVisible(true)
|
||||
await flush()
|
||||
assert.equal(app.calls(), 2)
|
||||
app.setVisible(false)
|
||||
app.windowVisible(false)
|
||||
app.windowVisible(true)
|
||||
await app.advance(30_000)
|
||||
assert.equal(app.calls(), 2)
|
||||
assert.equal(app.timers.size, 0)
|
||||
})
|
||||
|
||||
test("A request finishing after closure cannot restart the polling loop", async t => {
|
||||
let resolve
|
||||
const app = harness(t, () => new Promise(done => { resolve = done }))
|
||||
app.setVisible(true)
|
||||
await flush()
|
||||
assert.equal(app.calls(), 1)
|
||||
app.setVisible(false)
|
||||
resolve(app.sample())
|
||||
await flush()
|
||||
assert.equal(app.timers.size, 0)
|
||||
await app.advance(60_000)
|
||||
assert.equal(app.calls(), 1)
|
||||
})
|
||||
|
||||
test("Pause, refresh-rate changes, manual refresh, and cleanup respect visibility", async t => {
|
||||
const app = harness(t)
|
||||
app.setVisible(true)
|
||||
await flush()
|
||||
app.client.setPaused(true)
|
||||
const before = app.calls()
|
||||
await app.advance(30_000)
|
||||
assert.equal(app.calls(), before)
|
||||
await app.client.refresh()
|
||||
assert.equal(app.calls(), before + 1)
|
||||
app.setInterval(10_000)
|
||||
app.client.setPaused(false)
|
||||
await flush()
|
||||
const resumed = app.calls()
|
||||
await app.advance(9000)
|
||||
assert.equal(app.calls(), resumed)
|
||||
await app.advance(1000)
|
||||
assert.equal(app.calls(), resumed + 1)
|
||||
app.dispose()
|
||||
assert.equal(app.timers.size, 0)
|
||||
app.windowVisible(false)
|
||||
app.windowVisible(true)
|
||||
await app.advance(60_000)
|
||||
assert.equal(app.calls(), resumed + 1)
|
||||
})
|
||||
|
||||
test("Closing immediately after opening cancels a queued but not started request", async t => {
|
||||
const app = harness(t)
|
||||
app.setVisible(true)
|
||||
app.setVisible(false)
|
||||
await flush()
|
||||
assert.equal(app.calls(), 0)
|
||||
assert.equal(app.timers.size, 0)
|
||||
})
|
||||
@@ -0,0 +1,72 @@
|
||||
import { test } from "node:test"
|
||||
import assert from "node:assert/strict"
|
||||
import { Monitor, aggregateEngines, cpuUsage } from "../dist/metrics.js"
|
||||
import { intelCountersScript } from "../dist/windows-gpu.js"
|
||||
import { bytes, percent } from "../dist/format.js"
|
||||
|
||||
const intel = "luid_0x00000000_0x000242BD"
|
||||
const nvidia = "luid_0x00000000_0x00024847"
|
||||
const engine = (pid, luid, index, value) => ({
|
||||
Name: `pid_${pid}_${luid}_phys_0_eng_${index}_engtype_3D`,
|
||||
UtilizationPercentage: value,
|
||||
})
|
||||
|
||||
test("Windows engine readings sum processes per engine, never different engines or adapters", () => {
|
||||
assert.equal(aggregateEngines([
|
||||
engine(1, intel, 0, 30), engine(2, intel, 0, 40),
|
||||
engine(1, intel, 1, 55), engine(3, nvidia, 0, 60),
|
||||
]), 70)
|
||||
assert.equal(aggregateEngines([engine(1, intel, 0, 80), engine(2, intel, 0, 40)]), 100)
|
||||
assert.equal(aggregateEngines([]), null)
|
||||
assert.equal(aggregateEngines([{ Name: "invalid", UtilizationPercentage: 99 }]), null)
|
||||
assert.equal(aggregateEngines([engine(1, intel, 0, 0)]), 0)
|
||||
})
|
||||
|
||||
test("CPU counters handle normal deltas, idle, missing intervals, and counter resets", () => {
|
||||
assert.equal(cpuUsage({ idle: 100, total: 200 }, { idle: 150, total: 400 }), 75)
|
||||
assert.equal(cpuUsage({ idle: 100, total: 200 }, { idle: 200, total: 300 }), 0)
|
||||
assert.equal(cpuUsage({ idle: 100, total: 200 }, { idle: 100, total: 200 }), null)
|
||||
assert.equal(cpuUsage({ idle: 100, total: 200 }, { idle: 10, total: 20 }), null)
|
||||
})
|
||||
|
||||
test("Only a DXGI LUID can be inserted in the fixed PowerShell query", () => {
|
||||
assert.match(intelCountersScript(intel), /luid_0x00000000_0x000242BD/)
|
||||
assert.throws(() => intelCountersScript("'; exit; '"), /Invalid GPU adapter identity/)
|
||||
})
|
||||
|
||||
test("Intel-only collection excludes another GPU, coalesces concurrent calls, and caches", { skip: process.platform !== "win32" }, async () => {
|
||||
let calls = 0
|
||||
const collector = new Monitor(async (file, args) => {
|
||||
calls++
|
||||
assert.match(file, /powershell\.exe$/)
|
||||
const script = Buffer.from(args.at(-1), "base64").toString("utf16le")
|
||||
if (script.includes("Add-Type")) return JSON.stringify({ name: "Intel(R) Graphics", luid: intel, sharedLimit: 16 * 1024 ** 3 })
|
||||
if (script.includes("Win32_LogicalDisk")) return JSON.stringify([{ DeviceID: "C:", VolumeName: "OS", Size: 1000, FreeSpace: 750 }])
|
||||
return JSON.stringify({
|
||||
engines: [engine(1, intel, 0, 12), engine(2, intel, 0, 8), engine(3, nvidia, 0, 99)],
|
||||
memory: [{ SharedUsage: 256 * 1024 ** 2 }],
|
||||
})
|
||||
})
|
||||
const first = collector.snapshot()
|
||||
assert.equal(first, collector.snapshot())
|
||||
const snapshot = await first
|
||||
assert.equal(snapshot.gpu.adapter.percent, 20)
|
||||
assert.equal(snapshot.gpu.adapter.sharedUsed, 256 * 1024 ** 2)
|
||||
assert.equal(snapshot.disks.items[0].percent, 25)
|
||||
assert.equal(snapshot.gpu.error, null)
|
||||
assert.equal(await collector.snapshot(), snapshot)
|
||||
assert.equal(calls, 3)
|
||||
})
|
||||
|
||||
test("A missing GPU or failed disk query shows unavailable, never false idle/full values", { skip: process.platform !== "win32" }, async () => {
|
||||
const collector = new Monitor(async () => { throw new Error("Permission denied") })
|
||||
const snapshot = await collector.snapshot()
|
||||
assert.equal(snapshot.gpu.adapter, null)
|
||||
assert.match(snapshot.gpu.error, /Permission denied/)
|
||||
assert.equal(snapshot.disks.items.length, 0)
|
||||
assert.match(snapshot.disks.error, /Permission denied/)
|
||||
assert.ok(snapshot.ram.total > 0)
|
||||
assert.equal(percent(null), "—")
|
||||
assert.equal(bytes(null), "Unavailable")
|
||||
assert.match(bytes(1024 ** 3), /1 GiB/)
|
||||
})
|
||||
Reference in New Issue
Block a user