-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
338 lines (287 loc) · 7.35 KB
/
Copy pathbench_test.go
File metadata and controls
338 lines (287 loc) · 7.35 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
package gogenfilter
import (
"fmt"
"io"
"maps"
"strings"
"testing"
"testing/fstest"
)
const (
sqlcGeneratedContent = "// Code generated by sqlc. DO NOT EDIT.\npackage db\n\ntype User struct {\n\tID int64\n\tName string\n}"
protobufGeneratedContent = "// Code generated by protoc-gen-go. DO NOT EDIT.\npackage pb\n"
genericGeneratedContent = "// Code generated by some-tool. DO NOT EDIT.\npackage main\n"
)
func benchmarkFilter(b *testing.B, enabled bool) {
b.Helper()
fsys := fstest.MapFS{
dbModelsGo: &fstest.MapFile{
Data: []byte(sqlcGeneratedContent),
},
}
var (
filter *Filter
err error
)
if enabled {
opts, optErr := WithFilterOptions(FilterAll)
if optErr != nil {
b.Fatal(optErr)
}
filter, err = NewFilter(opts, WithFS(fsys))
if err != nil {
b.Fatal(err)
}
} else {
filter, err = NewFilter(WithFS(fsys))
if err != nil {
b.Fatal(err)
}
}
for b.Loop() {
_, _ = filter.Filter("db/models.go")
}
}
func BenchmarkFilter(b *testing.B) {
tests := []struct {
name string
enabled bool
}{
{name: "enabled", enabled: true},
{name: "disabled", enabled: false},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
benchmarkFilter(b, tt.enabled)
})
}
}
func BenchmarkDetectGenerated(b *testing.B) {
opts := optionsMap(FilterAll)
for b.Loop() {
_, _ = getFilenameBasedReasonWithTrace(dbModelsGo, opts)
}
}
func benchmarkIsGenerated(b *testing.B, content string, fn func(string, string) bool) {
b.Helper()
for b.Loop() {
_ = fn("file.go", content)
}
}
func BenchmarkIsSQLCGenerated(b *testing.B) {
benchmarkIsGenerated(
b,
sqlcGeneratedContent,
IsSQLCGenerated,
)
}
func BenchmarkIsProtobufGenerated(b *testing.B) {
benchmarkIsGenerated(
b,
protobufGeneratedContent,
IsProtobufGenerated,
)
}
func BenchmarkIsGenericGenerated(b *testing.B) {
benchmarkIsGenerated(
b,
genericGeneratedContent,
IsGenericGenerated,
)
}
func BenchmarkMatchPattern(b *testing.B) {
tests := []struct {
name string
path string
pattern string
}{
{name: "exact", path: fileGo, pattern: fileGo},
{name: "wildcard", path: "test.go", pattern: wildcardGo},
{name: "doublestar", path: "a/b/c/file.go", pattern: "**/*.go"},
{name: "question", path: aGo, pattern: questionGo},
{name: "no_match", path: "other.txt", pattern: wildcardGo},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
for b.Loop() {
_ = MatchPattern(tt.path, tt.pattern)
}
})
}
}
func BenchmarkDetectReason(b *testing.B) {
tests := []struct {
name string
path string
content string
}{
{
name: "filename_only",
path: dbModelsGo,
content: sqlcGeneratedContentTest,
},
{
name: "content_based",
path: customGenGo,
content: myToolGenContentTest,
},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
for b.Loop() {
_ = DetectReason(tt.path, tt.content, FilterAll)
}
})
}
b.Run("not_filtered", func(b *testing.B) {
for b.Loop() {
_ = DetectReason("main.go", "package main\n", FilterAll)
}
})
}
func BenchmarkDetectReasonReader(b *testing.B) {
tests := []struct {
name string
path string
content string
}{
{
name: "sqlc_filename",
path: dbModelsGo,
content: sqlcGeneratedContent,
},
{
name: "not_filtered",
path: mainGo,
content: packageMainFunc,
},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
for b.Loop() {
reader := io.NopCloser(strings.NewReader(tt.content))
_, _ = DetectReasonReader(tt.path, reader, FilterAll)
}
})
}
}
func BenchmarkDetectReasonFileFS(b *testing.B) {
fsys := fstest.MapFS{
pageTemplGo: &fstest.MapFile{
Data: []byte("package main\nfunc main() {}\n"),
},
"custom_gen.go": &fstest.MapFile{
Data: []byte(genericGeneratedContent),
},
mainGo: &fstest.MapFile{
Data: []byte(packageMainFunc),
},
}
b.Run("filename_only", func(b *testing.B) {
for b.Loop() {
_, _ = DetectReasonFileFS(fsys, "page_templ.go", FilterAll)
}
})
b.Run("content_based", func(b *testing.B) {
for b.Loop() {
_, _ = DetectReasonFileFS(fsys, "custom_gen.go", FilterAll)
}
})
b.Run("not_filtered", func(b *testing.B) {
for b.Loop() {
_, _ = DetectReasonFileFS(fsys, mainGo, FilterAll)
}
})
}
// BenchmarkSQLCDerivedConfigForFS measures the cost of lazily discovering and
// parsing sqlc configs by walking a large filesystem with many directories.
// The "no_config" variant walks a large FS with zero sqlc.yaml files.
// The "with_config" variant includes a single sqlc.yaml at the root.
func BenchmarkSQLCDerivedConfigForFS(b *testing.B) {
const numDirs = 200
// Build a large MapFS with many directories and .go files, no sqlc config.
largeFS := make(fstest.MapFS, numDirs*2+5)
for i := range numDirs {
dir := fmt.Sprintf("pkg/depth%d/file.go", i)
largeFS[dir] = &fstest.MapFile{Data: []byte("package pkg\n")}
}
// Variant with a sqlc config at root.
configFS := make(fstest.MapFS, len(largeFS)+1)
maps.Copy(configFS, largeFS)
configFS["sqlc.yaml"] = &fstest.MapFile{Data: []byte(sqlcConfigV2YAML())}
b.Run("no_config_large_fs", func(b *testing.B) {
for b.Loop() {
_, _ = sqlcDerivedConfigForFS(largeFS, []string{"."})
}
})
b.Run("with_config_large_fs", func(b *testing.B) {
for b.Loop() {
_, _ = sqlcDerivedConfigForFS(configFS, []string{"."})
}
})
b.Run("empty_fs", func(b *testing.B) {
emptyFS := fstest.MapFS{}
for b.Loop() {
_, _ = sqlcDerivedConfigForFS(emptyFS, []string{"."})
}
})
}
// BenchmarkFilterSQLCDerivedConfigCached measures the cost of the cached
// derived config lookup (second+ call should be O(1) via atomic.Pointer.Load).
func BenchmarkFilterSQLCDerivedConfigCached(b *testing.B) {
fsys := fstest.MapFS{
"sqlc.yaml": &fstest.MapFile{Data: []byte(sqlcConfigV2YAML())},
"db/models.go": &fstest.MapFile{Data: []byte("package db\n")},
}
opts, err := WithFilterOptions(FilterSQLC)
if err != nil {
b.Fatal(err)
}
filter, err := NewFilter(opts, WithFS(fsys))
if err != nil {
b.Fatal(err)
}
// Prime the cache with one call.
result, _, _ := filter.FilterDetailedAndContent("db/models.go")
_ = result
b.ResetTimer()
for b.Loop() {
_ = filter.sqlcDerivedConfig()
}
}
// BenchmarkFilterDetailedAndContent measures the cost of FilterDetailedAndContent
// including file reading and content return, for both filename-match (no content
// read) and content-match (content read and returned) paths.
func BenchmarkFilterDetailedAndContent(b *testing.B) {
fsys := fstest.MapFS{
"db/query.sql.go": &fstest.MapFile{Data: []byte(sqlcGeneratedContent)},
"repository.go": &fstest.MapFile{Data: []byte(genericGeneratedContent)},
"main.go": &fstest.MapFile{Data: []byte("package main\nfunc main() {}\n")},
}
opts, err := WithFilterOptions(FilterAll)
if err != nil {
b.Fatal(err)
}
filter, err := NewFilter(opts, WithFS(fsys))
if err != nil {
b.Fatal(err)
}
b.Run("filename_match_no_content", func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _, _ = filter.FilterDetailedAndContent("db/query.sql.go")
}
})
b.Run("content_match_returns_content", func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _, _ = filter.FilterDetailedAndContent("repository.go")
}
})
b.Run("not_filtered_reads_content", func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _, _ = filter.FilterDetailedAndContent("main.go")
}
})
}