Files
membank/auto-check/pkg/stress/benchmark_test.go

247 lines
7.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package stress
import (
"strings"
"testing"
)
// TestEvalBench 验证基准评估逻辑
func TestEvalBench(t *testing.T) {
tests := []struct {
name string
bench MetricBench
val float64
want Level
}{
// CPU 跑分越高越好4 档
{"CPU-远低于", benchCPU, 5000, LevelPoor},
{"CPU-正常", benchCPU, 15000, LevelNormal},
{"CPU-超过", benchCPU, 30000, LevelGood},
{"CPU-远超过", benchCPU, 50000, LevelExcellent},
{"CPU-边界-正常下限", benchCPU, 8000, LevelNormal},
{"CPU-边界-超过下限", benchCPU, 20000, LevelGood},
{"CPU-边界-远超过下限", benchCPU, 40000, LevelExcellent},
// 温度(越低越好)
{"Temp-远超过(危险)", benchTemp, 95, LevelPoor},
{"Temp-偏高", benchTemp, 80, LevelNormal},
{"Temp-正常", benchTemp, 70, LevelGood},
{"Temp-优秀", benchTemp, 50, LevelExcellent},
{"Temp-边界-正常", benchTemp, 90, LevelNormal},
{"Temp-边界-良好", benchTemp, 75, LevelGood},
{"Temp-边界-优秀", benchTemp, 60, LevelExcellent},
// 磁盘 IOPS
{"IOPS-远低于(HDD)", benchDiskIOPS, 500, LevelPoor},
{"IOPS-正常(低端SSD)", benchDiskIOPS, 5000, LevelNormal},
{"IOPS-超过(SATA)", benchDiskIOPS, 30000, LevelGood},
{"IOPS-远超过(NVMe)", benchDiskIOPS, 80000, LevelExcellent},
// 磁盘吞吐
{"BW-远低于(HDD)", benchDiskBW, 50, LevelPoor},
{"BW-正常", benchDiskBW, 300, LevelNormal},
{"BW-超过", benchDiskBW, 1000, LevelGood},
{"BW-远超过", benchDiskBW, 3000, LevelExcellent},
}
for _, tc := range tests {
got := EvalBench(tc.bench, tc.val)
if got != tc.want {
t.Errorf("%s: val=%.0f got=%s want=%s", tc.name, tc.val, got, tc.want)
}
}
}
// TestEvaluateResult 验证 Result 评估
func TestEvaluateResult(t *testing.T) {
// 模拟一次 CPU 压测结果:跑分 25000超过正常最高温度 70°C良好
r := Result{
Type: TestCPU,
Score: 25000,
Metrics: map[string]float64{
"max_temp": 70,
},
}
// 使用 N100 库CPU 跑分阈值 10000/22000/38000 → 25000 介于 22k-38k应为 LevelGood
items := EvaluateResult(r, "Intel N100")
if len(items) != 2 {
t.Fatalf("期望 2 个评估项(跑分+温度),实际 %d", len(items))
}
// 第一项是 CPU 跑分
if items[0].Level != LevelGood {
t.Errorf("CPU 跑分评级错误: got=%s want=%s", items[0].Level, LevelGood)
}
// 第二项是温度70°C 在 60-75 之间,应为 LevelGood
if items[1].Level != LevelGood {
t.Errorf("温度评级错误: got=%s want=%s", items[1].Level, LevelGood)
}
// 综合评级应取最差
if OverallLevel(items) != LevelGood {
t.Errorf("综合评级错误: got=%s", OverallLevel(items))
}
}
// TestEvaluateResultDisk 验证磁盘测试评估
func TestEvaluateResultDisk(t *testing.T) {
// 模拟一次磁盘压测:吞吐 1500MB/s良好IOPS 80000远超过
r := Result{
Type: TestDiskIO,
Score: 1500, // score 是 bw_total
Metrics: map[string]float64{
"iops_total": 80000,
"bw_total": 1500,
},
}
// 磁盘阈值与 CPU 型号无关,传任意库内型号都能正常评估
items := EvaluateResult(r, "Intel N100")
if len(items) != 2 {
t.Fatalf("期望 2 个评估项,实际 %d", len(items))
}
// 找 IOPS 项
var iops, bw BenchItem
for _, it := range items {
switch it.Key {
case "iops_total":
iops = it
case "bw_total":
bw = it
}
}
if iops.Level != LevelExcellent {
t.Errorf("IOPS 80000 应评为远超过正常: got=%s", iops.Level)
}
if bw.Level != LevelGood {
t.Errorf("吞吐 1500 应评为超过正常: got=%s", bw.Level)
}
}
// TestMatchHardware 验证硬件库匹配
func TestMatchHardware(t *testing.T) {
tests := []struct {
cpu string
wantNil bool
wantNote string // 仅检查 Note 包含的子串
}{
// 飞牛常用低功耗
{"Intel(R) Celeron(R) N100 CPU @ 0.80GHz", false, "N100"},
{"Intel N305", false, "N305"},
{"12th Gen Intel(R) Core(TM) i3-12100", false, "12代 i3"},
{"13th Gen Intel(R) Core(TM) i5-13500", false, "13代 i5"},
{"11th Gen Intel(R) Core(TM) i7-11700K", false, "11代 i7"},
{"Intel(R) Core(TM) i9-12900K CPU", false, "12代 i9"},
{"Intel(R) Celeron(R) J4125 CPU @ 2.00GHz", false, "J4125"},
{"Intel(R) Celeron(R) N5095 @ 2.00GHz", false, "N5095"},
// AMD
{"AMD Ryzen 5 5600 6-Core Processor", false, "Ryzen 5"},
{"AMD Ryzen 7 5800X 8-Core", false, "Ryzen 7"},
{"AMD Ryzen 9 5900X", false, "Ryzen 9"},
// 大小写不敏感
{"INTEL N100", false, "N100"},
{"intel celeron n100", false, "N100"},
// 不在库中
{"", true, ""},
{"Some Unknown CPU XYZ", true, ""},
{"Apple M1", true, ""},
{"Loongson 3A5000", true, ""},
}
for _, tc := range tests {
got := MatchHardware(tc.cpu)
if tc.wantNil {
if got != nil {
t.Errorf("cpu=%q 应返回 nil得到 %v (Note=%s)", tc.cpu, got, got.Note)
}
continue
}
if got == nil {
t.Errorf("cpu=%q 应匹配到硬件,得到 nil", tc.cpu)
continue
}
if !strings.Contains(got.Note, tc.wantNote) {
t.Errorf("cpu=%q 匹配到 %q但 Note 应包含 %q", tc.cpu, got.Note, tc.wantNote)
}
}
}
// TestMatchHardware_Priority 验证匹配顺序优先级i5-12xxx 必须优先于 i5 通用)
func TestMatchHardware_Priority(t *testing.T) {
hb := MatchHardware("Intel Core i5-12400")
if hb == nil {
t.Fatal("i5-12400 应匹配到硬件")
}
if !strings.Contains(hb.Note, "12代 i5") {
t.Errorf("i5-12400 应优先匹配 12代 i5实际 Note=%s", hb.Note)
}
// 不带代号的 i5 应匹配通用条目
hb = MatchHardware("Intel Core i5-4400") // 老 4 代
if hb == nil {
t.Fatal("i5-4400 应匹配到硬件")
}
if !strings.Contains(hb.Note, "i5") {
t.Errorf("i5-4400 应匹配 i5 系列,实际 Note=%s", hb.Note)
}
}
// TestEvaluateResult_HardwareAware 验证同一分数在不同硬件下的评级不同
func TestEvaluateResult_HardwareAware(t *testing.T) {
r := Result{Type: TestCPU, Score: 25000}
mr := map[string]float64{"max_temp": 70}
// N100阈值 10000/22000/38000 → 25000 介于 22k-38k → Good
rN100 := r
rN100.Metrics = mr
itemsN100 := EvaluateResult(rN100, "Intel N100")
if itemsN100[0].Level != LevelGood {
t.Errorf("N100 跑分 25k 应为 Good实际 %s", itemsN100[0].Level)
}
// i3-12100阈值 18000/38000/62000 → 25000 介于 18k-38k → Normal
itemsI3 := EvaluateResult(r, "Intel Core i3-12100")
if itemsI3[0].Level != LevelNormal {
t.Errorf("i3-12100 跑分 25k 应为 Normal实际 %s", itemsI3[0].Level)
}
// i7-12700阈值 32000/70000/115000 → 25000 < 32k → Poor
itemsI7 := EvaluateResult(r, "Intel Core i7-12700")
if itemsI7[0].Level != LevelPoor {
t.Errorf("i7-12700 跑分 25k 应为 Poor明显低于 i7 水平),实际 %s", itemsI7[0].Level)
}
}
// TestEvaluateResult_UnknownHardware 验证未在库中的硬件全部返回 normal
func TestEvaluateResult_UnknownHardware(t *testing.T) {
r := Result{
Type: TestCPU,
Score: 25000,
Metrics: map[string]float64{
"max_temp": 70,
"iops_total": 80000,
"bw_total": 1500,
},
}
// 1) 空字符串
items := EvaluateResult(r, "")
if len(items) != 4 {
t.Fatalf("空 cpuModel 应返回 4 项,实际 %d", len(items))
}
for _, it := range items {
if it.Level != LevelNormal {
t.Errorf("空 cpuModel 时 %s 应为 Normal实际 %s", it.Label, it.Level)
}
}
// 2) 未知型号
items = EvaluateResult(r, "SomeUnknownCPU XYZ")
if len(items) != 4 {
t.Fatalf("未知 cpuModel 应返回 4 项,实际 %d", len(items))
}
for _, it := range items {
if it.Level != LevelNormal {
t.Errorf("未知 cpuModel 时 %s 应为 Normal实际 %s", it.Label, it.Level)
}
}
}