File: reload_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 (52 lines) | stat: -rw-r--r-- 1,694 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
// Copyright 2023 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 (
	"testing"

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

// BenchmarkReload benchmarks reloading a file metadata after a change to an import.
//
// This ensures we are able to diagnose a changed file without reloading all
// invalidated packages. See also golang/go#61344
func BenchmarkReload(b *testing.B) {
	// TODO(rfindley): add more tests, make this test table-driven
	const (
		repo = "kubernetes"
		// pkg/util/hash is transitively imported by a large number of packages.
		// We should not need to reload those packages to get a diagnostic.
		file = "pkg/util/hash/hash.go"
	)
	b.Run(repo, func(b *testing.B) {
		env := getRepo(b, repo).sharedEnv(b)

		env.OpenFile(file)
		defer closeBuffer(b, env, file)

		env.AfterChange()

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

		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			// Change the "hash" import. This may result in cache hits, but that's
			// OK: the goal is to ensure that we don't reload more than just the
			// current package.
			env.RegexpReplace(file, `"hash"`, `"hashx"`)
			// Note: don't use env.AfterChange() here: we only want to await the
			// first diagnostic.
			//
			// Awaiting a full diagnosis would await diagnosing everything, which
			// would require reloading everything.
			env.Await(Diagnostics(ForFile(file)))
			env.RegexpReplace(file, `"hashx"`, `"hash"`)
			env.Await(NoDiagnostics(ForFile(file)))
		}
	})
}