on main help sign in
ci: add a bench test for grep
0dd52abRaphael Amorim
internal/llm/tools/grep_test.gonew file+77
@@ -0,0 +1,77 @@
1+package tools
2+
3+import (
4+ "regexp"
5+ "testing"
6+)
7+
8+func TestRegexCache(t *testing.T) {
9+ cache := newRegexCache()
10+
11+ // Test basic caching
12+ pattern := "test.*pattern"
13+ regex1, err := cache.get(pattern)
14+ if err != nil {
15+ t.Fatalf("Failed to compile regex: %v", err)
16+ }
17+
18+ regex2, err := cache.get(pattern)
19+ if err != nil {
20+ t.Fatalf("Failed to get cached regex: %v", err)
21+ }
22+
23+ // Should be the same instance (cached)
24+ if regex1 != regex2 {
25+ t.Error("Expected cached regex to be the same instance")
26+ }
27+
28+ // Test that it actually works
29+ if !regex1.MatchString("test123pattern") {
30+ t.Error("Regex should match test string")
31+ }
32+}
33+
34+func TestGlobToRegexCaching(t *testing.T) {
35+ // Test that globToRegex uses pre-compiled regex
36+ pattern1 := globToRegex("*.{js,ts}")
37+
38+ // Should not panic and should work correctly
39+ regex1, err := regexp.Compile(pattern1)
40+ if err != nil {
41+ t.Fatalf("Failed to compile glob regex: %v", err)
42+ }
43+
44+ if !regex1.MatchString("test.js") {
45+ t.Error("Glob regex should match .js files")
46+ }
47+ if !regex1.MatchString("test.ts") {
48+ t.Error("Glob regex should match .ts files")
49+ }
50+ if regex1.MatchString("test.go") {
51+ t.Error("Glob regex should not match .go files")
52+ }
53+}
54+
55+// Benchmark to show performance improvement
56+func BenchmarkRegexCacheVsCompile(b *testing.B) {
57+ cache := newRegexCache()
58+ pattern := "test.*pattern.*[0-9]+"
59+
60+ b.Run("WithCache", func(b *testing.B) {
61+ for b.Loop() {
62+ _, err := cache.get(pattern)
63+ if err != nil {
64+ b.Fatal(err)
65+ }
66+ }
67+ })
68+
69+ b.Run("WithoutCache", func(b *testing.B) {
70+ for b.Loop() {
71+ _, err := regexp.Compile(pattern)
72+ if err != nil {
73+ b.Fatal(err)
74+ }
75+ }
76+ })
77+}