-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_query_test.go
More file actions
86 lines (72 loc) · 1.94 KB
/
Copy pathbenchmark_query_test.go
File metadata and controls
86 lines (72 loc) · 1.94 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
package xref_test
import (
"testing"
"github.com/startvibecoding/xref"
"github.com/startvibecoding/xref/internal/grammars"
)
// BenchmarkQueryExec measures query compilation + execution on a 500-function Go file.
func BenchmarkQueryExec(b *testing.B) {
entry := grammars.DetectLanguage("main.go")
if entry == nil {
b.Skip("Go grammar not available")
}
lang := entry.Language()
src := makeGoBenchmarkSource(benchmarkFuncCount(b))
ts := mustGoTokenSource(b, src, lang)
parser := xref.NewParser(lang)
tree, err := parser.ParseWithTokenSource(src, ts)
if err != nil {
b.Fatalf("parse failed: %v", err)
}
if tree.RootNode() == nil {
b.Fatal("parse returned nil root")
}
defer tree.Release()
highlightQuery := entry.HighlightQuery
b.ReportAllocs()
b.SetBytes(int64(len(src)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
q, err := xref.NewQuery(highlightQuery, lang)
if err != nil {
b.Fatalf("NewQuery failed: %v", err)
}
matches := q.Execute(tree)
if len(matches) == 0 {
b.Fatal("query returned no matches")
}
}
}
// BenchmarkQueryExecCompiled measures execution of a pre-compiled query,
// amortizing the compilation cost.
func BenchmarkQueryExecCompiled(b *testing.B) {
entry := grammars.DetectLanguage("main.go")
if entry == nil {
b.Skip("Go grammar not available")
}
lang := entry.Language()
src := makeGoBenchmarkSource(benchmarkFuncCount(b))
ts := mustGoTokenSource(b, src, lang)
parser := xref.NewParser(lang)
tree, err := parser.ParseWithTokenSource(src, ts)
if err != nil {
b.Fatalf("parse failed: %v", err)
}
if tree.RootNode() == nil {
b.Fatal("parse returned nil root")
}
defer tree.Release()
q, err := xref.NewQuery(entry.HighlightQuery, lang)
if err != nil {
b.Fatalf("NewQuery failed: %v", err)
}
b.ReportAllocs()
b.SetBytes(int64(len(src)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
matches := q.Execute(tree)
if len(matches) == 0 {
b.Fatal("query returned no matches")
}
}
}