From 0dd52aba50ef88362076cd52469cf1c7f1a83b6f Mon Sep 17 00:00:00 2001 From: Raphael Amorim Date: Tue, 10 Jun 2025 12:46:51 +0200 Subject: [PATCH] ci: add a bench test for grep --- internal/llm/tools/grep_test.go | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 internal/llm/tools/grep_test.go diff --git a/internal/llm/tools/grep_test.go b/internal/llm/tools/grep_test.go new file mode 100644 index 0000000000000000000000000000000000000000..de4e6afb225075df34e14d54fb1e5db76955625b --- /dev/null +++ b/internal/llm/tools/grep_test.go @@ -0,0 +1,77 @@ +package tools + +import ( + "regexp" + "testing" +) + +func TestRegexCache(t *testing.T) { + cache := newRegexCache() + + // Test basic caching + pattern := "test.*pattern" + regex1, err := cache.get(pattern) + if err != nil { + t.Fatalf("Failed to compile regex: %v", err) + } + + regex2, err := cache.get(pattern) + if err != nil { + t.Fatalf("Failed to get cached regex: %v", err) + } + + // Should be the same instance (cached) + if regex1 != regex2 { + t.Error("Expected cached regex to be the same instance") + } + + // Test that it actually works + if !regex1.MatchString("test123pattern") { + t.Error("Regex should match test string") + } +} + +func TestGlobToRegexCaching(t *testing.T) { + // Test that globToRegex uses pre-compiled regex + pattern1 := globToRegex("*.{js,ts}") + + // Should not panic and should work correctly + regex1, err := regexp.Compile(pattern1) + if err != nil { + t.Fatalf("Failed to compile glob regex: %v", err) + } + + if !regex1.MatchString("test.js") { + t.Error("Glob regex should match .js files") + } + if !regex1.MatchString("test.ts") { + t.Error("Glob regex should match .ts files") + } + if regex1.MatchString("test.go") { + t.Error("Glob regex should not match .go files") + } +} + +// Benchmark to show performance improvement +func BenchmarkRegexCacheVsCompile(b *testing.B) { + cache := newRegexCache() + pattern := "test.*pattern.*[0-9]+" + + b.Run("WithCache", func(b *testing.B) { + for b.Loop() { + _, err := cache.get(pattern) + if err != nil { + b.Fatal(err) + } + } + }) + + b.Run("WithoutCache", func(b *testing.B) { + for b.Loop() { + _, err := regexp.Compile(pattern) + if err != nil { + b.Fatal(err) + } + } + }) +} \ No newline at end of file