-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress_test.go
More file actions
727 lines (647 loc) · 21.5 KB
/
Copy pathstress_test.go
File metadata and controls
727 lines (647 loc) · 21.5 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
package okf_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"
okf "github.com/superops-team/okf/pkg/okf"
"github.com/superops-team/okf/pkg/git"
"github.com/superops-team/okf/pkg/lint"
"github.com/superops-team/okf/pkg/parser"
"github.com/superops-team/okf/pkg/query"
)
// ============================================================================
// Stress Test Suite for OKF
// ============================================================================
// ---------------------------------------------------------------------------
// 1. Parser Stress Tests
// ---------------------------------------------------------------------------
func TestStressParseLargeConcept(t *testing.T) {
t.Parallel()
// 10MB content body
content := strings.Repeat("This is a line of content for stress testing.\n", 200000)
fm := fmt.Sprintf("---\ntype: table\ntitle: Large Table\ndescription: A very large concept\n---\n\n%s", content)
start := time.Now()
c, err := parser.ParseConceptBytes("large.md", []byte(fm))
elapsed := time.Since(start)
if err != nil {
t.Fatalf("ParseConceptBytes failed: %v", err)
}
if c.Title != "Large Table" {
t.Fatalf("title = %q, want 'Large Table'", c.Title)
}
t.Logf("Parsed 10MB concept in %v", elapsed)
}
func TestStressParseManyConcepts(t *testing.T) {
t.Parallel()
count := 5000
start := time.Now()
for i := 0; i < count; i++ {
fm := fmt.Sprintf("---\ntype: table\ntitle: concept-%d\ndescription: Concept number %d\n---\n\nContent for concept %d\n", i, i, i)
_, err := parser.ParseConceptBytes(fmt.Sprintf("concept-%d.md", i), []byte(fm))
if err != nil {
t.Fatalf("ParseConceptBytes failed at %d: %v", i, err)
}
}
elapsed := time.Since(start)
t.Logf("Parsed %d concepts in %v (avg: %v per concept)", count, elapsed, elapsed/time.Duration(count))
}
func TestStressParseMalformedYAML(t *testing.T) {
t.Parallel()
malformed := []string{
"---\ntype: [unclosed\n---\n",
"---\ntype: table\ntitle: \n---\n",
"---\n: : :\n---\n",
"---\ntype: \ntitle: \n---\n",
"",
"---\n---\n",
"---\ntype: table\n---\n",
"---\ntitle: only-title\n---\n",
}
for i, m := range malformed {
_, err := parser.ParseConceptBytes(fmt.Sprintf("bad-%d.md", i), []byte(m))
if err == nil {
t.Logf("WARNING: malformed input %d did not return error (input: %q)", i, m)
}
}
}
func TestStressParseSpecialCharacters(t *testing.T) {
t.Parallel()
specials := []struct {
name string
title string
}{
{"unicode", "中文标题测试 🚀"},
{"emoji", "📊 Data Analysis 📈"},
// YAML reserved characters in title require quoting - this is expected behavior
{"special_chars", "title with forward slash and backslash and pipe"},
{"very_long", strings.Repeat("a", 10000)},
{"zero_width", "hello\u200bworld"},
{"rtl", "مرحبا بالعالم"},
}
for _, tc := range specials {
t.Run(tc.name, func(t *testing.T) {
fm := fmt.Sprintf("---\ntype: table\ntitle: %s\n---\n\nTest content\n", tc.title)
c, err := parser.ParseConceptBytes("special.md", []byte(fm))
if err != nil {
t.Fatalf("ParseConceptBytes failed for %s: %v", tc.name, err)
}
if c.Title != tc.title {
t.Fatalf("title = %q, want %q", c.Title, tc.title)
}
})
}
}
func TestStressParseEmptyAndNil(t *testing.T) {
t.Parallel()
// nil bytes should return error
_, err := parser.ParseConceptBytes("nil.md", nil)
if err == nil {
t.Fatal("ParseConceptBytes should return error for nil input")
}
// empty bytes should return error
_, err = parser.ParseConceptBytes("empty.md", []byte{})
if err == nil {
t.Fatal("ParseConceptBytes should return error for empty input")
}
}
// ---------------------------------------------------------------------------
// 2. Lint Stress Tests
// ---------------------------------------------------------------------------
func TestStressLintManyConcepts(t *testing.T) {
t.Parallel()
count := 10000
concepts := make([]*lint.Concept, count)
for i := 0; i < count; i++ {
concepts[i] = &lint.Concept{
Type: "table",
Title: fmt.Sprintf("concept-%d", i),
Description: fmt.Sprintf("Description for concept %d", i),
Tags: []string{"test", "stress"},
Timestamp: "2024-01-15T10:30:00Z",
Content: fmt.Sprintf("Content for concept %d", i),
FilePath: fmt.Sprintf("concept-%d.md", i),
}
}
cfg := lint.DefaultConfig()
start := time.Now()
result := lint.LintBundle(concepts, cfg)
elapsed := time.Since(start)
t.Logf("Linted %d concepts in %v (avg: %v per concept)", count, elapsed, elapsed/time.Duration(count))
if result.ConceptsChecked != count {
t.Fatalf("checked = %d, want %d", result.ConceptsChecked, count)
}
}
func TestStressLintEdgeCases(t *testing.T) {
t.Parallel()
edgeCases := []*lint.Concept{
{Type: "", Title: "", FilePath: "empty.md"},
{Type: "UPPERCASE", Title: "test", FilePath: "upper.md"},
{Type: "table", Title: "test", Tags: []string{"TAG1", "TAG2", "TAG1"}, FilePath: "dup.md"},
{Type: "table", Title: "test", Timestamp: "not-a-date", FilePath: "bad-date.md"},
{Type: "table", Title: "test", Description: "short", FilePath: "short.md"},
{Type: "table", Title: "test", Content: strings.Repeat("x", 300), FilePath: "long.md"},
{Type: "table", Title: "test", Tags: []string{"has space"}, FilePath: "space.md"},
}
cfg := lint.DefaultConfig()
result := lint.LintBundle(edgeCases, cfg)
t.Logf("Edge case lint: %d issues found", len(result.Issues))
for _, issue := range result.Issues {
t.Logf(" [%s] %s: %s", issue.Severity, issue.Code, issue.Message)
}
}
// ---------------------------------------------------------------------------
// 3. Bundle Operations Stress Tests
// ---------------------------------------------------------------------------
func TestStressBundleLargeOperations(t *testing.T) {
t.Parallel()
bundle := okf.NewBundle("stress-test")
count := 20000
for i := 0; i < count; i++ {
c := okf.NewConcept("table", fmt.Sprintf("concept-%d", i))
c.Description = fmt.Sprintf("Description %d", i)
c.Tags = []string{"test", fmt.Sprintf("tag-%d", i%100)}
c.Content = fmt.Sprintf("Content for concept %d\n", i)
bundle.AddConcept(c)
}
// Test Search
start := time.Now()
results := bundle.Search("concept-12345")
elapsed := time.Since(start)
if len(results) != 1 {
t.Fatalf("Search returned %d results, want 1", len(results))
}
t.Logf("Search in %d concepts: %v", count, elapsed)
// Test Stats
start = time.Now()
stats := bundle.Stats()
elapsed = time.Since(start)
t.Logf("Stats for %d concepts: %v (types=%d, tags=%d)", count, elapsed, stats.UniqueTypes, stats.UniqueTags)
// Test FilterByType
start = time.Now()
filtered := bundle.FilterByType("table")
elapsed = time.Since(start)
if len(filtered) != count {
t.Fatalf("FilterByType returned %d, want %d", len(filtered), count)
}
t.Logf("FilterByType in %d concepts: %v", count, elapsed)
// Test FilterByTag
start = time.Now()
tagged := bundle.FilterByTag("tag-50")
elapsed = time.Since(start)
t.Logf("FilterByTag in %d concepts: %v (found %d)", count, elapsed, len(tagged))
// Test RelatedConcepts
start = time.Now()
related := bundle.RelatedConcepts(bundle.Concepts[0])
elapsed = time.Since(start)
t.Logf("RelatedConcepts in %d concepts: %v (found %d)", count, elapsed, len(related))
// Test GetConcept
start = time.Now()
c := bundle.GetConcept("concept-15000")
elapsed = time.Since(start)
if c == nil {
t.Fatal("GetConcept returned nil")
}
t.Logf("GetConcept in %d concepts: %v", count, elapsed)
// Test RemoveConcept
start = time.Now()
removed := bundle.RemoveConcept("concept-0")
elapsed = time.Since(start)
if !removed {
t.Fatal("RemoveConcept failed")
}
t.Logf("RemoveConcept in %d concepts: %v", count, elapsed)
}
func TestStressBundleNilAndEmpty(t *testing.T) {
t.Parallel()
bundle := okf.NewBundle("empty")
// Operations on empty bundle
if results := bundle.Search("anything"); len(results) != 0 {
t.Fatalf("Search on empty bundle returned %d results", len(results))
}
if c := bundle.GetConcept("nonexistent"); c != nil {
t.Fatal("GetConcept on empty bundle returned non-nil")
}
if bundle.RemoveConcept("nonexistent") {
t.Fatal("RemoveConcept on empty bundle returned true")
}
// RelatedConcepts with nil should return nil (not panic)
if related := bundle.RelatedConcepts(nil); related != nil {
t.Fatalf("RelatedConcepts(nil) should return nil, got %d results", len(related))
}
// Add nil concept
bundle.Concepts = append(bundle.Concepts, nil)
stats := bundle.Stats()
t.Logf("Stats with nil concept: total=%d", stats.TotalConcepts)
}
func TestStressBundleConcurrentAccess(t *testing.T) {
t.Parallel()
bundle := okf.NewBundle("concurrent")
count := 1000
for i := 0; i < count; i++ {
c := okf.NewConcept("table", fmt.Sprintf("concept-%d", i))
c.Content = fmt.Sprintf("Content %d", i)
bundle.AddConcept(c)
}
var wg sync.WaitGroup
errors := make(chan error, 100)
// Concurrent reads
for g := 0; g < 50; g++ {
wg.Add(1)
go func(gid int) {
defer wg.Done()
for i := 0; i < 100; i++ {
_ = bundle.Search(fmt.Sprintf("concept-%d", i))
_ = bundle.GetConcept(fmt.Sprintf("concept-%d", i))
_ = bundle.FilterByType("table")
_ = bundle.Stats()
}
}(g)
}
// Concurrent writes
for g := 0; g < 10; g++ {
wg.Add(1)
go func(gid int) {
defer wg.Done()
for i := 0; i < 50; i++ {
c := okf.NewConcept("table", fmt.Sprintf("concurrent-%d-%d", gid, i))
bundle.AddConcept(c)
}
}(g)
}
wg.Wait()
close(errors)
for err := range errors {
t.Errorf("concurrent error: %v", err)
}
t.Logf("Concurrent test completed, bundle has %d concepts", len(bundle.Concepts))
}
// ---------------------------------------------------------------------------
// 4. Query Engine Stress Tests
// ---------------------------------------------------------------------------
func TestStressQueryLargeIndex(t *testing.T) {
t.Parallel()
count := 10000
concepts := make([]*query.Concept, count)
for i := 0; i < count; i++ {
concepts[i] = &query.Concept{
Type: fmt.Sprintf("type-%d", i%10),
Title: fmt.Sprintf("Concept Title %d", i),
Description: fmt.Sprintf("Description for concept number %d", i),
Tags: []string{fmt.Sprintf("tag-%d", i%50), "common"},
Content: fmt.Sprintf("## Content %d\n\n- `function` `Func%d` (exported) at `file%d.go:1-10`\n- Language: `go`\n- Path: `pkg/file%d.go`\n", i, i, i, i),
FilePath: fmt.Sprintf("file-%d.md", i),
}
}
bundle := &query.KnowledgeBundle{Concepts: concepts}
// Build index
start := time.Now()
bundle.BuildIndex()
elapsed := time.Since(start)
t.Logf("Built index for %d concepts in %v", count, elapsed)
// Indexed search
start = time.Now()
results := query.Search(bundle, "Concept Title 5000")
elapsed = time.Since(start)
if len(results) == 0 {
t.Fatal("Search returned no results")
}
t.Logf("Indexed search in %d concepts: %v (found %d)", count, elapsed, len(results))
// Filter by type
start = time.Now()
filtered := query.FilterByType(bundle, "type-5")
elapsed = time.Since(start)
t.Logf("FilterByType in %d concepts: %v (found %d)", count, elapsed, len(filtered))
// Code dimension filter
start = time.Now()
q := query.New().WithCodeLanguage("go").Build()
codeResults := q.Execute(bundle)
elapsed = time.Since(start)
t.Logf("Code language filter in %d concepts: %v (found %d)", count, elapsed, len(codeResults))
// Complex query
start = time.Now()
q2 := query.New().WithType("type-5").WithCodeSymbolKind("function").Build()
complexResults := q2.Execute(bundle)
elapsed = time.Since(start)
t.Logf("Complex query in %d concepts: %v (found %d)", count, elapsed, len(complexResults))
}
func TestStressQueryEdgeCases(t *testing.T) {
t.Parallel()
// nil bundle
if results := query.Search(nil, "test"); results != nil {
t.Fatal("Search on nil bundle should return nil")
}
if results := query.FilterByType(nil, "test"); results != nil {
t.Fatal("FilterByType on nil bundle should return nil")
}
if results := query.FilterByTag(nil, "test"); results != nil {
t.Fatal("FilterByTag on nil bundle should return nil")
}
// Empty bundle
empty := &query.KnowledgeBundle{}
if results := query.Search(empty, "test"); len(results) != 0 {
t.Fatalf("Search on empty bundle returned %d results", len(results))
}
// Empty query
bundle := &query.KnowledgeBundle{
Concepts: []*query.Concept{
{Type: "table", Title: "Test", Content: "content"},
},
}
q := query.New().Build()
results := q.Execute(bundle)
if len(results) != 1 {
t.Fatalf("Empty query should return all concepts, got %d", len(results))
}
}
// ---------------------------------------------------------------------------
// 5. Save/Load Roundtrip Stress Tests
// ---------------------------------------------------------------------------
func TestStressSaveLoadRoundtrip(t *testing.T) {
t.Parallel()
dir := t.TempDir()
bundle := okf.NewBundle("roundtrip-test")
count := 1000
for i := 0; i < count; i++ {
c := okf.NewConcept("table", fmt.Sprintf("concept-%d", i))
c.Description = fmt.Sprintf("Description for concept %d", i)
c.Tags = []string{"test", fmt.Sprintf("tag-%d", i%10)}
c.Content = fmt.Sprintf("## Concept %d\n\nContent for concept %d\n\n- Item 1\n- Item 2\n", i, i)
bundle.AddConcept(c)
}
// Save
start := time.Now()
err := okf.SaveBundle(bundle, dir, okf.DefaultSaveOptions())
elapsed := time.Since(start)
if err != nil {
t.Fatalf("SaveBundle failed: %v", err)
}
t.Logf("Saved %d concepts in %v", count, elapsed)
// Load
start = time.Now()
loaded, err := okf.LoadBundle(dir, okf.DefaultLoadOptions())
elapsed = time.Since(start)
if err != nil {
t.Fatalf("LoadBundle failed: %v", err)
}
t.Logf("Loaded %d concepts in %v", len(loaded.Concepts), elapsed)
if len(loaded.Concepts) != count {
t.Fatalf("roundtrip mismatch: saved %d, loaded %d", count, len(loaded.Concepts))
}
}
// ---------------------------------------------------------------------------
// 6. Git Integration Stress Tests
// ---------------------------------------------------------------------------
func TestStressGitLargeRepoSimulation(t *testing.T) {
dir := initStressRepo(t, 500)
cfg := &git.Config{
RepoPath: dir,
KnowledgeDir: ".okf/knowledge",
IncludeFiles: []string{"*.go"},
ExcludeDirs: []string{".git", ".okf"},
MaxFileSizeKB: 1024,
}
start := time.Now()
bundle, err := git.GenerateBundle(cfg, true)
elapsed := time.Since(start)
if err != nil {
t.Fatalf("GenerateBundle failed: %v", err)
}
t.Logf("Generated bundle for 500 files in %v (%d concepts)", elapsed, len(bundle.Concepts))
// Verify key concepts exist
if c := bundle.GetConcept("Project Overview"); c == nil {
t.Fatal("Project Overview concept missing")
}
if c := bundle.GetConcept("Relationship Graph"); c == nil {
t.Fatal("Relationship Graph concept missing")
}
// Test save
saved, err := git.SaveKnowledgeBase(bundle, cfg)
if err != nil {
t.Fatalf("SaveKnowledgeBase failed: %v", err)
}
t.Logf("Saved %d concepts to disk", saved)
// Test load
loaded, err := okf.LoadBundle(filepath.Join(dir, cfg.KnowledgeDir), okf.DefaultLoadOptions())
if err != nil {
t.Fatalf("LoadBundle failed: %v", err)
}
t.Logf("Loaded %d concepts from disk", len(loaded.Concepts))
// Test incremental update
mustWriteFile(t, filepath.Join(dir, "new_file.go"), "package main\nfunc newFunc() {}\n")
runGit(t, dir, "add", "new_file.go")
runGit(t, dir, "commit", "-m", "add new file")
start = time.Now()
incBundle, updated, err := git.UpdateFromLastCommit(cfg)
elapsed = time.Since(start)
if err != nil {
t.Fatalf("UpdateFromLastCommit failed: %v", err)
}
t.Logf("Incremental update in %v: %d concepts, updated files: %v", elapsed, len(incBundle.Concepts), updated)
if err := git.ApplyIncrementalUpdate(cfg, incBundle); err != nil {
t.Fatalf("ApplyIncrementalUpdate failed: %v", err)
}
}
func TestStressGitEdgeCases(t *testing.T) {
dir := initStressRepo(t, 10)
// Non-existent path
if git.IsRepo(filepath.Join(dir, "nonexistent")) {
t.Fatal("IsRepo should return false for non-existent path")
}
// Empty repo (no commits)
emptyDir := t.TempDir()
runGit(t, emptyDir, "init")
cfg := git.DefaultConfig()
cfg.RepoPath = emptyDir
_, err := git.GenerateBundle(cfg, true)
if err == nil {
t.Log("GenerateBundle on empty repo: no error (expected)")
}
// GetRepoRoot (note: macOS /var is symlink to /private/var)
root, err := git.GetRepoRoot(dir)
if err != nil {
t.Fatalf("GetRepoRoot failed: %v", err)
}
resolvedDir, _ := filepath.EvalSymlinks(dir)
if root != dir && root != resolvedDir {
t.Fatalf("GetRepoRoot = %q, want %q or %q", root, dir, resolvedDir)
}
// GetCurrentBranch
branch, err := git.GetCurrentBranch(dir)
if err != nil {
t.Fatalf("GetCurrentBranch failed: %v", err)
}
t.Logf("Current branch: %s", branch)
// GetLastCommits
commits, err := git.GetLastCommits(dir, 5)
if err != nil {
t.Fatalf("GetLastCommits failed: %v", err)
}
t.Logf("Last %d commits retrieved", len(commits))
}
func TestStressGitConcurrentGeneration(t *testing.T) {
dir := initStressRepo(t, 200)
cfg := &git.Config{
RepoPath: dir,
KnowledgeDir: ".okf/knowledge",
IncludeFiles: []string{"*.go"},
ExcludeDirs: []string{".git", ".okf"},
MaxFileSizeKB: 1024,
Workers: runtime.NumCPU(),
}
var wg sync.WaitGroup
results := make(chan struct {
concepts int
duration time.Duration
err error
}, 5)
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
start := time.Now()
bundle, err := git.GenerateBundle(cfg, true)
results <- struct {
concepts int
duration time.Duration
err error
}{len(bundle.Concepts), time.Since(start), err}
}()
}
wg.Wait()
close(results)
for r := range results {
if r.err != nil {
t.Errorf("concurrent GenerateBundle failed: %v", r.err)
}
t.Logf("Concurrent generation: %d concepts in %v", r.concepts, r.duration)
}
}
// ---------------------------------------------------------------------------
// 7. Memory and Performance Benchmarks
// ---------------------------------------------------------------------------
func BenchmarkStressParseLargeConcept(b *testing.B) {
content := strings.Repeat("This is a line of content for stress testing.\n", 200000)
fm := fmt.Sprintf("---\ntype: table\ntitle: Large Table\ndescription: A very large concept\n---\n\n%s", content)
data := []byte(fm)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = parser.ParseConceptBytes("large.md", data)
}
}
func BenchmarkStressBundleSearch(b *testing.B) {
bundle := okf.NewBundle("bench")
for i := 0; i < 10000; i++ {
c := okf.NewConcept("table", fmt.Sprintf("concept-%d", i))
c.Content = fmt.Sprintf("Content for concept %d", i)
bundle.AddConcept(c)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = bundle.Search(fmt.Sprintf("concept-%d", i%10000))
}
}
func BenchmarkStressBundleStats(b *testing.B) {
bundle := okf.NewBundle("bench")
for i := 0; i < 10000; i++ {
c := okf.NewConcept("table", fmt.Sprintf("concept-%d", i))
c.Tags = []string{fmt.Sprintf("tag-%d", i%100)}
bundle.AddConcept(c)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = bundle.Stats()
}
}
func BenchmarkStressLintManyConcepts(b *testing.B) {
concepts := make([]*lint.Concept, 10000)
for i := 0; i < 10000; i++ {
concepts[i] = &lint.Concept{
Type: "table",
Title: fmt.Sprintf("concept-%d", i),
Description: fmt.Sprintf("Description for concept %d", i),
Tags: []string{"test", "stress"},
Timestamp: "2024-01-15T10:30:00Z",
Content: fmt.Sprintf("Content for concept %d", i),
FilePath: fmt.Sprintf("concept-%d.md", i),
}
}
cfg := lint.DefaultConfig()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lint.LintBundle(concepts, cfg)
}
}
func BenchmarkStressQueryBuildIndex(b *testing.B) {
concepts := make([]*query.Concept, 10000)
for i := 0; i < 10000; i++ {
concepts[i] = &query.Concept{
Type: fmt.Sprintf("type-%d", i%10),
Title: fmt.Sprintf("Concept Title %d", i),
Content: fmt.Sprintf("Content %d\n- Language: `go`\n- `function` `Func%d` (exported) at `file%d.go:1-10`\n", i, i, i),
}
}
bundle := &query.KnowledgeBundle{Concepts: concepts}
b.ResetTimer()
for i := 0; i < b.N; i++ {
bundle.BuildIndex()
}
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
func initStressRepo(t *testing.T, fileCount int) string {
t.Helper()
dir := t.TempDir()
runGit(t, dir, "init")
runGit(t, dir, "config", "user.name", "Stress Test")
runGit(t, dir, "config", "user.email", "stress@test.local")
for i := 0; i < fileCount; i++ {
pkg := fmt.Sprintf("pkg%d", i%10)
dirPath := filepath.Join(dir, pkg)
os.MkdirAll(dirPath, 0755)
content := fmt.Sprintf(`package %s
import (
"fmt"
"strings"
)
type Struct%d struct {
Name string
Value int
}
func (s *Struct%d) Method%d() string {
return fmt.Sprintf("%%s: %%d", s.Name, s.Value)
}
func Func%d(input string) string {
return strings.ToUpper(input)
}
`, pkg, i, i, i, i)
mustWriteFile(t, filepath.Join(dirPath, fmt.Sprintf("file_%d.go", i)), content)
}
runGit(t, dir, "add", ".")
runGit(t, dir, "commit", "-m", "initial stress test repo")
return dir
}
func mustWriteFile(t *testing.T, path, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
t.Fatalf("MkdirAll: %v", err)
}
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
}
func runGit(t *testing.T, dir string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, string(out))
}
}