Debian Codegraph @

gopls/internal/test/integration/bench/repo_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
5 package bench
6
7 import (
8 "bytes"
9 "context"
10 "errors"
11 "flag"
12 "fmt"
13 "log"
14 "os"
15 "path/filepath"
16 "strings"
17 "sync"
18 "testing"
19 "time"
20
21 . "golang.org/x/tools/gopls/internal/test/integration"
22 "golang.org/x/tools/gopls/internal/test/integration/fake"
23 )
24
25 // repos holds shared repositories for use in benchmarks.
26 //
27 // These repos were selected to represent a variety of different types of
28 // codebases.
29 var repos = map[string]*repo{
30 // google-cloud-go has 145 workspace modules (!), and is quite large.
31 "google-cloud-go": {
32 name: "google-cloud-go",
33 url: "https://github.com/googleapis/google-cloud-go.git",
34 commit: "07da765765218debf83148cc7ed8a36d6e8921d5",
35 inDir: flag.String("cloud_go_dir", "", "if set, reuse this directory as google-cloud-go@07da7657"),
36 },
37
38 // Used by x/benchmarks; large.
39 "istio": {
40 name: "istio",
41 url: "https://github.com/istio/istio",
42 commit: "1.17.0",
43 inDir: flag.String("istio_dir", "", "if set, reuse this directory as [email protected]"),
44 },
45
46 // Kubernetes is a large repo with many dependencies, and in the past has
47 // been about as large a repo as gopls could handle.
48 "kubernetes": {
49 name: "kubernetes",
50 url: "https://github.com/kubernetes/kubernetes",
51 commit: "v1.24.0",
52 short: true,
53 inDir: flag.String("kubernetes_dir", "", "if set, reuse this directory as [email protected]"),
54 },
55
56 // A large, industrial application.
57 "kuma": {
58 name: "kuma",
59 url: "https://github.com/kumahq/kuma",
60 commit: "2.1.1",
61 inDir: flag.String("kuma_dir", "", "if set, reuse this directory as [email protected]"),
62 },
63
64 // A repo containing a very large package (./dataintegration).
65 "oracle": {
66 name: "oracle",
67 url: "https://github.com/oracle/oci-go-sdk.git",
68 commit: "v65.43.0",
69 short: true,
70 inDir: flag.String("oracle_dir", "", "if set, reuse this directory as oracle/[email protected]"),
71 },
72
73 // x/pkgsite is familiar and represents a common use case (a webserver). It
74 // also has a number of static non-go files and template files.
75 "pkgsite": {
76 name: "pkgsite",
77 url: "https://go.googlesource.com/pkgsite",
78 commit: "81f6f8d4175ad0bf6feaa03543cc433f8b04b19b",
79 short: true,
80 inDir: flag.String("pkgsite_dir", "", "if set, reuse this directory as pkgsite@81f6f8d4"),
81 },
82
83 // A tiny self-contained project.
84 "starlark": {
85 name: "starlark",
86 url: "https://github.com/google/starlark-go",
87 commit: "3f75dec8e4039385901a30981e3703470d77e027",
88 short: true,
89 inDir: flag.String("starlark_dir", "", "if set, reuse this directory as starlark@3f75dec8"),
90 },
91
92 // The current repository, which is medium-small and has very few dependencies.
93 "tools": {
94 name: "tools",
95 url: "https://go.googlesource.com/tools",
96 commit: "gopls/v0.9.0",
97 short: true,
98 inDir: flag.String("tools_dir", "", "if set, reuse this directory as x/[email protected]"),
99 },
100
101 // A repo of similar size to kubernetes, but with substantially more
102 // complex types that led to a serious performance regression (issue #60621).
103 "hashiform": {
104 name: "hashiform",
105 url: "https://github.com/hashicorp/terraform-provider-aws",
106 commit: "ac55de2b1950972d93feaa250d7505d9ed829c7c",
107 inDir: flag.String("hashiform_dir", "", "if set, reuse this directory as hashiform@ac55de2"),
108 },
109 }
110
111 // getRepo gets the requested repo, and skips the test if -short is set and
112 // repo is not configured as a short repo.
113 //
114 // The name may include an optional ".foo" suffix after the repo
115 // identifier. This allows several tests to use the same repo but have
116 // distinct test names and associated file names.
117 func getRepo(tb testing.TB, name string) *repo {
118 tb.Helper()
119 name, _, _ = strings.Cut(name, ".") // remove ".foo" suffix
120 repo := repos[name]
121 if repo == nil {
122 tb.Fatalf("repo %s does not exist", name)
123 }
124 if !repo.short && testing.Short() {
125 tb.Skipf("large repo %s does not run with -short", repo.name)
126 }
127 return repo
128 }
129
130 // A repo represents a working directory for a repository checked out at a
131 // specific commit.
132 //
133 // Repos are used for sharing state across benchmarks that operate on the same
134 // codebase.
135 type repo struct {
136 // static configuration
137 name string // must be unique, used for subdirectory
138 url string // repo url
139 commit string // full commit hash or tag
140 short bool // whether this repo runs with -short
141 inDir *string // if set, use this dir as url@commit, and don't delete
142
143 dirOnce sync.Once
144 dir string // directory containing source code checked out to url@commit
145
146 // shared editor state
147 editorOnce sync.Once
148 editor *fake.Editor
149 sandbox *fake.Sandbox
150 awaiter *Awaiter
151 }
152
153 // reusableDir return a reusable directory for benchmarking, or "".
154 //
155 // If the user specifies a directory, the test will create and populate it
156 // on the first run and re-use it on subsequent runs. Otherwise it will
157 // create, populate, and delete a temporary directory.
158 func (r *repo) reusableDir() string {
159 if r.inDir == nil {
160 return ""
161 }
162 return *r.inDir
163 }
164
165 // getDir returns directory containing repo source code, creating it if
166 // necessary. It is safe for concurrent use.
167 func (r *repo) getDir() string {
168 r.dirOnce.Do(func() {
169 if r.dir = r.reusableDir(); r.dir == "" {
170 r.dir = filepath.Join(getTempDir(), r.name)
171 }
172
173 _, err := os.Stat(r.dir)
174 switch {
175 case os.IsNotExist(err):
176 log.Printf("cloning %s@%s into %s", r.url, r.commit, r.dir)
177 if err := shallowClone(r.dir, r.url, r.commit); err != nil {
178 log.Fatal(err)
179 }
180 case err != nil:
181 log.Fatal(err)
182 default:
183 log.Printf("reusing %s as %s@%s", r.dir, r.url, r.commit)
184 }
185 })
186 return r.dir
187 }
188
189 // sharedEnv returns a shared benchmark environment. It is safe for concurrent
190 // use.
191 //
192 // Every call to sharedEnv uses the same editor and sandbox, as a means to
193 // avoid reinitializing the editor for large repos. Calling repo.Close cleans
194 // up the shared environment.
195 //
196 // Repos in the package-local Repos var are closed at the end of the test main
197 // function.
198 func (r *repo) sharedEnv(tb testing.TB) *Env {
199 r.editorOnce.Do(func() {
200 dir := r.getDir()
201
202 start := time.Now()
203 log.Printf("starting initial workspace load for %s", r.name)
204 ts, err := newGoplsConnector(profileArgs(r.name, false))
205 if err != nil {
206 log.Fatal(err)
207 }
208 r.sandbox, r.editor, r.awaiter, err = connectEditor(dir, fake.EditorConfig{}, ts)
209 if err != nil {
210 log.Fatalf("connecting editor: %v", err)
211 }
212
213 if err := r.awaiter.Await(context.Background(), InitialWorkspaceLoad); err != nil {
214 log.Fatal(err)
215 }
216 log.Printf("initial workspace load (cold) for %s took %v", r.name, time.Since(start))
217 })
218
219 return &Env{
220 TB: tb,
221 Ctx: context.Background(),
222 Editor: r.editor,
223 Sandbox: r.sandbox,
224 Awaiter: r.awaiter,
225 }
226 }
227
228 // newEnv returns a new Env connected to a new gopls process communicating
229 // over stdin/stdout. It is safe for concurrent use.
230 //
231 // It is the caller's responsibility to call Close on the resulting Env when it
232 // is no longer needed.
233 func (r *repo) newEnv(tb testing.TB, config fake.EditorConfig, forOperation string, cpuProfile bool) *Env {
234 dir := r.getDir()
235
236 args := profileArgs(qualifiedName(r.name, forOperation), cpuProfile)
237 ts, err := newGoplsConnector(args)
238 if err != nil {
239 tb.Fatal(err)
240 }
241 sandbox, editor, awaiter, err := connectEditor(dir, config, ts)
242 if err != nil {
243 log.Fatalf("connecting editor: %v", err)
244 }
245
246 return &Env{
247 TB: tb,
248 Ctx: context.Background(),
249 Editor: editor,
250 Sandbox: sandbox,
251 Awaiter: awaiter,
252 }
253 }
254
255 // Close cleans up shared state referenced by the repo.
256 func (r *repo) Close() error {
257 var errBuf bytes.Buffer
258 if r.editor != nil {
259 if err := r.editor.Close(context.Background()); err != nil {
260 fmt.Fprintf(&errBuf, "closing editor: %v", err)
261 }
262 }
263 if r.sandbox != nil {
264 if err := r.sandbox.Close(); err != nil {
265 fmt.Fprintf(&errBuf, "closing sandbox: %v", err)
266 }
267 }
268 if r.dir != "" && r.reusableDir() == "" {
269 if err := os.RemoveAll(r.dir); err != nil {
270 fmt.Fprintf(&errBuf, "cleaning dir: %v", err)
271 }
272 }
273 if errBuf.Len() > 0 {
274 return errors.New(errBuf.String())
275 }
276 return nil
277 }
278
279 // cleanup cleans up state that is shared across benchmark functions.
280 func cleanup() error {
281 var errBuf bytes.Buffer
282 for _, repo := range repos {
283 if err := repo.Close(); err != nil {
284 fmt.Fprintf(&errBuf, "closing %q: %v", repo.name, err)
285 }
286 }
287 if tempDir != "" {
288 if err := os.RemoveAll(tempDir); err != nil {
289 fmt.Fprintf(&errBuf, "cleaning tempDir: %v", err)
290 }
291 }
292 if errBuf.Len() > 0 {
293 return errors.New(errBuf.String())
294 }
295 return nil
296 }