golang.org/x/tools/gopls@v0.15.3/internal/test/integration/bench/reload_test.go (about)

     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  package bench
     5  
     6  import (
     7  	"testing"
     8  
     9  	. "golang.org/x/tools/gopls/internal/test/integration"
    10  )
    11  
    12  // BenchmarkReload benchmarks reloading a file metadata after a change to an import.
    13  //
    14  // This ensures we are able to diagnose a changed file without reloading all
    15  // invalidated packages. See also golang/go#61344
    16  func BenchmarkReload(b *testing.B) {
    17  	// TODO(rfindley): add more tests, make this test table-driven
    18  	const (
    19  		repo = "kubernetes"
    20  		// pkg/util/hash is transitively imported by a large number of packages.
    21  		// We should not need to reload those packages to get a diagnostic.
    22  		file = "pkg/util/hash/hash.go"
    23  	)
    24  	b.Run(repo, func(b *testing.B) {
    25  		env := getRepo(b, repo).sharedEnv(b)
    26  
    27  		env.OpenFile(file)
    28  		defer closeBuffer(b, env, file)
    29  
    30  		env.AfterChange()
    31  
    32  		if stopAndRecord := startProfileIfSupported(b, env, qualifiedName(repo, "reload")); stopAndRecord != nil {
    33  			defer stopAndRecord()
    34  		}
    35  
    36  		b.ResetTimer()
    37  		for i := 0; i < b.N; i++ {
    38  			// Change the "hash" import. This may result in cache hits, but that's
    39  			// OK: the goal is to ensure that we don't reload more than just the
    40  			// current package.
    41  			env.RegexpReplace(file, `"hash"`, `"hashx"`)
    42  			// Note: don't use env.AfterChange() here: we only want to await the
    43  			// first diagnostic.
    44  			//
    45  			// Awaiting a full diagnosis would await diagnosing everything, which
    46  			// would require reloading everything.
    47  			env.Await(Diagnostics(ForFile(file)))
    48  			env.RegexpReplace(file, `"hashx"`, `"hash"`)
    49  			env.Await(NoDiagnostics(ForFile(file)))
    50  		}
    51  	})
    52  }