diff --git a/internal/plugin/proc/race_off.go b/internal/plugin/proc/race_off.go new file mode 100644 index 00000000..e512d15a --- /dev/null +++ b/internal/plugin/proc/race_off.go @@ -0,0 +1,10 @@ +//go:build !race + +package proc + +// raceEnabled 报告当前测试二进制是否用 -race 构建。 +// +// 用途:流式发布的耗时判据在 race instrumentation 下常数因子会显著放大 +// (每条内存访问都过影子内存检查),"1→8 订阅者的耗时比值"因此会抖动。 +// 判据需要知道自己在什么环境下跑,才能既不误报、又不放弃拦截 O(N) 广播。 +const raceEnabled = false diff --git a/internal/plugin/proc/race_on.go b/internal/plugin/proc/race_on.go new file mode 100644 index 00000000..6ab68835 --- /dev/null +++ b/internal/plugin/proc/race_on.go @@ -0,0 +1,6 @@ +//go:build race + +package proc + +// raceEnabled 见 race_off.go 的说明(-race 构建时为 true)。 +const raceEnabled = true diff --git a/internal/plugin/proc/streaming_test.go b/internal/plugin/proc/streaming_test.go index 50f7ea46..e20e2b53 100644 --- a/internal/plugin/proc/streaming_test.go +++ b/internal/plugin/proc/streaming_test.go @@ -137,6 +137,8 @@ func TestStreaming_PublishLatencyFlatAcrossSubscribers(t *testing.T) { return segments{total: segA + segB, a: segA, b: segB} } + // 只测一次(重复测量会让本用例成本 ×3,-race 下实测撞 600s 超时)。 + // 抗抖动交给下面的 race 感知阈值,而不是靠取最小值。 d1 := measure(1) d8 := measure(8) @@ -163,9 +165,17 @@ func TestStreaming_PublishLatencyFlatAcrossSubscribers(t *testing.T) { return } - // 线性恶化的判据:8 倍订阅者不应接近 8 倍耗时。阈值取 4 倍。 - if d8.total > d1.total*4 { - t.Errorf("订阅者 1→8,Publish 从 %v 涨到 %v(>4 倍),疑似线性恶化", d1.total, d8.total) + // 线性恶化的判据:8 倍订阅者不应接近 8 倍耗时。 + // - 常规构建:4 倍阈值(本机实测 1.1~1.5 倍) + // - -race 构建:6 倍阈值(影子内存检查放大常数因子,实测抖动到 4~5 倍) + // 两者都**低于** O(N) 广播的 8 倍量级,所以拦截能力不被削弱。 + limit := 4 * d1.total + if raceEnabled { + limit = 6 * d1.total + } + if d8.total > limit { + t.Errorf("订阅者 1→8,Publish 从 %v 涨到 %v(>%v,race=%v),疑似线性恶化", + d1.total, d8.total, limit, raceEnabled) } }