File: imports_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (89 lines) | stat: -rw-r--r-- 2,929 bytes parent folder | download | duplicates (2)
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
// Copyright 2024 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 (
	"context"
	"flag"
	"testing"
	"time"

	"golang.org/x/tools/gopls/internal/protocol"
	"golang.org/x/tools/gopls/internal/protocol/command"
	. "golang.org/x/tools/gopls/internal/test/integration"
	"golang.org/x/tools/gopls/internal/test/integration/fake"
)

var gopath = flag.String("gopath", "", "if set, run goimports scan with this GOPATH value")

func BenchmarkInitialGoimportsScan(b *testing.B) {
	if *gopath == "" {
		// This test doesn't make much sense with a tiny module cache.
		// For now, don't bother trying to construct a huge cache, since it likely
		// wouldn't work well on the perf builder. Instead, this benchmark only
		// runs with a pre-existing GOPATH.
		b.Skip("imports scan requires an explicit GOPATH to be set with -gopath")
	}

	repo := getRepo(b, "tools") // since this a test of module cache scanning, any repo will do

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		func() {
			// Unfortunately we (intentionally) don't support resetting the module
			// cache scan state, so in order to have an accurate benchmark we must
			// effectively restart gopls on every iteration.
			//
			// Warning: this can cause this benchmark to run quite slowly if the
			// observed time (when the timer is running) is a tiny fraction of the
			// actual time.
			b.StopTimer()
			config := fake.EditorConfig{
				Env: map[string]string{"GOPATH": *gopath},
			}
			env := repo.newEnv(b, config, "imports", false)
			defer env.Close()
			env.Await(InitialWorkspaceLoad)

			// Create a buffer with a dangling selctor where the receiver is a single
			// character ('a') that matches a large fraction of the module cache.
			env.CreateBuffer("internal/lsp/cache/temp.go", `
// This is a temp file to exercise goimports scan of the module cache.
package cache

func _() {
	_ = a.B // a dangling selector causes goimports to scan many packages
}
`)
			env.AfterChange()

			// Force a scan of the imports cache, so that the goimports algorithm
			// observes all directories.
			env.ExecuteCommand(&protocol.ExecuteCommandParams{
				Command: command.ScanImports.String(),
			}, nil)

			if stopAndRecord := startProfileIfSupported(b, env, "importsscan"); stopAndRecord != nil {
				defer stopAndRecord()
			}

			b.StartTimer()
			if false {
				// golang/go#67923: testing resuming imports scanning after a
				// cancellation.
				//
				// Cancelling and then resuming the scan should take around the same
				// amount of time.
				ctx, cancel := context.WithTimeout(env.Ctx, 50*time.Millisecond)
				defer cancel()
				if err := env.Editor.OrganizeImports(ctx, "internal/lsp/cache/temp.go"); err != nil {
					b.Logf("organize imports failed: %v", err)
				}
			}
			env.OrganizeImports("internal/lsp/cache/temp.go")
		}()
	}
}