memoryweave/go/internal/distributed/bench_test.go

118 lines
2.9 KiB
Go

// 织忆 MemoryWeave — API/分布式层性能基准
package distributed
import (
"testing"
"time"
)
// ─── CRDT 合并 ────────────────────────────────────────────
func BenchmarkCRDT_Merge(b *testing.B) {
crdt := &CRDTMerge{}
a := map[string]interface{}{
"id": "mem-001",
"content": "system uses Linux",
"source": "config_parse",
"version": 2,
"updated_at": time.Now().Format(time.RFC3339),
}
bNode := map[string]interface{}{
"id": "mem-001",
"content": "system uses Deepin",
"source": "muchen_oral",
"version": 1,
"updated_at": time.Now().Add(-1 * time.Hour).Format(time.RFC3339),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
crdt.Merge(a, bNode)
}
}
func BenchmarkCRDT_Merge_TimestampFirst(b *testing.B) {
crdt := &CRDTMerge{}
older := map[string]interface{}{
"id": "mem-002",
"content": "old value",
"source": "agent_infer",
"updated_at": time.Now().Add(-24 * time.Hour).Format(time.RFC3339),
}
newer := map[string]interface{}{
"id": "mem-002",
"content": "new value",
"source": "agent_infer",
"updated_at": time.Now().Format(time.RFC3339),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
crdt.Merge(older, newer)
}
}
// ─── 速率限制 ────────────────────────────────────────────
func BenchmarkRateLimiter_Allow(b *testing.B) {
rl := NewRateLimiter(100, 200) // 100 QPS, burst 200
b.ResetTimer()
for i := 0; i < b.N; i++ {
rl.Allow("agent-1")
}
}
func BenchmarkRateLimiter_Allow_Parallel(b *testing.B) {
rl := NewRateLimiter(1000, 2000)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
rl.Allow("agent-1")
}
})
}
func BenchmarkRateLimiter_MultiAgent(b *testing.B) {
rl := NewRateLimiter(100, 200)
agents := []string{"hermes", "openclaw", "cron", "test-1", "test-2"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
rl.Allow(agents[i%len(agents)])
}
}
// ─── 事件总线 ────────────────────────────────────────────
func BenchmarkEventBus_Publish(b *testing.B) {
eb := NewEventBus(nil)
received := 0
eb.Subscribe("memory_committed", func(e Event) {
received++
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
eb.Publish(Event{
ID: "evt-001",
Type: "memory_committed",
AgentID: "hermes",
Timestamp: time.Now(),
})
}
}
func BenchmarkEventBus_Publish_Parallel(b *testing.B) {
eb := NewEventBus(nil)
eb.Subscribe("gap_found", func(e Event) {})
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
eb.Publish(Event{
ID: "evt-001",
Type: "gap_found",
AgentID: "hermes",
Timestamp: time.Now(),
})
}
})
}