memoryweave/go/internal/selfoptimize/bench_test.go

181 lines
4.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.

// 织忆 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()
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()
// 预填充 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()
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)])
}
}