-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_uncovered_test.go
More file actions
285 lines (251 loc) · 8.06 KB
/
Copy pathbenchmark_uncovered_test.go
File metadata and controls
285 lines (251 loc) · 8.06 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
package xref_test
// Benchmarks for critical paths not covered by the existing benchmark suite.
// Targets: LoadLanguage, Query compilation, Node.SExpr, Rewriter, InjectionParser.
import (
"os"
"strconv"
"strings"
"testing"
"github.com/startvibecoding/xref"
"github.com/startvibecoding/xref/internal/grammars"
)
// BenchmarkLoadLanguage measures grammar blob deserialization.
// This path runs once per language per process; hot in multi-tenant servers
// that spin up new language pools on demand.
func BenchmarkLoadLanguage(b *testing.B) {
blob, err := os.ReadFile("grammars/grammar_blobs/go.bin") //nolint:gocritic
if err != nil {
b.Skipf("grammar blob not accessible: %v", err)
}
b.ReportAllocs()
b.SetBytes(int64(len(blob)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
loaded, err := xref.LoadLanguage(blob)
if err != nil || loaded == nil {
b.Fatalf("LoadLanguage: %v", err)
}
}
}
// BenchmarkQueryCompile measures NewQuery pattern compilation from scratch.
// The existing BenchmarkQueryExecCompiled pre-compiles once; this isolates
// the compilation cost (pattern parse + symbol resolution + DFA build).
func BenchmarkQueryCompile(b *testing.B) {
lang := grammars.GoLanguage()
pattern := `
(function_declaration name: (identifier) @name) @func
(method_declaration name: (field_identifier) @name) @method
(call_expression function: (identifier) @callee)
`
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
q, err := xref.NewQuery(pattern, lang)
if err != nil || q == nil {
b.Fatalf("NewQuery: %v", err)
}
}
}
// BenchmarkNodeSExpr measures S-expression generation from a parsed tree.
// This path is hit heavily by editor integrations that display parse trees
// and by tests that compare output against reference strings.
func BenchmarkNodeSExpr(b *testing.B) {
lang := grammars.GoLanguage()
parser := xref.NewParser(lang)
src := makeGoBenchmarkSource(benchmarkFuncCount(b))
tree, err := parser.Parse(src)
if err != nil {
b.Fatalf("parse: %v", err)
}
b.Cleanup(func() { tree.Release() })
root := tree.RootNode()
b.ReportAllocs()
b.SetBytes(int64(len(src)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := root.SExpr(lang)
if s == "" {
b.Fatal("SExpr returned empty string")
}
}
}
// BenchmarkRewriterApply measures Rewriter.Apply for a rename-across-file edit.
// Rewriter is used in all refactoring and code-generation workflows; its edit
// validation and byte-splicing loop are not covered by any existing benchmark.
func BenchmarkRewriterApply(b *testing.B) {
lang := grammars.GoLanguage()
parser := xref.NewParser(lang)
src := makeGoBenchmarkSource(benchmarkFuncCount(b))
tree, err := parser.Parse(src)
if err != nil {
b.Fatalf("parse: %v", err)
}
b.Cleanup(func() { tree.Release() })
// Pre-collect all identifier nodes so we don't include query overhead.
q, err := xref.NewQuery(`(identifier) @id`, lang)
if err != nil {
b.Fatalf("NewQuery: %v", err)
}
matches := q.Execute(tree)
if len(matches) == 0 {
b.Fatal("no identifiers found")
}
b.ReportAllocs()
b.SetBytes(int64(len(src)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
rw := xref.NewRewriter(src)
// Rename every occurrence of "f0" -> "g0" to exercise the full rewrite path.
for _, m := range matches {
for _, cap := range m.Captures {
n := cap.Node
if string(src[n.StartByte():n.EndByte()]) == "f0" {
rw.Replace(n, []byte("g0"))
}
}
}
newSrc, _, err := rw.Apply()
if err != nil {
b.Fatalf("Apply: %v", err)
}
if len(newSrc) == 0 {
b.Fatal("Apply returned empty source")
}
}
}
// BenchmarkInjectionParseFull measures InjectionParser.Parse for a Markdown
// document with fenced code blocks. Injection parsing is used by editors that
// highlight embedded languages (markdown+Go, HTML+JS+CSS, Vue, Svelte).
func BenchmarkInjectionParseFull(b *testing.B) {
mdEntry := grammars.DetectLanguage("README.md")
if mdEntry == nil {
b.Skip("Markdown grammar not available")
}
goEntry := grammars.DetectLanguage("main.go")
if goEntry == nil {
b.Skip("Go grammar not available")
}
ip := xref.NewInjectionParser()
ip.RegisterLanguage("markdown", mdEntry.Language())
ip.RegisterLanguage("go", goEntry.Language())
// Use a markdown injection query that extracts fenced code blocks.
const mdInjectionQuery = `
(fenced_code_block
(info_string (language) @injection.language)
(code_fence_content) @injection.content)
`
if err := ip.RegisterInjectionQuery("markdown", mdInjectionQuery); err != nil {
b.Skipf("RegisterInjectionQuery: %v", err)
}
src := makeMDWithGoBlocks(benchmarkFuncCount(b))
b.ReportAllocs()
b.SetBytes(int64(len(src)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
result, err := ip.Parse(src, "markdown")
if err != nil {
b.Fatalf("InjectionParser.Parse: %v", err)
}
if result == nil {
b.Fatal("Parse returned nil result")
}
}
}
// BenchmarkInjectionParseIncremental measures InjectionParser.ParseIncremental
// for the same Markdown+Go corpus after a single-byte edit.
func BenchmarkInjectionParseIncremental(b *testing.B) {
mdEntry := grammars.DetectLanguage("README.md")
if mdEntry == nil {
b.Skip("Markdown grammar not available")
}
goEntry := grammars.DetectLanguage("main.go")
if goEntry == nil {
b.Skip("Go grammar not available")
}
ip := xref.NewInjectionParser()
ip.RegisterLanguage("markdown", mdEntry.Language())
ip.RegisterLanguage("go", goEntry.Language())
const mdInjectionQuery = `
(fenced_code_block
(info_string (language) @injection.language)
(code_fence_content) @injection.content)
`
if err := ip.RegisterInjectionQuery("markdown", mdInjectionQuery); err != nil {
b.Skipf("RegisterInjectionQuery: %v", err)
}
src := makeMDWithGoBlocks(benchmarkFuncCount(b))
// Warm-up parse to get the initial InjectionResult.
first, err := ip.Parse(src, "markdown")
if err != nil {
b.Fatalf("initial parse: %v", err)
}
b.ReportAllocs()
b.SetBytes(int64(len(src)))
b.ResetTimer()
prev := first
for i := 0; i < b.N; i++ {
// InjectionParser.ParseIncremental re-parses from current src; the caller
// is responsible for mutating src and marking edits on the inner trees
// directly. Here we just re-parse with toggled source to exercise the path.
result, err := ip.ParseIncremental(src, "markdown", prev)
if err != nil {
b.Fatalf("ParseIncremental: %v", err)
}
prev = result
}
}
// makeMDWithGoBlocks builds a Markdown document with n fenced Go code blocks.
func makeMDWithGoBlocks(n int) []byte {
var sb strings.Builder
sb.WriteString("# Doc\n\n")
for i := 0; i < n; i++ {
sb.WriteString("```go\n")
sb.WriteString("func f")
sb.WriteString(itoa(i))
sb.WriteString("() int { return ")
sb.WriteString(itoa(i))
sb.WriteString(" }\n")
sb.WriteString("```\n\n")
}
return []byte(sb.String())
}
// BenchmarkParserPoolSerial measures checkout→parse→release in a single goroutine.
// This isolates pool overhead (sync.Pool Get/Put + applyDefaults) from parse time.
func BenchmarkParserPoolSerial(b *testing.B) {
lang := grammars.GoLanguage()
pool := xref.NewParserPool(lang)
src := makeGoBenchmarkSource(benchmarkFuncCount(b))
b.ReportAllocs()
b.SetBytes(int64(len(src)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
tree, err := pool.Parse(src)
if err != nil || tree == nil {
b.Fatalf("ParserPool.Parse: %v", err)
}
}
}
// BenchmarkParserPoolConcurrentThroughput measures throughput under goroutine
// contention — the scenario that justifies pooling over per-request allocation.
// RunParallel drives GOMAXPROCS goroutines simultaneously; sync.Pool shines here
// because each OS thread maintains a per-P free list, minimising cross-core
// cache traffic on the Parser's reuse cursor and arena hint fields.
func BenchmarkParserPoolConcurrentThroughput(b *testing.B) {
lang := grammars.GoLanguage()
pool := xref.NewParserPool(lang)
src := makeGoBenchmarkSource(benchmarkFuncCount(b))
b.ReportAllocs()
b.SetBytes(int64(len(src)))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
tree, err := pool.Parse(src)
if err != nil || tree == nil {
b.Fatalf("ParserPool.Parse: %v", err)
}
}
})
}
func itoa(n int) string {
return strconv.Itoa(n)
}