-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
81 lines (72 loc) · 1.83 KB
/
Copy pathbenchmark_test.go
File metadata and controls
81 lines (72 loc) · 1.83 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
package main
import (
"fastgrep/internal/orchestrator"
"fmt"
"os"
"path/filepath"
"testing"
)
func createLargeTestFile(t testing.TB, sizeMB int) string {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "large_test_file.txt")
f, err := os.Create(filePath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
line := "This is a test line that will be repeated many times to reach the desired file size. SearchPattern is here.\n"
iterations := (sizeMB * 1024 * 1024) / len(line)
for range iterations {
f.WriteString(line)
}
return filePath
}
func BenchmarkEndToEnd(b *testing.B) {
// Create a 50-MB file
filePath := createLargeTestFile(b, 50)
searchString := "SearchPattern"
// Redirect stdout to avoid cluttering benchmark results
oldStdout := os.Stdout
devNull, _ := os.Open(os.DevNull)
os.Stdout = devNull
defer func() {
os.Stdout = oldStdout
devNull.Close()
}()
for b.Loop() {
err := orchestrator.Execute(searchString, []string{filePath}, false, false)
if err != nil {
b.Fatalf("Execute failed: %v", err)
}
}
}
func BenchmarkEndToEndMultipleFiles(b *testing.B) {
// Create 5 files of 10MB each
var filePaths []string
tmpDir := b.TempDir()
for i := range 5 {
filePath := filepath.Join(tmpDir, fmt.Sprintf("file_%d.txt", i))
f, _ := os.Create(filePath)
line := "This is a test line for multi-file search. SearchPattern is here.\n"
iterations := (10 * 1024 * 1024) / len(line)
for range iterations {
f.WriteString(line)
}
f.Close()
filePaths = append(filePaths, filePath)
}
searchString := "SearchPattern"
oldStdout := os.Stdout
devNull, _ := os.Open(os.DevNull)
os.Stdout = devNull
defer func() {
os.Stdout = oldStdout
devNull.Close()
}()
for b.Loop() {
err := orchestrator.Execute(searchString, filePaths, false, false)
if err != nil {
b.Fatalf("Execute failed: %v", err)
}
}
}