fix(host-sensors): one shared Windows sampler, guard late rows, address review

This commit is contained in:
Devvyn committed 2026-10-24 00:05:21 +08:00
1 parent e1d6471808
commit ce7d198b91
9 files changed
+186 -54

No files matched your search

+5 -2
View File
@@ -131,7 +131,7 @@ describe('header host sensors', () => {
expect(appJs).not.toMatch(/\} else \{\s*this\.stopSystemStatsPolling\(\);/);
});
it('is opt-in in the settings page: unchecked chips, off in every default, accepted by the schema', () => {
it('is opt-in in the settings page: unchecked chips, off in every default, stripped from the settings PUT', () => {
const html = read('index.html');
for (const id of ['appSettingsShowGpuStats', 'appSettingsShowCpuMhz']) {
expect(html).toMatch(new RegExp(`<input type="checkbox" id="${id}">`));
@@ -139,6 +139,9 @@ describe('header host sensors', () => {
const settingsUi = read('settings-ui.js');
expect(settingsUi).toContain('showGpuStats ?? defaults.showGpuStats ?? false');
expect(settingsUi).toContain('showCpuMhz ?? defaults.showCpuMhz ?? false');
expect(readFileSync(join(process.cwd(), 'src/web/schemas.ts'), 'utf-8')).toMatch(/showCpuMhz: z\.boolean\(\)/);
// per-device, so stripped from the PUT body like the other header toggles (the schema is .strict())
expect(settingsUi).toContain('showGpuStats: _sgp,');
expect(settingsUi).toContain('showCpuMhz: _scm,');
expect(readFileSync(join(process.cwd(), 'src/web/schemas.ts'), 'utf-8')).not.toContain('showCpuMhz');
});
});
+89
View File
@@ -7,6 +7,8 @@ import { PassThrough } from 'node:stream';
import { describe, expect, it } from 'vitest';
import {
createCounterStream,
createWindowsSource,
windowsCounterList,
meanKhzAsMhz,
parseCpuInfoMhz,
parseDpmClock,
@@ -174,3 +176,90 @@ describe('createCounterStream', () => {
expect(await stream.read(counters)).toBeUndefined();
});
});
describe('the Windows sampler shared by every browser', () => {
const HEADER = '"(PDH-CSV 4.0)","\\\\H\\Processor Information(_Total)\\Processor Frequency"\r\n';
function rig() {
const children: Array<{ stdout: PassThrough; killed: boolean; counters: string[] }> = [];
const stream = createCounterStream({
start: (counters) => {
const stdout = new PassThrough();
const child = { stdout, killed: false, counters };
children.push(child);
return {
stdout,
kill: () => {
child.killed = true;
return true;
},
once: () => undefined,
} as never;
},
firstSampleMs: 20,
});
return { children, source: createWindowsSource(stream) };
}
it('samples the same counters whatever a browser asked for, so two browsers share one process', async () => {
const { children, source } = rig();
// browser A (GPU Stats) and browser B (GPU Stats and CPU MHz) read the same sampler in turn
for (let i = 0; i < 8; i++) {
const read = source.read();
if (i === 0) children[0].stdout.write(HEADER + '"t","1700"\r\n');
await read;
}
expect(children).toHaveLength(1);
expect(children[0].killed).toBe(false);
});
it('adds the GPU counters once, for good, on a machine with no native GPU reader', async () => {
const { children, source } = rig();
void source.read();
expect(children[0].counters).toEqual(windowsCounterList(false));
expect(children[0].counters.some((c) => c.includes('GPU Engine'))).toBe(false);
source.needGpuCounters();
void source.read();
expect(children).toHaveLength(2);
expect(children[0].killed).toBe(true);
expect(children[1].counters).toEqual(windowsCounterList(true));
source.needGpuCounters();
void source.read();
void source.read();
expect(children).toHaveLength(2);
});
});
describe('a late row from a replaced sampler', () => {
it("is ignored, so it can never become the new sampler's header", async () => {
const children: PassThrough[] = [];
let clock = 1000;
const stream = createCounterStream({
start: () => {
const stdout = new PassThrough();
children.push(stdout);
return { stdout, kill: () => true, once: () => undefined } as never;
},
now: () => clock,
firstSampleMs: 20,
recycleMs: 30000,
});
const counters = ['\\Processor Information(_Total)\\Processor Frequency'];
const header = '"(PDH-CSV 4.0)","\\\\H\\Processor Information(_Total)\\Processor Frequency"\r\n';
const first = stream.read(counters);
children[0].write(header + '"t","1700"\r\n');
await first;
clock += 31000;
const second = stream.read(counters); // replaces the sampler
expect(children).toHaveLength(2);
// the killed process still had a data row in its pipe: it arrives after the replacement began
children[0].write('"t","999"\r\n');
await new Promise((r) => setImmediate(r));
// the stale row is not a sample: with nothing from the new process yet there is still nothing to hand out
expect(await stream.read(counters)).toBeUndefined();
children[1].write(header + '"t","1800"\r\n');
await second;
await new Promise((r) => setImmediate(r));
expect(await stream.read(counters)).toEqual({ cpuMhz: 1800 });
});
});