-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_bench_test.go
More file actions
185 lines (174 loc) · 5.38 KB
/
Copy pathmemory_bench_test.go
File metadata and controls
185 lines (174 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"testing"
)
// mustBenchStore creates a MemoryStore for benchmarks (no storage, no summarizer).
func mustBenchStore(b *testing.B) *MemoryStore {
b.Helper()
s, err := NewMemoryStore(MemoryStoreOptions{})
if err != nil {
b.Fatalf("NewMemoryStore() error: %v", err)
}
return s
}
// seedStore adds n memories with realistic text content.
func seedStore(b *testing.B, s *MemoryStore, n int) {
b.Helper()
topics := []string{
"Go concurrency patterns with goroutines and channels for parallel processing",
"SQLite WAL mode enables concurrent readers with single writer for better performance",
"TF-IDF cosine similarity scoring for text search and document matching",
"REST API design with Gin framework using JSON middleware and error handling",
"Memory management in Go with sync.RWMutex for concurrent read-write access",
"Edge-based similarity graph for finding related memories and neighbors",
"Export import round-trip preserving IDs timestamps and metadata fidelity",
"Auto-consolidation of duplicate memories using high similarity threshold",
"MCP protocol tools for persistent AI memory across conversation sessions",
"Token indexing with inverted index for fast lexical candidate lookup",
}
for i := range n {
text := fmt.Sprintf("#fact Memory %d: %s", i, topics[i%len(topics)])
if _, _, err := s.Add(text, "", "fact", nil); err != nil {
b.Fatalf("seed Add(%d) error: %v", i, err)
}
}
}
// BenchmarkAdd measures the cost of adding a memory to a store of size N.
// Adds then deletes to keep the store at a stable size.
func BenchmarkAdd(b *testing.B) {
for _, size := range []int{10, 100, 500} {
b.Run(fmt.Sprintf("n=%d", size), func(b *testing.B) {
s := mustBenchStore(b)
seedStore(b, s, size)
b.ResetTimer()
for i := range b.N {
text := fmt.Sprintf("benchmark memory %d about testing performance", i)
chunk, _, err := s.Add(text, "", "fact", nil)
if err != nil {
b.Fatalf("Add() error: %v", err)
}
s.Delete(chunk.ID)
}
})
}
}
// BenchmarkGet measures retrieval by ID (map lookup + access tracking).
func BenchmarkGet(b *testing.B) {
s := mustBenchStore(b)
seedStore(b, s, 100)
ids := make([]string, 0, 100)
for _, item := range s.List("", "") {
ids = append(ids, item.ID)
}
b.ResetTimer()
for i := range b.N {
id := ids[i%len(ids)]
if _, _, err := s.Get(id); err != nil {
b.Fatalf("Get(%s) error: %v", id, err)
}
}
}
// BenchmarkSearch measures content search across a store of size N.
func BenchmarkSearch(b *testing.B) {
for _, size := range []int{10, 100, 500} {
b.Run(fmt.Sprintf("n=%d", size), func(b *testing.B) {
s := mustBenchStore(b)
seedStore(b, s, size)
queries := []string{
"concurrent goroutines channels",
"SQLite database performance",
"similarity scoring search",
"REST API JSON endpoints",
"memory management mutex",
}
b.ResetTimer()
for i := range b.N {
q := queries[i%len(queries)]
if _, err := s.Search(q, "", ""); err != nil {
b.Fatalf("Search() error: %v", err)
}
}
})
}
}
// BenchmarkFindRelevant measures mid-conversation relevance search.
func BenchmarkFindRelevant(b *testing.B) {
for _, size := range []int{10, 100, 500} {
b.Run(fmt.Sprintf("n=%d", size), func(b *testing.B) {
s := mustBenchStore(b)
seedStore(b, s, size)
messages := []string{
"How does the SQLite storage work?",
"Tell me about memory consolidation",
"What are the API endpoints?",
"How do you handle concurrent access?",
"What about the edge similarity graph?",
}
b.ResetTimer()
for i := range b.N {
msg := messages[i%len(messages)]
s.FindRelevant(msg, 5, "", "")
}
})
}
}
// BenchmarkUpdate measures updating a memory in a store of size N.
func BenchmarkUpdate(b *testing.B) {
for _, size := range []int{10, 100, 500} {
b.Run(fmt.Sprintf("n=%d", size), func(b *testing.B) {
s := mustBenchStore(b)
seedStore(b, s, size)
ids := make([]string, 0, size)
for _, item := range s.List("", "") {
ids = append(ids, item.ID)
}
b.ResetTimer()
for i := range b.N {
id := ids[i%len(ids)]
text := fmt.Sprintf("updated memory %d with new content about testing", i)
if _, _, err := s.Update(id, text, "", "", nil); err != nil {
b.Fatalf("Update() error: %v", err)
}
}
})
}
}
// BenchmarkGetContext measures context restoration across categories.
func BenchmarkGetContext(b *testing.B) {
s := mustBenchStore(b)
categories := []string{"#self", "#goal", "#relationship", "#status", "#principle", "#thought"}
for i := range 60 {
cat := categories[i%len(categories)]
text := fmt.Sprintf("%s memory number %d about the project", cat, i)
if _, _, err := s.Add(text, "", "fact", nil); err != nil {
b.Fatalf("seed Add() error: %v", err)
}
}
b.ResetTimer()
for range b.N {
s.GetContext("", "", 0)
}
}
// BenchmarkList measures listing all memories.
func BenchmarkList(b *testing.B) {
s := mustBenchStore(b)
seedStore(b, s, 100)
b.ResetTimer()
for range b.N {
s.List("", "")
}
}
// BenchmarkTokenize measures the tokenization function.
func BenchmarkTokenize(b *testing.B) {
texts := []string{
"Go concurrency patterns with goroutines and channels for parallel processing",
"#principle Build for your actual use case not imagined ones",
"SQLite WAL mode enables concurrent readers with single writer",
}
for range b.N {
for _, t := range texts {
tokenize(t)
}
}
}