| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 19 |
|
| 20 |
|
| 21 |
var editID int64 = time.Now().UnixNano() |
| 22 |
|
| 23 |
type changeTest struct { |
| 24 |
repo string |
| 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 |
|
| 34 |
|
| 35 |
{"kubernetes.types", "staging/src/k8s.io/api/core/v1/types.go", false}, |
| 36 |
{"kuma", "api/generic/insights.go", true}, |
| 37 |
{"oracle", "dataintegration/data_type.go", false}, |
| 38 |
{"pkgsite", "internal/frontend/server.go", true}, |
| 39 |
{"starlark", "starlark/eval.go", true}, |
| 40 |
{"tools", "internal/lsp/cache/snapshot.go", true}, |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 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 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
func makeEditFunc(env *Env, file string) func() { |
| 81 |
|
| 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 |
|
| 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 |
|
| 105 |
|
| 106 |
func BenchmarkDiagnoseSave(b *testing.B) { |
| 107 |
for _, test := range didChangeTests { |
| 108 |
runChangeDiagnosticsBenchmark(b, test, true, "diagnoseSave") |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 129 |
|
| 130 |
env := getRepo(b, test.repo).newEnv(b, config, operation, false) |
| 131 |
defer env.Close() |
| 132 |
env.OpenFile(test.file) |
| 133 |
|
| 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 |
|
| 142 |
|
| 143 |
|
| 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 |
|
| 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 |
} |