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
|
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bench
import (
"flag"
"fmt"
"sync/atomic"
"testing"
"golang.org/x/tools/gopls/internal/protocol"
. "golang.org/x/tools/gopls/internal/test/integration"
"golang.org/x/tools/gopls/internal/test/integration/fake"
)
var completionGOPATH = flag.String("completion_gopath", "", "if set, use this GOPATH for BenchmarkCompletion")
type completionBenchOptions struct {
file, locationRegexp string
// Hooks to run edits before initial completion
setup func(*Env) // run before the benchmark starts
beforeCompletion func(*Env) // run before each completion
}
// Deprecated: new tests should be expressed in BenchmarkCompletion.
func benchmarkCompletion(options completionBenchOptions, b *testing.B) {
repo := getRepo(b, "tools")
_ = repo.sharedEnv(b) // ensure cache is warm
env := repo.newEnv(b, fake.EditorConfig{}, "completion", false)
defer env.Close()
// Run edits required for this completion.
if options.setup != nil {
options.setup(env)
}
// Run a completion to make sure the system is warm.
loc := env.RegexpSearch(options.file, options.locationRegexp)
completions := env.Completion(loc)
if testing.Verbose() {
fmt.Println("Results:")
for i := 0; i < len(completions.Items); i++ {
fmt.Printf("\t%d. %v\n", i, completions.Items[i])
}
}
b.Run("tools", func(b *testing.B) {
if stopAndRecord := startProfileIfSupported(b, env, qualifiedName("tools", "completion")); stopAndRecord != nil {
defer stopAndRecord()
}
for i := 0; i < b.N; i++ {
if options.beforeCompletion != nil {
options.beforeCompletion(env)
}
env.Completion(loc)
}
})
}
// endRangeInBuffer returns the position for last character in the buffer for
// the given file.
func endRangeInBuffer(env *Env, name string) protocol.Range {
buffer := env.BufferText(name)
m := protocol.NewMapper("", []byte(buffer))
rng, err := m.OffsetRange(len(buffer), len(buffer))
if err != nil {
env.T.Fatal(err)
}
return rng
}
// Benchmark struct completion in tools codebase.
func BenchmarkStructCompletion(b *testing.B) {
file := "internal/lsp/cache/session.go"
setup := func(env *Env) {
env.OpenFile(file)
env.EditBuffer(file, protocol.TextEdit{
Range: endRangeInBuffer(env, file),
NewText: "\nvar testVariable map[string]bool = Session{}.\n",
})
}
benchmarkCompletion(completionBenchOptions{
file: file,
locationRegexp: `var testVariable map\[string\]bool = Session{}(\.)`,
setup: setup,
}, b)
}
// Benchmark import completion in tools codebase.
func BenchmarkImportCompletion(b *testing.B) {
const file = "internal/lsp/source/completion/completion.go"
benchmarkCompletion(completionBenchOptions{
file: file,
locationRegexp: `go\/()`,
setup: func(env *Env) { env.OpenFile(file) },
}, b)
}
// Benchmark slice completion in tools codebase.
func BenchmarkSliceCompletion(b *testing.B) {
file := "internal/lsp/cache/session.go"
setup := func(env *Env) {
env.OpenFile(file)
env.EditBuffer(file, protocol.TextEdit{
Range: endRangeInBuffer(env, file),
NewText: "\nvar testVariable []byte = \n",
})
}
benchmarkCompletion(completionBenchOptions{
file: file,
locationRegexp: `var testVariable \[\]byte (=)`,
setup: setup,
}, b)
}
// Benchmark deep completion in function call in tools codebase.
func BenchmarkFuncDeepCompletion(b *testing.B) {
file := "internal/lsp/source/completion/completion.go"
fileContent := `
func (c *completer) _() {
c.inference.kindMatches(c.)
}
`
setup := func(env *Env) {
env.OpenFile(file)
originalBuffer := env.BufferText(file)
env.EditBuffer(file, protocol.TextEdit{
Range: endRangeInBuffer(env, file),
// TODO(rfindley): this is a bug: it should just be fileContent.
NewText: originalBuffer + fileContent,
})
}
benchmarkCompletion(completionBenchOptions{
file: file,
locationRegexp: `func \(c \*completer\) _\(\) {\n\tc\.inference\.kindMatches\((c)`,
setup: setup,
}, b)
}
type completionTest struct {
repo string
name string
file string // repo-relative file to create
content string // file content
locationRegexp string // regexp for completion
}
var completionTests = []completionTest{
{
"tools",
"selector",
"internal/lsp/source/completion/completion2.go",
`
package completion
func (c *completer) _() {
c.inference.kindMatches(c.)
}
`,
`func \(c \*completer\) _\(\) {\n\tc\.inference\.kindMatches\((c)`,
},
{
"tools",
"unimportedident",
"internal/lsp/source/completion/completion2.go",
`
package completion
func (c *completer) _() {
lo
}
`,
`lo()`,
},
{
"tools",
"unimportedselector",
"internal/lsp/source/completion/completion2.go",
`
package completion
func (c *completer) _() {
log.
}
`,
`log\.()`,
},
{
"kubernetes",
"selector",
"pkg/kubelet/kubelet2.go",
`
package kubelet
func (kl *Kubelet) _() {
kl.
}
`,
`kl\.()`,
},
{
"kubernetes",
"identifier",
"pkg/kubelet/kubelet2.go",
`
package kubelet
func (kl *Kubelet) _() {
k // here
}
`,
`k() // here`,
},
{
"oracle",
"selector",
"dataintegration/pivot2.go",
`
package dataintegration
func (p *Pivot) _() {
p.
}
`,
`p\.()`,
},
}
// Benchmark completion following an arbitrary edit.
//
// Edits force type-checked packages to be invalidated, so we want to measure
// how long it takes before completion results are available.
func BenchmarkCompletion(b *testing.B) {
for _, test := range completionTests {
b.Run(fmt.Sprintf("%s_%s", test.repo, test.name), func(b *testing.B) {
for _, followingEdit := range []bool{true, false} {
b.Run(fmt.Sprintf("edit=%v", followingEdit), func(b *testing.B) {
for _, completeUnimported := range []bool{true, false} {
b.Run(fmt.Sprintf("unimported=%v", completeUnimported), func(b *testing.B) {
for _, budget := range []string{"0s", "100ms"} {
b.Run(fmt.Sprintf("budget=%s", budget), func(b *testing.B) {
runCompletion(b, test, followingEdit, completeUnimported, budget)
})
}
})
}
})
}
})
}
}
// For optimizing unimported completion, it can be useful to benchmark with a
// huge GOMODCACHE.
var gomodcache = flag.String("gomodcache", "", "optional GOMODCACHE for unimported completion benchmarks")
func runCompletion(b *testing.B, test completionTest, followingEdit, completeUnimported bool, budget string) {
repo := getRepo(b, test.repo)
gopath := *completionGOPATH
if gopath == "" {
// use a warm GOPATH
sharedEnv := repo.sharedEnv(b)
gopath = sharedEnv.Sandbox.GOPATH()
}
envvars := map[string]string{
"GOPATH": gopath,
}
if *gomodcache != "" {
envvars["GOMODCACHE"] = *gomodcache
}
env := repo.newEnv(b, fake.EditorConfig{
Env: envvars,
Settings: map[string]interface{}{
"completeUnimported": completeUnimported,
"completionBudget": budget,
},
}, "completion", false)
defer env.Close()
env.CreateBuffer(test.file, "// __TEST_PLACEHOLDER_0__\n"+test.content)
editPlaceholder := func() {
edits := atomic.AddInt64(&editID, 1)
env.EditBuffer(test.file, protocol.TextEdit{
Range: protocol.Range{
Start: protocol.Position{Line: 0, Character: 0},
End: protocol.Position{Line: 1, Character: 0},
},
// Increment the placeholder text, to ensure cache misses.
NewText: fmt.Sprintf("// __TEST_PLACEHOLDER_%d__\n", edits),
})
}
env.AfterChange()
// Run a completion to make sure the system is warm.
loc := env.RegexpSearch(test.file, test.locationRegexp)
completions := env.Completion(loc)
if testing.Verbose() {
fmt.Println("Results:")
for i, item := range completions.Items {
fmt.Printf("\t%d. %v\n", i, item)
}
}
b.ResetTimer()
if stopAndRecord := startProfileIfSupported(b, env, qualifiedName(test.repo, "completion")); stopAndRecord != nil {
defer stopAndRecord()
}
for i := 0; i < b.N; i++ {
if followingEdit {
editPlaceholder()
}
loc := env.RegexpSearch(test.file, test.locationRegexp)
env.Completion(loc)
}
}
|