Files
memoryweave/go/internal/selfoptimize/bench_test.go
T
小唯 bea2849494 fix(tests): 修 pre-existing 测试失配,恢复 make test 全绿 (t_8496e8b6)
四处均为本任务之前就存在的测试漂移(与优化B 无关,但让 go test ./... 无法通过):

1. selfoptimize: 2a350cf 给 NewGapDetector 增加了 (emb, ldb) 两个参数,
   8 处测试调用点未跟进,整包 test 构建失败。测试只用到 RecordMiss/Close/List,
   不需要真实依赖,传 nil, nil 即可。
2. integration_test.go:296 selfoptimize.NewGapDetector() 同上。
3. integration_test.go:323 InMemoryGraph.Navigate 新增 relFilter 参数,补 nil。
4. integration_test.go:339 /api/v1/triggers/fire 的夹具用了 "t1"(那是 executor
   内部编号),routes.Triggers 的真实 ID 是 t_decay/t_distill/t_merge 等,
   导致 Fire 返回 404 trigger not found。夹具改用 t_decay。

验证: go vet ./... 无输出; go test ./... -count=1 全部 ok(根包 8.1s)
2026-09-12 18:07:40 +08:00

181 lines
4.6 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.
// 织忆 MemoryWeave — 自优化层性能基准
package selfoptimize
import (
"fmt"
"testing"
)
// ─── 仪表盘 ──────────────────────────────────────────────
func BenchmarkDashboard_Metrics(b *testing.B) {
d := &Dashboard{
UsefulCount: 850,
NotUsefulCount: 150,
TotalRecalls: 5000,
HitCount: 4250,
ClosedGaps: 30,
TotalGaps: 45,
CascadeFixedTotal: 12,
TotalFixes: 20,
DeprecatedToday: 5,
DistillLossSum: 8.5,
DistillLossCount: 25,
AutoResolvedConflicts: 15,
TotalConflicts: 25,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
d.Metrics()
}
}
func BenchmarkDashboard_RecordRecall_Parallel(b *testing.B) {
d := &Dashboard{}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
d.RecordRecall(true)
}
})
}
func BenchmarkDashboard_RecordFeedback_Parallel(b *testing.B) {
d := &Dashboard{}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
d.RecordFeedback(true)
}
})
}
// ─── 缺口检测 ────────────────────────────────────────────
func BenchmarkGapDetector_RecordMiss(b *testing.B) {
gd := NewGapDetector(nil, nil)
topics := []string{"config", "memory", "system", "docker", "nginx"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
gd.RecordMiss(topics[i%len(topics)])
}
}
func BenchmarkGapDetector_List(b *testing.B) {
gd := NewGapDetector(nil, nil)
// 预填充 100 个缺口
for i := 0; i < 100; i++ {
topic := fmt.Sprintf("gap-%d", i)
for j := 0; j < 3; j++ {
gd.RecordMiss(topic)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
gd.List()
}
}
func BenchmarkGapDetector_Close(b *testing.B) {
gd := NewGapDetector(nil, nil)
for i := 0; i < 100; i++ {
topic := fmt.Sprintf("close-%d", i)
for j := 0; j < 3; j++ {
gd.RecordMiss(topic)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
gd.Close(fmt.Sprintf("close-%d", i%100))
}
}
// ─── 因果追踪 ────────────────────────────────────────────
func BenchmarkCausalTracker_RecordVersion(b *testing.B) {
ct := NewCausalTracker()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ct.RecordVersion(
fmt.Sprintf("mem-%d", i%100),
fmt.Sprintf("content v%d", i),
"muchen_oral",
"manual_correction",
)
}
}
func BenchmarkCausalTracker_GetAffected(b *testing.B) {
ct := NewCausalTracker()
// 构建链:mem-0 → mem-1 → mem-2 → ... → mem-99
for i := 0; i < 100; i++ {
ct.RecordVersion(fmt.Sprintf("mem-%d", i), fmt.Sprintf("content-%d", i), "llm_distill", "auto")
if i > 0 {
ct.AddDependency(fmt.Sprintf("mem-%d", i), fmt.Sprintf("mem-%d", i-1))
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ct.GetAffected("mem-50", nil)
}
}
func BenchmarkCausalTracker_IsVolatile(b *testing.B) {
ct := NewCausalTracker()
// 预填充:mem-0 有 5 个版本(volatile)
for i := 0; i < 5; i++ {
ct.RecordVersion("mem-0", fmt.Sprintf("v%d", i), "muchen_correction", "fix")
}
// mem-1 只有 1 个版本(stable)
ct.RecordVersion("mem-1", "v1", "config_parse", "init")
b.ResetTimer()
for i := 0; i < b.N; i++ {
if i%2 == 0 {
ct.IsVolatile("mem-0")
} else {
ct.IsVolatile("mem-1")
}
}
}
// ─── 记忆预取 ────────────────────────────────────────────
func BenchmarkPrefetchGraph_RecordCoAccess(b *testing.B) {
pg := NewPrefetchGraph()
pairs := [][2]string{
{"docker", "nginx"},
{"docker", "kubernetes"},
{"nginx", "ssl"},
{"system", "config"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
p := pairs[i%len(pairs)]
pg.RecordCoAccess(p[0], p[1])
}
}
func BenchmarkPrefetchGraph_GetPrefetch(b *testing.B) {
pg := NewPrefetchGraph()
// 预填充:docker → nginx (100次), docker → kubernetes (50次)
for i := 0; i < 100; i++ {
pg.RecordCoAccess("docker", "nginx")
}
for i := 0; i < 50; i++ {
pg.RecordCoAccess("docker", "kubernetes")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
pg.GetPrefetch("docker")
}
}
// ─── 来源信任度 ──────────────────────────────────────────
func BenchmarkSourceTrust(b *testing.B) {
sources := []string{"muchen_oral", "config_parse", "agent_infer", "llm_distill"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
SourceTrust(sources[i%len(sources)])
}
}