- 审计误读订正:deprecated_per_day 是计数器(无分母),不给 _samples/_valid 是对的 - 新增 deprecated_total:区分「今天 0 条」与「从未统计过」(0 的二义性) - executor t8:原 Condition:0.1 从不生效(判据硬编码 >5)→ Condition:5.0 + 读 t.Condition,行为不变 - 单测钉住:RecordDeprecation 双计数、t8 阈值真的可配、计数器不带率键 - docs/metrics-semantics.md:counter/rate/mean 三种读法 + 0 的二义性铁律
54 lines
2.1 KiB
Go
54 lines
2.1 KiB
Go
package selfoptimize
|
||
|
||
import "testing"
|
||
|
||
// 2026-10-26(t_da03e910 终审):把「计数器语义」和「t8 阈值可配」钉住。
|
||
//
|
||
// 背景:审计曾把 deprecated_per_day 当成率指标报「0 样本却 valid=None ⇒ 缺样本门」,
|
||
// 实为探针模型错配(计数器无分母)。真正的缺陷是:
|
||
// ① 0 无法区分「今天没淘汰」与「从来没统计过」→ 加 DeprecatedTotal;
|
||
// ② executor t8 硬编码 >5 且无视 Condition(配置写 0.1 从不生效)。
|
||
|
||
func TestRecordDeprecation_CountsBothTodayAndTotal(t *testing.T) {
|
||
oldToday, oldTotal := Dash.DeprecatedToday, Dash.DeprecatedTotal
|
||
defer func() { Dash.DeprecatedToday, Dash.DeprecatedTotal = oldToday, oldTotal }()
|
||
Dash.DeprecatedToday, Dash.DeprecatedTotal = 0, 100
|
||
|
||
Dash.RecordDeprecation()
|
||
Dash.RecordDeprecation()
|
||
|
||
if Dash.DeprecatedToday != 2 || Dash.DeprecatedTotal != 102 {
|
||
t.Fatalf("today=%d total=%d,期望 2/102:累计值必须跨天保留,日值才归零",
|
||
Dash.DeprecatedToday, Dash.DeprecatedTotal)
|
||
}
|
||
|
||
m := Dash.Metrics()
|
||
if v, ok := m["deprecated_total"]; !ok || v != 102 {
|
||
t.Fatalf("metrics 缺 deprecated_total 或值不对: %v (ok=%v)", v, ok)
|
||
}
|
||
// 计数器不该带 _samples/_valid(有分母才有样本门)
|
||
for _, k := range []string{"deprecated_samples", "deprecated_valid"} {
|
||
if _, bad := m[k]; bad {
|
||
t.Fatalf("计数器不应用率语义键 %s", k)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestTDecayCritical_ReadsConditionNotHardcode(t *testing.T) {
|
||
te := &TriggerExecutor{}
|
||
th := TriggerDef{ID: "t8", Type: TDecayCritical, Condition: 5.0, Enabled: true}
|
||
|
||
if te.evaluate(&th, map[string]float64{"deprecated_per_day": 4}) {
|
||
t.Fatal("4 条/天不应触发(阈值 5)")
|
||
}
|
||
if !te.evaluate(&th, map[string]float64{"deprecated_per_day": 6}) {
|
||
t.Fatal("6 条/天应触发(阈值 5)")
|
||
}
|
||
|
||
// 配置必须真的生效:把阈值调到 10,同样 6 条就不该触发。
|
||
th.Condition = 10.0
|
||
if te.evaluate(&th, map[string]float64{"deprecated_per_day": 6}) {
|
||
t.Fatal("Condition=10 时 6 条不该触发 —— 说明仍在用硬编码阈值")
|
||
}
|
||
}
|