-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
170 lines (141 loc) · 4 KB
/
benchmark_test.go
File metadata and controls
170 lines (141 loc) · 4 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
package rag
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
// generateMarkdownDoc creates a ~10KB markdown document for benchmarking.
func generateMarkdownDoc() string {
var sb strings.Builder
sb.WriteString("# Benchmark Document\n\n")
sb.WriteString("This document is generated for benchmarking the chunking pipeline.\n\n")
for i := range 20 {
sb.WriteString(fmt.Sprintf("## Section %d\n\n", i+1))
for j := range 5 {
sb.WriteString(fmt.Sprintf(
"Paragraph %d in section %d contains representative text for testing. "+
"It includes multiple sentences to exercise the sentence-aware splitter. "+
"The content is deliberately verbose to reach a realistic document size. "+
"Each paragraph is unique to avoid deduplication effects.\n\n",
j+1, i+1))
}
}
return sb.String()
}
// generateQueryResults creates n QueryResult entries for benchmarking.
func generateQueryResults(n int) []QueryResult {
results := make([]QueryResult, n)
for i := range results {
results[i] = QueryResult{
Text: fmt.Sprintf("This is result number %d with some representative text content for testing purposes.", i+1),
Source: fmt.Sprintf("docs/section-%d/file-%d.md", i/5, i),
Section: fmt.Sprintf("Section %d", i+1),
Category: "documentation",
ChunkIndex: i,
Score: 1.0 - float32(i)*0.01,
}
}
return results
}
// --- BenchmarkChunk ---
func BenchmarkChunk(b *testing.B) {
doc := generateMarkdownDoc()
cfg := DefaultChunkConfig()
b.ResetTimer()
for b.Loop() {
ChunkMarkdown(doc, cfg)
}
}
// --- BenchmarkChunkWithOverlap ---
func BenchmarkChunkWithOverlap(b *testing.B) {
doc := generateMarkdownDoc()
cfg := ChunkConfig{Size: 500, Overlap: 100}
b.ResetTimer()
for b.Loop() {
ChunkMarkdown(doc, cfg)
}
}
// --- BenchmarkQuery_Mock ---
func BenchmarkQuery_Mock(b *testing.B) {
store := newMockVectorStore()
store.collections["bench-col"] = 768
// Pre-populate with 50 points
for i := range 50 {
store.points["bench-col"] = append(store.points["bench-col"], Point{
ID: fmt.Sprintf("p%d", i),
Vector: make([]float32, 768),
Payload: map[string]any{
"text": fmt.Sprintf("Benchmark document %d with relevant content.", i),
"source": fmt.Sprintf("doc%d.md", i),
"section": "Section",
"category": "docs",
"chunk_index": i,
},
})
}
embedder := newMockEmbedder(768)
cfg := DefaultQueryConfig()
cfg.Collection = "bench-col"
cfg.Threshold = 0.0
ctx := context.Background()
b.ResetTimer()
for b.Loop() {
_, _ = Query(ctx, store, embedder, "benchmark query text", cfg)
}
}
// --- BenchmarkIngest_Mock ---
func BenchmarkIngest_Mock(b *testing.B) {
dir := b.TempDir()
// Create 10 markdown files
for i := range 10 {
content := fmt.Sprintf("## File %d\n\nThis is file number %d with some test content for benchmarking.\n", i, i)
path := filepath.Join(dir, fmt.Sprintf("doc%d.md", i))
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
b.Fatal(err)
}
}
ctx := context.Background()
b.ResetTimer()
for b.Loop() {
store := newMockVectorStore()
embedder := newMockEmbedder(768)
cfg := DefaultIngestConfig()
cfg.Directory = dir
cfg.Collection = "bench-ingest"
_, _ = Ingest(ctx, store, embedder, cfg, nil)
}
}
// --- BenchmarkFormatResults ---
func BenchmarkFormatResultsText(b *testing.B) {
results := generateQueryResults(20)
b.ResetTimer()
for b.Loop() {
FormatResultsText(results)
}
}
func BenchmarkFormatResultsContext(b *testing.B) {
results := generateQueryResults(20)
b.ResetTimer()
for b.Loop() {
FormatResultsContext(results)
}
}
func BenchmarkFormatResultsJSON(b *testing.B) {
results := generateQueryResults(20)
b.ResetTimer()
for b.Loop() {
FormatResultsJSON(results)
}
}
// --- BenchmarkKeywordFilter ---
func BenchmarkKeywordFilter(b *testing.B) {
results := generateQueryResults(100)
keywords := []string{"result", "content", "testing", "documentation", "section"}
b.ResetTimer()
for b.Loop() {
KeywordFilter(results, keywords)
}
}