1
0

feat: 新增本机 CPU checker

- 新增 type: cpu checker,基于 os.cpus() 两次快照计算 CPU 使用率
- 配置项:sampleDuration(默认 1s)、includePerCore(默认 false)
- expect 字段:usagePercent、idlePercent、maxCoreUsagePercent、minCoreUsagePercent、durationMs
- idlePercent 与 usagePercent 互补恒等于 100,百分比范围 0-100
- logicalCoreCount 仅输出到 observation,不作为 expect 字段
- 不暴露 userPercent / systemPercent
- 语义校验禁止 sampleDuration >= timeout
- 支持 AbortSignal 超时取消
- 完整测试覆盖:schema、validate、normalize、resolve、calculate、execute、expect、config-loader
- 新增用户文档 docs/user/checkers/cpu.md
- 更新 checker 索引、配置类型列表、示例配置和 schema
This commit is contained in:
2026-05-26 22:34:57 +08:00
parent f38286d74d
commit c2dcfab80c
22 changed files with 1839 additions and 3 deletions

View File

@@ -0,0 +1,41 @@
import { describe, expect, test } from "bun:test";
import { normalizeTargetExpect } from "../../../../../src/server/checker/runner/cpu/normalize";
describe("normalizeTargetExpect (cpu)", () => {
test("无 expect 直接返回", () => {
const target = { cpu: {}, id: "test", type: "cpu" };
expect(normalizeTargetExpect(target)).toEqual(target);
});
test("expect 为非对象直接返回", () => {
const target = { cpu: {}, expect: "not-an-object", id: "test", type: "cpu" };
expect(normalizeTargetExpect(target)).toEqual(target);
});
test("ValueMatcher 简写展开", () => {
const target = { cpu: {}, expect: { usagePercent: 85 }, id: "test", type: "cpu" };
const result = normalizeTargetExpect(target);
expect((result.expect as Record<string, unknown>)["usagePercent"]).toEqual({ equals: 85 });
});
test("已经是 matcher 对象的不变", () => {
const target = { cpu: {}, expect: { usagePercent: { lte: 85 } }, id: "test", type: "cpu" };
const result = normalizeTargetExpect(target);
expect((result.expect as Record<string, unknown>)["usagePercent"]).toEqual({ lte: 85 });
});
test("多个字段同时展开", () => {
const target = {
cpu: {},
expect: { idlePercent: 15, maxCoreUsagePercent: { lte: 95 }, usagePercent: 85 },
id: "test",
type: "cpu",
};
const result = normalizeTargetExpect(target);
const expectObj = result.expect as Record<string, unknown>;
expect(expectObj["idlePercent"]).toEqual({ equals: 15 });
expect(expectObj["maxCoreUsagePercent"]).toEqual({ lte: 95 });
expect(expectObj["usagePercent"]).toEqual({ equals: 85 });
});
});