Repository navigation
Expand file tree
/
Copy pathgitignore_bench_test.go
More file actions
180 lines (167 loc) · 4.5 KB
/
Copy pathgitignore_bench_test.go
File metadata and controls
180 lines (167 loc) · 4.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
package gitignore_test
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/git-pkgs/gitignore"
)
func benchMatcher(b *testing.B, patterns string) *gitignore.Matcher {
b.Helper()
root := b.TempDir()
if err := os.MkdirAll(filepath.Join(root, ".git", "info"), 0755); err != nil {
b.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, ".gitignore"), []byte(patterns), 0644); err != nil {
b.Fatal(err)
}
return gitignore.New(root)
}
func realisticPatterns() string {
var b strings.Builder
// Extensions
exts := []string{"log", "tmp", "bak", "swp", "swo", "o", "a", "so", "dylib",
"pyc", "pyo", "class", "jar", "war", "ear", "dll", "exe", "obj", "lib",
"out", "app", "DS_Store", "thumbs.db", "desktop.ini", "iml", "ipr", "iws"}
for _, ext := range exts {
fmt.Fprintf(&b, "*.%s\n", ext)
}
// Directories
dirs := []string{"node_modules/", "vendor/", "build/", "dist/", "target/",
".cache/", ".tmp/", "__pycache__/", ".pytest_cache/", "coverage/",
".nyc_output/", ".next/", ".nuxt/", ".output/", ".vscode/", ".idea/",
".gradle/", ".mvn/", "bin/", "obj/"}
for _, d := range dirs {
b.WriteString(d)
b.WriteByte('\n')
}
// Doublestar patterns
dsPats := []string{"**/logs/**", "**/.env", "**/.env.*", "**/secret*",
"**/credentials.*", "**/*.min.js", "**/*.min.css", "**/*.map"}
for _, p := range dsPats {
b.WriteString(p)
b.WriteByte('\n')
}
// Negation
b.WriteString("!.env.example\n")
b.WriteString("!important.log\n")
// Anchored
b.WriteString("/Makefile.local\n")
b.WriteString("/config/local.yml\n")
// Bracket
b.WriteString("*.[oa]\n")
b.WriteString("*~\n")
b.WriteString(".*.sw[a-p]\n")
return b.String()
}
func BenchmarkCompile(b *testing.B) {
patterns := realisticPatterns()
root := b.TempDir()
if err := os.MkdirAll(filepath.Join(root, ".git", "info"), 0755); err != nil {
b.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, ".gitignore"), []byte(patterns), 0644); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for b.Loop() {
gitignore.New(root)
}
}
func BenchmarkMatchHit(b *testing.B) {
m := benchMatcher(b, realisticPatterns())
b.ResetTimer()
for b.Loop() {
m.Match("src/app.log")
}
}
func BenchmarkMatchMiss(b *testing.B) {
m := benchMatcher(b, realisticPatterns())
b.ResetTimer()
for b.Loop() {
m.Match("src/main.go")
}
}
func BenchmarkMatchLargePatternSet(b *testing.B) {
var sb strings.Builder
sb.WriteString(realisticPatterns())
for i := range 200 {
fmt.Fprintf(&sb, "pattern_%d_*.txt\n", i)
}
m := benchMatcher(b, sb.String())
b.ResetTimer()
for b.Loop() {
m.Match("src/components/Button.tsx")
}
}
func BenchmarkMatchDeepPath(b *testing.B) {
m := benchMatcher(b, realisticPatterns())
b.ResetTimer()
for b.Loop() {
m.Match("a/b/c/d/e/f/g/file.txt")
}
}
func BenchmarkMatchLiteral(b *testing.B) {
for _, tc := range []struct {
name string
path string
want bool
}{
{"RootHit", "vendor/", true},
{"DeepHit", "a/b/c/d/e/f/g/vendor/", true},
{"DeepMiss", "a/b/c/d/e/f/g/source/", false},
{"ExcludedParent", "vendor/a/b/c/d/e/f/g/source.go", true},
{"SuffixCollision", "a/b/c/d/e/f/g/myvendor/", false},
} {
b.Run(tc.name, func(b *testing.B) {
m := benchMatcher(b, realisticPatterns())
if got := m.Match(tc.path); got != tc.want {
b.Fatalf("Match(%q) = %v, want %v", tc.path, got, tc.want)
}
for b.Loop() {
m.Match(tc.path)
}
})
}
}
func BenchmarkMatchNestedPatterns(b *testing.B) {
m := benchMatcher(b, realisticPatterns())
for _, dir := range []string{"src", "src/pkg", "src/pkg/internal", "src/pkg/internal/util"} {
m.AddPatterns([]byte("*.gen.go\nlocal/\n*.tmp\n"), dir)
}
b.ResetTimer()
for b.Loop() {
m.Match("src/pkg/internal/util/main.go")
}
}
func BenchmarkWalk(b *testing.B) {
b.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
root := b.TempDir()
if err := os.MkdirAll(filepath.Join(root, ".git", "info"), 0755); err != nil {
b.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, ".gitignore"), []byte(realisticPatterns()), 0644); err != nil {
b.Fatal(err)
}
for i := range 10 {
dir := filepath.Join(root, fmt.Sprintf("d%d", i))
for j := range 5 {
sub := filepath.Join(dir, fmt.Sprintf("s%d", j))
if err := os.MkdirAll(sub, 0755); err != nil {
b.Fatal(err)
}
for _, name := range []string{"a.go", "b.go", "c.log", "d.tmp"} {
if err := os.WriteFile(filepath.Join(sub, name), []byte("x"), 0644); err != nil {
b.Fatal(err)
}
}
}
}
b.ResetTimer()
for b.Loop() {
_ = gitignore.Walk(root, func(path string, d os.DirEntry) error {
return nil
})
}
}