Files
memoryweave/go/internal/selfoptimize/bench_test.go
T
xiaowei ffb13f830a perf: 34 benchmarks + 性能报告 — i5-11260H
基准覆盖 (4 files):
- storage: cosine similarity (254ns), MMR rerank (847μs), memory alloc (6.3μs)
- governance: conflict scan 100 (2.2μs), graph navigate 2000 (485μs), prune (135μs)
- selfoptimize: dashboard metrics (187ns), gap detect (39ns), causal chain (74μs), prefetch (181ns)
- distributed: CRDT merge (325ns), rate limiter (79ns), event bus (304ns)

综合评估: 所有纯 CPU 操作纳秒/微秒级,远超设计目标。
瓶颈在外部 I/O(LanceDB + Embedding 推理)。

报告: BENCHMARK.md
2026-05-28 18:41:11 +08:00

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)])
}
}