Debian Codegraph @

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

raw symbols SCIP
1 // Copyright 2023 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 package bench
5
6 import (
7 "fmt"
8 "path"
9 "regexp"
10 "testing"
11
12 . "golang.org/x/tools/gopls/internal/test/integration"
13 )
14
15 // BenchmarkReload benchmarks reloading a file metadata after a change to an import.
16 //
17 // This ensures we are able to diagnose a changed file without reloading all
18 // invalidated packages. See also golang/go#61344
19 func BenchmarkReload(b *testing.B) {
20 type replace map[string]string
21 tests := []struct {
22 repo string
23 file string
24 // replacements must be 'reversible', in the sense that the replacing
25 // string is unique.
26 replace replace
27 }{
28 // pkg/util/hash is transitively imported by a large number of packages. We
29 // should not need to reload those packages to get a diagnostic.
30 {"kubernetes", "pkg/util/hash/hash.go", replace{`"hash"`: `"hashx"`}},
31 {"kubernetes", "pkg/kubelet/kubelet.go", replace{
32 `"k8s.io/kubernetes/pkg/kubelet/config"`: `"k8s.io/kubernetes/pkg/kubelet/configx"`,
33 }},
34 }
35
36 for _, test := range tests {
37 b.Run(fmt.Sprintf("%s/%s", test.repo, path.Base(test.file)), func(b *testing.B) {
38 env := getRepo(b, test.repo).sharedEnv(b)
39
40 env.OpenFile(test.file)
41 defer closeBuffer(b, env, test.file)
42
43 env.AfterChange()
44
45 profileName := qualifiedName("reload", test.repo, path.Base(test.file))
46 if stopAndRecord := startProfileIfSupported(b, env, profileName); stopAndRecord != nil {
47 defer stopAndRecord()
48 }
49
50 b.ResetTimer()
51 for b.Loop() {
52 // Mutate the file. This may result in cache hits, but that's OK: the
53 // goal is to ensure that we don't reload more than just the current
54 // package.
55 for k, v := range test.replace {
56 env.RegexpReplace(test.file, regexp.QuoteMeta(k), v)
57 }
58 // Note: don't use env.AfterChange() here: we only want to await the
59 // first diagnostic.
60 //
61 // Awaiting a full diagnosis would await diagnosing everything, which
62 // would require reloading everything.
63 env.Await(Diagnostics(ForFile(test.file)))
64 for k, v := range test.replace {
65 env.RegexpReplace(test.file, regexp.QuoteMeta(v), k)
66 }
67 env.Await(NoDiagnostics(ForFile(test.file)))
68 }
69 })
70 }
71 }