60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / store / concurrency_bench_test.go
1.8 KB
1package store 2 3import ( 4 "math" 5 "path/filepath" 6 "sort" 7 "sync" 8 "sync/atomic" 9 "testing" 10 "time" 11) 12 13func BenchmarkConcurrentWrites(b *testing.B) { 14 dbPath := filepath.Join(b.TempDir(), "bench.db") 15 store, err := Open(dbPath) 16 if err != nil { 17 b.Fatalf("Open() error: %v", err) 18 } 19 defer store.Close() 20 21 mysis, err := store.CreateMysis("bench", "ollama", "llama3", 0.7) 22 if err != nil { 23 b.Fatalf("CreateMysis() error: %v", err) 24 } 25 26 workers := 16 27 durations := make([]time.Duration, b.N) 28 var idx atomic.Int64 29 var errCount atomic.Int64 30 31 tasks := make(chan struct{}) 32 var wg sync.WaitGroup 33 for i := 0; i < workers; i++ { 34 wg.Add(1) 35 go func() { 36 defer wg.Done() 37 for range tasks { 38 start := time.Now() 39 err := store.AddMemory(mysis.ID, MemoryRoleUser, MemorySourceDirect, "bench", "", "") 40 if err != nil { 41 errCount.Add(1) 42 } 43 pos := idx.Add(1) - 1 44 if pos >= 0 && int(pos) < len(durations) { 45 durations[pos] = time.Since(start) 46 } 47 } 48 }() 49 } 50 51 b.ResetTimer() 52 for i := 0; i < b.N; i++ { 53 tasks <- struct{}{} 54 } 55 close(tasks) 56 wg.Wait() 57 b.StopTimer() 58 59 if errCount.Load() > 0 { 60 b.Fatalf("AddMemory errors: %d", errCount.Load()) 61 } 62 63 count := int(idx.Load()) 64 if count == 0 { 65 return 66 } 67 if count < len(durations) { 68 durations = durations[:count] 69 } 70 71 samples := make([]int64, len(durations)) 72 for i, d := range durations { 73 samples[i] = d.Nanoseconds() 74 } 75 sort.Slice(samples, func(i, j int) bool { return samples[i] < samples[j] }) 76 77 median := samples[len(samples)/2] 78 p99Index := int(math.Ceil(float64(len(samples))*0.99)) - 1 79 if p99Index < 0 { 80 p99Index = 0 81 } 82 if p99Index >= len(samples) { 83 p99Index = len(samples) - 1 84 } 85 p99 := samples[p99Index] 86 87 b.ReportMetric(float64(median)/1e6, "p50_ms") 88 b.ReportMetric(float64(p99)/1e6, "p99_ms") 89}