Debian Codegraph @

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

raw symbols SCIP
1 // Copyright 2025 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 "fmt"
10 "go/token"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "strings"
15 "testing"
16
17 . "golang.org/x/tools/gopls/internal/test/integration"
18 "golang.org/x/tools/gopls/internal/test/integration/fake"
19 "golang.org/x/tools/internal/modindex"
20 )
21
22 // This code is designed to show the comparative effectiveness
23 // of the current and the old ways of doing unimported completions.
24 // It should only be run by hand when there is an existing module
25 // cache and module cache index. It compares the two algorithms
26 // for various values of the completion budget. "gopls" is the
27 // current algorithm, "goimports" is the old one. [On my workstation
28 // the old algorithms occasionally fails to find the completion,
29 // and when it does it is more than 10 times slower.]
30 func BenchmarkLocalModcache(b *testing.B) {
31 b.Skip("only run by hand")
32 budgets := []string{"0s", "100ms", "200ms", "500ms", "1s", "5s"}
33 sources := []string{"gopls", "goimports"}
34 for _, budget := range budgets {
35 b.Run(fmt.Sprintf("budget=%s", budget), func(b *testing.B) {
36 for _, source := range sources {
37 b.Run(fmt.Sprintf("source=%s", source), func(b *testing.B) {
38 runModcacheCompletion(b, budget, source)
39 })
40 }
41 })
42 }
43 }
44
45 func runModcacheCompletion(b *testing.B, budget, source string) {
46 // First set up the program to be edited
47 gomod := `
48 module mod.com
49
50 go 1.21
51 `
52 pat := `
53 package main
54 var _ = %s.%s
55 `
56 pkg, name, modcache := findSym(b)
57 name, _, _ = strings.Cut(name, " ")
58 mainfile := fmt.Sprintf(pat, pkg, name)
59 // Second, create the Env and start gopls
60 dir := getTempDir()
61 if err := os.Mkdir(dir, 0750); err != nil {
62 if !os.IsExist(err) {
63 b.Fatal(err)
64 }
65 }
66 defer os.RemoveAll(dir) // is this right? needed?
67 if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte(gomod), 0644); err != nil {
68 b.Fatal(err)
69 }
70 if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte(mainfile), 0644); err != nil {
71 b.Fatal(err)
72 }
73 ts, err := newGoplsConnector(nil)
74 if err != nil {
75 b.Fatal(err)
76 }
77 // PJW: put better EditorConfig here
78 envvars := map[string]string{
79 "GOMODCACHE": modcache,
80 //"GOPATH": sandbox.GOPATH(), // do we need a GOPATH?
81 }
82 fc := fake.EditorConfig{
83 Env: envvars,
84 Settings: map[string]any{
85 "completeUnimported": true,
86 "completionBudget": budget, // "0s", "100ms"
87 "importsSource": source, // "gopls" or "goimports"
88 },
89 }
90 sandbox, editor, awaiter, err := connectEditor(dir, fc, ts)
91 if err != nil {
92 b.Fatal(err)
93 }
94 defer sandbox.Close()
95 defer editor.Close(context.Background())
96 if err := awaiter.Await(context.Background(), InitialWorkspaceLoad); err != nil {
97 b.Fatal(err)
98 }
99 env := &Env{
100 TB: b,
101 Ctx: context.Background(),
102 Editor: editor,
103 Sandbox: sandbox,
104 Awaiter: awaiter,
105 }
106 // Check that completion works as expected
107 env.CreateBuffer("main.go", mainfile)
108 env.AfterChange()
109 loc := env.RegexpSearch("main.go", name)
110 completions := env.Completion(loc)
111 if len(completions.Items) == 0 {
112 b.Error("no completions")
113 }
114
115 // run benchmark
116 for b.Loop() {
117 loc := env.RegexpSearch("main.go", name)
118 env.Completion(loc)
119 }
120 }
121
122 // find some symbol in the module cache
123 func findSym(t testing.TB) (pkg, name, gomodcache string) {
124 initForTest(t) // set modindex.IndexDir
125 cmd := exec.Command("go", "env", "GOMODCACHE")
126 out, err := cmd.Output()
127 if err != nil {
128 t.Fatal(err)
129 }
130 modcache := strings.TrimSpace(string(out))
131 ix, err := modindex.Read(modcache)
132 if err != nil {
133 t.Fatal(err)
134 }
135 if ix == nil {
136 t.Fatal("nil index")
137 }
138 // find some symbol in the module cache
139 nth := 100 // or something
140 for _, e := range ix.Entries {
141 if token.IsExported(e.PkgName) || strings.HasPrefix(e.PkgName, "_") {
142 continue // weird stuff in module cache
143 }
144
145 for _, nm := range e.Names {
146 nth--
147 if nth == 0 {
148 return e.PkgName, nm, modcache
149 }
150 }
151 }
152 t.Fatalf("index doesn't have enough usable names, need another %d", nth)
153 return "", "", modcache
154 }
155
156 // Set IndexDir, avoiding the special case for tests,
157 func initForTest(t testing.TB) {
158 dir, err := os.UserCacheDir()
159 if err != nil {
160 t.Fatalf("os.UserCacheDir: %v", err)
161 }
162 dir = filepath.Join(dir, "goimports")
163 modindex.IndexDir = dir
164 }