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

     1  // Copyright 2022 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  	"testing"
     9  
    10  	"golang.org/x/tools/gopls/internal/protocol"
    11  	"golang.org/x/tools/gopls/internal/protocol/command"
    12  	. "golang.org/x/tools/gopls/internal/test/integration"
    13  	"golang.org/x/tools/gopls/internal/test/integration/fake"
    14  )
    15  
    16  // BenchmarkInitialWorkspaceLoad benchmarks the initial workspace load time for
    17  // a new editing session.
    18  func BenchmarkInitialWorkspaceLoad(b *testing.B) {
    19  	repoNames := []string{
    20  		"google-cloud-go",
    21  		"istio",
    22  		"kubernetes",
    23  		"kuma",
    24  		"oracle",
    25  		"pkgsite",
    26  		"starlark",
    27  		"tools",
    28  		"hashiform",
    29  	}
    30  	for _, repoName := range repoNames {
    31  		b.Run(repoName, func(b *testing.B) {
    32  			repo := getRepo(b, repoName)
    33  			// get the (initialized) shared env to ensure the cache is warm.
    34  			// Reuse its GOPATH so that we get cache hits for things in the module
    35  			// cache.
    36  			sharedEnv := repo.sharedEnv(b)
    37  			b.ResetTimer()
    38  
    39  			for i := 0; i < b.N; i++ {
    40  				doIWL(b, sharedEnv.Sandbox.GOPATH(), repo)
    41  			}
    42  		})
    43  	}
    44  }
    45  
    46  func doIWL(b *testing.B, gopath string, repo *repo) {
    47  	// Exclude the time to set up the env from the benchmark time, as this may
    48  	// involve installing gopls and/or checking out the repo dir.
    49  	b.StopTimer()
    50  	config := fake.EditorConfig{Env: map[string]string{"GOPATH": gopath}}
    51  	env := repo.newEnv(b, config, "iwl", true)
    52  	defer env.Close()
    53  	b.StartTimer()
    54  
    55  	// Note: in the future, we may need to open a file in order to cause gopls to
    56  	// start loading the workspace.
    57  
    58  	env.Await(InitialWorkspaceLoad)
    59  
    60  	if env.Editor.HasCommand(command.MemStats.ID()) {
    61  		b.StopTimer()
    62  		params := &protocol.ExecuteCommandParams{
    63  			Command: command.MemStats.ID(),
    64  		}
    65  		var memstats command.MemStatsResult
    66  		env.ExecuteCommand(params, &memstats)
    67  		b.ReportMetric(float64(memstats.HeapAlloc), "alloc_bytes")
    68  		b.ReportMetric(float64(memstats.HeapInUse), "in_use_bytes")
    69  		b.ReportMetric(float64(memstats.TotalAlloc), "total_alloc_bytes")
    70  		b.StartTimer()
    71  	}
    72  }