-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_gpu_test.go
More file actions
394 lines (327 loc) · 10.6 KB
/
benchmark_gpu_test.go
File metadata and controls
394 lines (327 loc) · 10.6 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//go:build rag
package rag
import (
"context"
"crypto/md5"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// --- Embedding benchmarks (Ollama on ROCm GPU) ---
func BenchmarkEmbedSingle(b *testing.B) {
skipBenchIfOllamaUnavailable(b)
cfg := DefaultOllamaConfig()
client, err := NewOllamaClient(cfg)
require.NoError(b, err)
ctx := context.Background()
// Warm up — first call loads model into GPU memory.
_, err = client.Embed(ctx, "warmup")
require.NoError(b, err)
b.ResetTimer()
for range b.N {
_, err := client.Embed(ctx, "The quick brown fox jumps over the lazy dog.")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkEmbedBatch(b *testing.B) {
skipBenchIfOllamaUnavailable(b)
cfg := DefaultOllamaConfig()
client, err := NewOllamaClient(cfg)
require.NoError(b, err)
ctx := context.Background()
texts := []string{
"Go is a statically typed programming language designed at Google.",
"Rust prioritises memory safety without a garbage collector.",
"Python is widely used for data science and machine learning.",
"TypeScript adds static types to JavaScript for better tooling.",
"Zig is a systems programming language with manual memory management.",
"Elixir runs on the BEAM VM for fault-tolerant distributed systems.",
"Haskell is a purely functional programming language with lazy evaluation.",
"C++ remains dominant in game engines and high-performance computing.",
"Ruby emphasises developer happiness with elegant syntax.",
"Kotlin is the preferred language for Android development.",
}
// Warm up.
_, err = client.Embed(ctx, "warmup")
require.NoError(b, err)
b.ResetTimer()
for range b.N {
_, err := client.EmbedBatch(ctx, texts)
if err != nil {
b.Fatal(err)
}
}
}
// BenchmarkEmbedVaryingLength measures embedding latency across text lengths.
func BenchmarkEmbedVaryingLength(b *testing.B) {
skipBenchIfOllamaUnavailable(b)
cfg := DefaultOllamaConfig()
client, err := NewOllamaClient(cfg)
require.NoError(b, err)
ctx := context.Background()
_, err = client.Embed(ctx, "warmup")
require.NoError(b, err)
for _, size := range []int{50, 200, 500, 1000, 2000} {
text := strings.Repeat("word ", size/5)
b.Run(fmt.Sprintf("chars_%d", size), func(b *testing.B) {
for range b.N {
_, err := client.Embed(ctx, text)
if err != nil {
b.Fatal(err)
}
}
})
}
}
// --- Chunking benchmarks (pure CPU, varying sizes) ---
func BenchmarkChunkMarkdown_GPU(b *testing.B) {
// Generate a realistic markdown document.
var sb strings.Builder
for i := 0; i < 50; i++ {
sb.WriteString(fmt.Sprintf("## Section %d\n\n", i))
sb.WriteString("This is a paragraph of text that represents typical documentation content. ")
sb.WriteString("It contains technical information about software architecture and design patterns. ")
sb.WriteString("Each section discusses different aspects of the system being documented.\n\n")
sb.WriteString("```go\nfunc Example() error {\n\treturn nil\n}\n```\n\n")
}
content := sb.String()
cfg := DefaultChunkConfig()
b.ResetTimer()
for range b.N {
_ = ChunkMarkdown(content, cfg)
}
}
func BenchmarkChunkMarkdown_VaryingSize(b *testing.B) {
base := "This is a paragraph of text. "
for _, paragraphs := range []int{10, 50, 200, 1000} {
var sb strings.Builder
for i := 0; i < paragraphs; i++ {
sb.WriteString(fmt.Sprintf("## Section %d\n\n", i))
sb.WriteString(strings.Repeat(base, 5))
sb.WriteString("\n\n")
}
content := sb.String()
cfg := DefaultChunkConfig()
b.Run(fmt.Sprintf("paragraphs_%d", paragraphs), func(b *testing.B) {
for range b.N {
_ = ChunkMarkdown(content, cfg)
}
})
}
}
// --- Search latency benchmarks (Qdrant) ---
func BenchmarkQdrantSearch(b *testing.B) {
skipBenchIfQdrantUnavailable(b)
skipBenchIfOllamaUnavailable(b)
ctx := context.Background()
// Set up Qdrant with test data.
qdrantClient, err := NewQdrantClient(DefaultQdrantConfig())
require.NoError(b, err)
defer func() { _ = qdrantClient.Close() }()
ollamaClient, err := NewOllamaClient(DefaultOllamaConfig())
require.NoError(b, err)
collection := "bench-search"
dim := ollamaClient.EmbedDimension()
// Clean up from previous runs.
_ = qdrantClient.DeleteCollection(ctx, collection)
err = qdrantClient.CreateCollection(ctx, collection, dim)
require.NoError(b, err)
defer func() { _ = qdrantClient.DeleteCollection(ctx, collection) }()
// Seed with 100 points.
texts := make([]string, 100)
for i := range texts {
texts[i] = fmt.Sprintf("Document %d discusses topic %d about software engineering practices and patterns.", i, i%10)
}
var points []Point
for i, text := range texts {
vec, err := ollamaClient.Embed(ctx, text)
require.NoError(b, err)
points = append(points, Point{
ID: fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("bench-%d", i)))),
Vector: vec,
Payload: map[string]any{
"text": text,
"source": "benchmark",
"category": fmt.Sprintf("topic-%d", i%10),
},
})
}
err = qdrantClient.UpsertPoints(ctx, collection, points)
require.NoError(b, err)
// Generate a query vector.
queryVec, err := ollamaClient.Embed(ctx, "software engineering best practices")
require.NoError(b, err)
b.ResetTimer()
for range b.N {
_, err := qdrantClient.Search(ctx, collection, queryVec, 5, nil)
if err != nil {
b.Fatal(err)
}
}
}
// --- Full pipeline benchmark (ingest + query) ---
func BenchmarkFullPipeline(b *testing.B) {
skipBenchIfQdrantUnavailable(b)
skipBenchIfOllamaUnavailable(b)
ctx := context.Background()
// Create temp dir with markdown files.
dir := b.TempDir()
for i := 0; i < 5; i++ {
content := fmt.Sprintf("# Document %d\n\nThis file covers topic %d.\n\n## Details\n\nDetailed content about software patterns and architecture decisions for component %d.\n", i, i, i)
err := os.WriteFile(filepath.Join(dir, fmt.Sprintf("doc%d.md", i)), []byte(content), 0644)
require.NoError(b, err)
}
qdrantClient, err := NewQdrantClient(DefaultQdrantConfig())
require.NoError(b, err)
defer func() { _ = qdrantClient.Close() }()
ollamaClient, err := NewOllamaClient(DefaultOllamaConfig())
require.NoError(b, err)
collection := "bench-pipeline"
b.ResetTimer()
for range b.N {
// Ingest
cfg := DefaultIngestConfig()
cfg.Directory = dir
cfg.Collection = collection
cfg.Recreate = true
_, err := Ingest(ctx, qdrantClient, ollamaClient, cfg, nil)
if err != nil {
b.Fatal(err)
}
// Query
_, err = Query(ctx, qdrantClient, ollamaClient, "software architecture", QueryConfig{
Collection: collection,
Limit: 3,
Threshold: 0.0,
})
if err != nil {
b.Fatal(err)
}
}
// Clean up.
_ = qdrantClient.DeleteCollection(ctx, collection)
}
// --- Embedding throughput test (not a benchmark — reports human-readable stats) ---
func TestEmbeddingThroughput(t *testing.T) {
skipIfOllamaUnavailable(t)
cfg := DefaultOllamaConfig()
client, err := NewOllamaClient(cfg)
require.NoError(t, err)
ctx := context.Background()
// Warm up.
_, err = client.Embed(ctx, "warmup")
require.NoError(t, err)
// Single embedding latency (10 samples).
var singleTotal time.Duration
const singleN = 10
for i := 0; i < singleN; i++ {
start := time.Now()
_, err := client.Embed(ctx, "Measure single embedding latency on ROCm GPU.")
require.NoError(t, err)
singleTotal += time.Since(start)
}
singleAvg := singleTotal / singleN
// Batch embedding latency (10 texts, 5 samples).
texts := make([]string, 10)
for i := range texts {
texts[i] = fmt.Sprintf("Batch text %d for throughput measurement on AMD GPU with ROCm.", i)
}
var batchTotal time.Duration
const batchN = 5
for i := 0; i < batchN; i++ {
start := time.Now()
_, err := client.EmbedBatch(ctx, texts)
require.NoError(t, err)
batchTotal += time.Since(start)
}
batchAvg := batchTotal / batchN
t.Logf("--- Embedding Throughput (nomic-embed-text, ROCm GPU) ---")
t.Logf("Single embed: %v avg (%d samples)", singleAvg, singleN)
t.Logf("Batch (10): %v avg (%d samples)", batchAvg, batchN)
t.Logf("Per-text in batch: %v", batchAvg/10)
t.Logf("Throughput: %.1f embeds/sec (single), %.1f embeds/sec (batch)",
float64(time.Second)/float64(singleAvg),
float64(time.Second)/float64(batchAvg)*10)
}
// TestSearchLatency reports Qdrant search timing.
func TestSearchLatency(t *testing.T) {
skipIfQdrantUnavailable(t)
skipIfOllamaUnavailable(t)
ctx := context.Background()
qdrantClient, err := NewQdrantClient(DefaultQdrantConfig())
require.NoError(t, err)
defer func() { _ = qdrantClient.Close() }()
ollamaClient, err := NewOllamaClient(DefaultOllamaConfig())
require.NoError(t, err)
collection := "latency-test"
dim := ollamaClient.EmbedDimension()
_ = qdrantClient.DeleteCollection(ctx, collection)
err = qdrantClient.CreateCollection(ctx, collection, dim)
require.NoError(t, err)
defer func() { _ = qdrantClient.DeleteCollection(ctx, collection) }()
// Seed 200 points.
var points []Point
for i := 0; i < 200; i++ {
vec, err := ollamaClient.Embed(ctx, fmt.Sprintf("Document %d covers topic %d.", i, i%20))
require.NoError(t, err)
points = append(points, Point{
ID: fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("lat-%d", i)))),
Vector: vec,
Payload: map[string]any{
"text": fmt.Sprintf("doc %d", i),
"source": "latency-test",
},
})
}
err = qdrantClient.UpsertPoints(ctx, collection, points)
require.NoError(t, err)
queryVec, err := ollamaClient.Embed(ctx, "software engineering patterns")
require.NoError(t, err)
// Measure search latency (50 queries).
var searchTotal time.Duration
const searchN = 50
for i := 0; i < searchN; i++ {
start := time.Now()
_, err := qdrantClient.Search(ctx, collection, queryVec, 5, nil)
require.NoError(t, err)
searchTotal += time.Since(start)
}
searchAvg := searchTotal / searchN
t.Logf("--- Search Latency (200 points, top-5) ---")
t.Logf("Avg: %v (%d queries)", searchAvg, searchN)
t.Logf("QPS: %.0f queries/sec", float64(time.Second)/float64(searchAvg))
}
// --- Helpers ---
func skipBenchIfOllamaUnavailable(b *testing.B) {
b.Helper()
cfg := DefaultOllamaConfig()
client, err := NewOllamaClient(cfg)
if err != nil {
b.Skip("Ollama not available")
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := client.VerifyModel(ctx); err != nil {
b.Skip("Ollama model not available")
}
}
func skipBenchIfQdrantUnavailable(b *testing.B) {
b.Helper()
cfg := DefaultQdrantConfig()
client, err := NewQdrantClient(cfg)
if err != nil {
b.Skip("Qdrant not available")
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := client.HealthCheck(ctx); err != nil {
b.Skip("Qdrant health check failed")
}
_ = client.Close()
}