Debian Codegraph @

gopls/internal/test/integration/bench/didchange_test.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package bench
6
7 import (
8 "fmt"
9 "sync/atomic"
10 "testing"
11 "time"
12
13 "golang.org/x/tools/gopls/internal/protocol"
14 . "golang.org/x/tools/gopls/internal/test/integration"
15 "golang.org/x/tools/gopls/internal/test/integration/fake"
16 )
17
18 // Use a global edit counter as bench function may execute multiple times, and
19 // we want to avoid cache hits. Use time.Now to also avoid cache hits from the
20 // shared file cache.
21 var editID int64 = time.Now().UnixNano()
22
23 type changeTest struct {
24 repo string // repo identifier + optional disambiguating ".foo" suffix
25 file string
26 canSave bool
27 }
28
29 var didChangeTests = []changeTest{
30 {"google-cloud-go", "internal/annotate.go", true},
31 {"istio", "pkg/fuzz/util.go", true},
32 {"kubernetes", "pkg/controller/lookup_cache.go", true},
33 // disable kubernetes.types as it fails on the builders, but not locally on mac or glinux
34 // error is didchange_test.go:134: formatting before save: editing "..."": column is beyond end of line;
35 {"kubernetes.types", "staging/src/k8s.io/api/core/v1/types.go", false}, // results in 25K file batch!
36 {"kuma", "api/generic/insights.go", true},
37 {"oracle", "dataintegration/data_type.go", false}, // diagnoseSave fails because this package is generated
38 {"pkgsite", "internal/frontend/server.go", true},
39 {"starlark", "starlark/eval.go", true},
40 {"tools", "internal/lsp/cache/snapshot.go", true},
41 }
42
43 // BenchmarkDidChange benchmarks modifications of a single file by making
44 // synthetic modifications in a comment. It controls pacing by waiting for the
45 // server to actually start processing the didChange notification before
46 // proceeding. Notably it does not wait for diagnostics to complete.
47 func BenchmarkDidChange(b *testing.B) {
48 for _, test := range didChangeTests {
49 b.Run(test.repo, func(b *testing.B) {
50 env := getRepo(b, test.repo).sharedEnv(b)
51 env.OpenFile(test.file)
52 defer closeBuffer(b, env, test.file)
53
54 // Insert the text we'll be modifying at the top of the file.
55 edit := makeEditFunc(env, test.file)
56
57 if stopAndRecord := startProfileIfSupported(b, env, qualifiedName(test.repo, "didchange")); stopAndRecord != nil {
58 defer stopAndRecord()
59 }
60 b.ResetTimer()
61
62 for b.Loop() {
63 edit()
64 env.Await(env.StartedChange())
65 }
66 })
67 }
68 }
69
70 // makeEditFunc prepares the given file for incremental editing, by inserting a
71 // placeholder comment that will be overwritten with a new unique value by each
72 // call to the resulting function. While makeEditFunc awaits gopls to finish
73 // processing the initial edit, the callback for incremental edits does not
74 // await any gopls state.
75 //
76 // This is used for benchmarks that must repeatedly invalidate a file's
77 // contents.
78 //
79 // TODO(rfindley): use this throughout.
80 func makeEditFunc(env *Env, file string) func() {
81 // Insert the text we'll be modifying at the top of the file.
82 env.EditBuffer(file, protocol.TextEdit{NewText: "// __TEST_PLACEHOLDER_0__\n"})
83 env.AfterChange()
84
85 return func() {
86 edits := atomic.AddInt64(&editID, 1)
87 env.EditBuffer(file, protocol.TextEdit{
88 Range: protocol.Range{
89 Start: protocol.Position{Line: 0, Character: 0},
90 End: protocol.Position{Line: 1, Character: 0},
91 },
92 // Increment the placeholder text, to ensure cache misses.
93 NewText: fmt.Sprintf("// __TEST_PLACEHOLDER_%d__\n", edits),
94 })
95 }
96 }
97
98 func BenchmarkDiagnoseChange(b *testing.B) {
99 for _, test := range didChangeTests {
100 runChangeDiagnosticsBenchmark(b, test, false, "diagnoseChange")
101 }
102 }
103
104 // TODO(rfindley): add a benchmark for with a metadata-affecting change, when
105 // this matters.
106 func BenchmarkDiagnoseSave(b *testing.B) {
107 for _, test := range didChangeTests {
108 runChangeDiagnosticsBenchmark(b, test, true, "diagnoseSave")
109 }
110 }
111
112 // runChangeDiagnosticsBenchmark runs a benchmark to edit the test file and
113 // await the resulting diagnostics pass. If save is set, the file is also saved.
114 func runChangeDiagnosticsBenchmark(b *testing.B, test changeTest, save bool, operation string) {
115 b.Run(test.repo, func(b *testing.B) {
116 if !test.canSave {
117 b.Skipf("skipping as %s cannot be saved", test.file)
118 }
119 sharedEnv := getRepo(b, test.repo).sharedEnv(b)
120 config := fake.EditorConfig{
121 Env: map[string]string{
122 "GOPATH": sharedEnv.Sandbox.GOPATH(),
123 },
124 Settings: map[string]any{
125 "diagnosticsDelay": "0s",
126 },
127 }
128 // Use a new env to avoid the diagnostic delay: we want to measure how
129 // long it takes to produce the diagnostics.
130 env := getRepo(b, test.repo).newEnv(b, config, operation, false)
131 defer env.Close()
132 env.OpenFile(test.file)
133 // Insert the text we'll be modifying at the top of the file.
134 env.EditBuffer(test.file, protocol.TextEdit{NewText: "// __TEST_PLACEHOLDER_0__\n"})
135 if save {
136 env.SaveBuffer(test.file)
137 }
138 env.AfterChange()
139 b.ResetTimer()
140
141 // We must use an extra subtest layer here, so that we only set up the
142 // shared env once (otherwise we pay additional overhead and the profiling
143 // flags don't work).
144 b.Run("diagnose", func(b *testing.B) {
145 if stopAndRecord := startProfileIfSupported(b, env, qualifiedName(test.repo, operation)); stopAndRecord != nil {
146 defer stopAndRecord()
147 }
148 for b.Loop() {
149 edits := atomic.AddInt64(&editID, 1)
150 env.EditBuffer(test.file, protocol.TextEdit{
151 Range: protocol.Range{
152 Start: protocol.Position{Line: 0, Character: 0},
153 End: protocol.Position{Line: 1, Character: 0},
154 },
155 // Increment the placeholder text, to ensure cache misses.
156 NewText: fmt.Sprintf("// __TEST_PLACEHOLDER_%d__\n", edits),
157 })
158 if save {
159 env.SaveBuffer(test.file)
160 }
161 env.AfterChange()
162 }
163 })
164 })
165 }