Debian Codegraph @

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

raw symbols SCIP
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 //
19 // The OpenFiles variant of this test is more realistic: who cares if gopls is
20 // initialized if you can't use it? However, this test is left as is to
21 // preserve the validity of historical data, and to represent the baseline
22 // performance of validating the workspace state.
23 func BenchmarkInitialWorkspaceLoad(b *testing.B) {
24 repoNames := []string{
25 "google-cloud-go",
26 "istio",
27 "kubernetes",
28 "kuma",
29 "oracle",
30 "pkgsite",
31 "starlark",
32 "tools",
33 "hashiform",
34 }
35 for _, repoName := range repoNames {
36 b.Run(repoName, func(b *testing.B) {
37 repo := getRepo(b, repoName)
38 // get the (initialized) shared env to ensure the cache is warm.
39 // Reuse its GOPATH so that we get cache hits for things in the module
40 // cache.
41 sharedEnv := repo.sharedEnv(b)
42 b.ResetTimer()
43
44 for b.Loop() {
45 doIWL(b, sharedEnv.Sandbox.GOPATH(), repo, nil)
46 }
47 })
48 }
49 }
50
51 // BenchmarkInitialWorkspaceLoadOpenFiles benchmarks the initial workspace load
52 // after opening one or more files.
53 //
54 // It may differ significantly from [BenchmarkInitialWorkspaceLoad], since
55 // there is various active state that is proportional to the number of open
56 // files.
57 func BenchmarkInitialWorkspaceLoadOpenFiles(b *testing.B) {
58 for _, t := range didChangeTests {
59 b.Run(t.repo, func(b *testing.B) {
60 repo := getRepo(b, t.repo)
61 sharedEnv := repo.sharedEnv(b)
62 b.ResetTimer()
63
64 for b.Loop() {
65 doIWL(b, sharedEnv.Sandbox.GOPATH(), repo, []string{t.file})
66 }
67 })
68 }
69 }
70
71 func doIWL(b *testing.B, gopath string, repo *repo, openfiles []string) {
72 // Exclude the time to set up the env from the benchmark time, as this may
73 // involve installing gopls and/or checking out the repo dir.
74 b.StopTimer()
75 config := fake.EditorConfig{Env: map[string]string{"GOPATH": gopath}}
76 env := repo.newEnv(b, config, "iwl", true)
77 defer env.Close()
78 b.StartTimer()
79
80 // TODO(rfindley): not awaiting the IWL here leads to much more volatile
81 // results. Investigate.
82 env.Await(InitialWorkspaceLoad)
83
84 for _, f := range openfiles {
85 env.OpenFile(f)
86 }
87
88 env.AfterChange()
89
90 if env.Editor.HasCommand(command.MemStats) {
91 b.StopTimer()
92 params := &protocol.ExecuteCommandParams{
93 Command: command.MemStats.String(),
94 }
95 var memstats command.MemStatsResult
96 env.ExecuteCommand(params, &memstats)
97 b.ReportMetric(float64(memstats.HeapAlloc), "alloc_bytes")
98 b.ReportMetric(float64(memstats.HeapInUse), "in_use_bytes")
99 b.ReportMetric(float64(memstats.TotalAlloc), "total_alloc_bytes")
100 b.StartTimer()
101 }
102 }