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,77 @@
import Ajv from "ajv";
import { describe, expect, test } from "bun:test";
import { cpuCheckerSchemas } from "../../../../../src/server/checker/runner/cpu/schema";
const ajv = new Ajv({ strict: false });
describe("CPU checker schema", () => {
test("authoring config 允许变量引用", () => {
const validate = ajv.compile(cpuCheckerSchemas.authoring.config);
expect(validate({ includePerCore: "${per_core|false}", sampleDuration: "${sample_dur|1s}" })).toBe(true);
});
test("normalized config 允许合法值", () => {
const validate = ajv.compile(cpuCheckerSchemas.normalized.config);
expect(validate({ includePerCore: true, sampleDuration: "1s" })).toBe(true);
});
test("normalized config 空配置通过", () => {
const validate = ajv.compile(cpuCheckerSchemas.normalized.config);
expect(validate({})).toBe(true);
});
test("config 拒绝额外字段", () => {
const validate = ajv.compile(cpuCheckerSchemas.authoring.config);
expect(validate({ extraField: true })).toBe(false);
});
test("authoring expect 允许 ValueMatcher 简写", () => {
const validate = ajv.compile(cpuCheckerSchemas.authoring.expect);
expect(validate({ usagePercent: 85 })).toBe(true);
expect(validate({ usagePercent: { lte: 85 } })).toBe(true);
});
test("normalized expect 允许 matcher 对象", () => {
const validate = ajv.compile(cpuCheckerSchemas.normalized.expect);
expect(validate({ idlePercent: { gte: 15 }, usagePercent: { lte: 85 } })).toBe(true);
});
test("expect 拒绝 logicalCoreCount 字段", () => {
const validate = ajv.compile(cpuCheckerSchemas.authoring.expect);
expect(validate({ logicalCoreCount: { gte: 4 } })).toBe(false);
});
test("expect 拒绝 userPercent 字段", () => {
const validate = ajv.compile(cpuCheckerSchemas.authoring.expect);
expect(validate({ userPercent: { lte: 50 } })).toBe(false);
});
test("expect 拒绝 systemPercent 字段", () => {
const validate = ajv.compile(cpuCheckerSchemas.authoring.expect);
expect(validate({ systemPercent: { lte: 50 } })).toBe(false);
});
test("expect 允许所有合法字段", () => {
const validate = ajv.compile(cpuCheckerSchemas.normalized.expect);
expect(
validate({
durationMs: { lte: 2000 },
idlePercent: { gte: 15 },
maxCoreUsagePercent: { lte: 95 },
minCoreUsagePercent: { gte: 0 },
usagePercent: { lte: 85 },
}),
).toBe(true);
});
test("expect 拒绝额外字段", () => {
const validate = ajv.compile(cpuCheckerSchemas.normalized.expect);
expect(validate({ unknownField: 1 })).toBe(false);
});
test("expect 空对象通过", () => {
const validate = ajv.compile(cpuCheckerSchemas.normalized.expect);
expect(validate({})).toBe(true);
});
});