Debian Codegraph @

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

raw symbols SCIP
1 // Copyright 2024 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 "context"
9 "flag"
10 "testing"
11 "time"
12
13 "golang.org/x/tools/gopls/internal/protocol"
14 "golang.org/x/tools/gopls/internal/protocol/command"
15 . "golang.org/x/tools/gopls/internal/test/integration"
16 "golang.org/x/tools/gopls/internal/test/integration/fake"
17 )
18
19 var gopath = flag.String("gopath", "", "if set, run goimports scan with this GOPATH value")
20
21 func BenchmarkInitialGoimportsScan(b *testing.B) {
22 if *gopath == "" {
23 // This test doesn't make much sense with a tiny module cache.
24 // For now, don't bother trying to construct a huge cache, since it likely
25 // wouldn't work well on the perf builder. Instead, this benchmark only
26 // runs with a pre-existing GOPATH.
27 b.Skip("imports scan requires an explicit GOPATH to be set with -gopath")
28 }
29
30 repo := getRepo(b, "tools") // since this a test of module cache scanning, any repo will do
31
32 for b.Loop() {
33 func() {
34 // Unfortunately we (intentionally) don't support resetting the module
35 // cache scan state, so in order to have an accurate benchmark we must
36 // effectively restart gopls on every iteration.
37 //
38 // Warning: this can cause this benchmark to run quite slowly if the
39 // observed time (when the timer is running) is a tiny fraction of the
40 // actual time.
41 b.StopTimer()
42 config := fake.EditorConfig{
43 Env: map[string]string{"GOPATH": *gopath},
44 }
45 env := repo.newEnv(b, config, "imports", false)
46 defer env.Close()
47 env.Await(InitialWorkspaceLoad)
48
49 // Create a buffer with a dangling selector where the receiver is a single
50 // character ('a') that matches a large fraction of the module cache.
51 env.CreateBuffer("internal/lsp/cache/temp.go", `
52 // This is a temp file to exercise goimports scan of the module cache.
53 package cache
54
55 func _() {
56 _ = a.B // a dangling selector causes goimports to scan many packages
57 }
58 `)
59 env.AfterChange()
60
61 // Force a scan of the imports cache, so that the goimports algorithm
62 // observes all directories.
63 env.ExecuteCommand(&protocol.ExecuteCommandParams{
64 Command: command.ScanImports.String(),
65 }, nil)
66
67 if stopAndRecord := startProfileIfSupported(b, env, "importsscan"); stopAndRecord != nil {
68 defer stopAndRecord()
69 }
70
71 b.StartTimer()
72 if false {
73 // golang/go#67923: testing resuming imports scanning after a
74 // cancellation.
75 //
76 // Cancelling and then resuming the scan should take around the same
77 // amount of time.
78 ctx, cancel := context.WithTimeout(env.Ctx, 50*time.Millisecond)
79 defer cancel()
80 if err := env.Editor.OrganizeImports(ctx, "internal/lsp/cache/temp.go"); err != nil {
81 b.Logf("organize imports failed: %v", err)
82 }
83 }
84 env.OrganizeImports("internal/lsp/cache/temp.go")
85 }()
86 }
87 }