Files
memoryweave/go/internal/selfoptimize/bench_test.go
T
小唯 886c0c20e2 style(gofmt): 全仓 66 文件对齐 gofmt 1.25
gofmt -l 实测 66 文件不合规(历史遗留+gofmt 1.25 规范化:
单行 if/for/case 展开成块、Go1.19+ doc 注释 // 与段落 // 空行)。
本 commit 只含格式化,零逻辑改动:
- 66/66 文件「去注释+去空白+分号归一」后 token 流与 HEAD 完全一致
- go build ./... exit 0;go vet ./... exit 0(改前后一致)
- go test ./... 17 包全 ok(改前后一致)
验收: gofmt -l . 为空;git diff -w 非空系 gofmt 1.25 非纯空白特性,
非逻辑改动(等价性以 token 流 + test 兜底)。
2026-10-10 15:51:25 +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)])
}
}