Debian Codegraph @

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

raw symbols SCIP
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 "encoding/json"
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 )
14
15 func BenchmarkPackagesCommand(b *testing.B) {
16 // By convention, x/benchmarks runs the gopls benchmarks with -short, so that
17 // we can use this flag to filter out benchmarks that should not be run by
18 // the perf builder.
19 //
20 // In this case, the benchmark must be skipped because the current baseline
21 // ([email protected]) lacks the gopls.package command.
22 if testing.Short() {
23 b.Skip("not supported by the benchmark dashboard baseline")
24 }
25
26 tests := []struct {
27 repo string
28 files []string
29 recurse bool
30 }{
31 {"tools", []string{"internal/lsp/debounce_test.go"}, false},
32 }
33 for _, test := range tests {
34 b.Run(test.repo, func(b *testing.B) {
35 args := command.PackagesArgs{
36 Mode: command.NeedTests,
37 }
38
39 env := getRepo(b, test.repo).sharedEnv(b)
40 for _, file := range test.files {
41 env.OpenFile(file)
42 defer closeBuffer(b, env, file)
43 args.Files = append(args.Files, env.Editor.DocumentURI(file))
44 }
45 env.AfterChange()
46
47 result := executePackagesCmd(b, env, args) // pre-warm
48
49 // sanity check JSON {en,de}coding
50 var pkgs command.PackagesResult
51 data, err := json.Marshal(result)
52 if err != nil {
53 b.Fatal(err)
54 }
55 err = json.Unmarshal(data, &pkgs)
56 if err != nil {
57 b.Fatal(err)
58 }
59 var haveTest bool
60 for _, pkg := range pkgs.Packages {
61 for _, file := range pkg.TestFiles {
62 if len(file.Tests) > 0 {
63 haveTest = true
64 break
65 }
66 }
67 }
68 if !haveTest {
69 b.Fatalf("Expected tests")
70 }
71
72 b.ResetTimer()
73
74 if stopAndRecord := startProfileIfSupported(b, env, qualifiedName(test.repo, "packages")); stopAndRecord != nil {
75 defer stopAndRecord()
76 }
77
78 for b.Loop() {
79 executePackagesCmd(b, env, args)
80 }
81 })
82 }
83 }
84
85 func executePackagesCmd(t testing.TB, env *integration.Env, args command.PackagesArgs) any {
86 t.Helper()
87 cmd := command.NewPackagesCommand("Packages", args)
88 result, err := env.Editor.Server.ExecuteCommand(env.Ctx, &protocol.ExecuteCommandParams{
89 Command: command.Packages.String(),
90 Arguments: cmd.Arguments,
91 })
92 if err != nil {
93 t.Fatal(err)
94 }
95 return result
96 }