.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.6.9 / internal / regtest / bench / bench_test.go
1 // Copyright 2020 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         "flag"
9         "fmt"
10         "testing"
11
12         . "golang.org/x/tools/gopls/internal/regtest"
13
14         "golang.org/x/tools/internal/lsp/protocol"
15 )
16
17 func TestMain(m *testing.M) {
18         Main(m)
19 }
20
21 func printBenchmarkResults(result testing.BenchmarkResult) {
22         fmt.Println("Benchmark Statistics:")
23         fmt.Println(result.String())
24         fmt.Println(result.MemString())
25 }
26
27 var iwlOptions struct {
28         workdir string
29 }
30
31 func init() {
32         flag.StringVar(&iwlOptions.workdir, "iwl_workdir", "", "if set, run IWL benchmark in this directory")
33 }
34
35 func TestBenchmarkIWL(t *testing.T) {
36         if iwlOptions.workdir == "" {
37                 t.Skip("-iwl_workdir not configured")
38         }
39
40         opts := stressTestOptions(iwlOptions.workdir)
41         // Don't skip hooks, so that we can wait for IWL.
42         opts = append(opts, SkipHooks(false))
43
44         results := testing.Benchmark(func(b *testing.B) {
45                 for i := 0; i < b.N; i++ {
46                         WithOptions(opts...).Run(t, "", func(t *testing.T, env *Env) {})
47                 }
48         })
49
50         printBenchmarkResults(results)
51 }
52
53 var symbolOptions struct {
54         workdir, query, matcher, style string
55         printResults                   bool
56 }
57
58 func init() {
59         flag.StringVar(&symbolOptions.workdir, "symbol_workdir", "", "if set, run symbol benchmark in this directory")
60         flag.StringVar(&symbolOptions.query, "symbol_query", "test", "symbol query to use in benchmark")
61         flag.StringVar(&symbolOptions.matcher, "symbol_matcher", "", "symbol matcher to use in benchmark")
62         flag.StringVar(&symbolOptions.style, "symbol_style", "", "symbol style to use in benchmark")
63         flag.BoolVar(&symbolOptions.printResults, "symbol_print_results", false, "whether to print symbol query results")
64 }
65
66 func TestBenchmarkSymbols(t *testing.T) {
67         if symbolOptions.workdir == "" {
68                 t.Skip("-symbol_workdir not configured")
69         }
70
71         opts := stressTestOptions(symbolOptions.workdir)
72         conf := EditorConfig{}
73         if symbolOptions.matcher != "" {
74                 conf.SymbolMatcher = &symbolOptions.matcher
75         }
76         if symbolOptions.style != "" {
77                 conf.SymbolStyle = &symbolOptions.style
78         }
79         opts = append(opts, conf)
80
81         WithOptions(opts...).Run(t, "", func(t *testing.T, env *Env) {
82                 // We can't Await in this test, since we have disabled hooks. Instead, run
83                 // one symbol request to completion to ensure all necessary cache entries
84                 // are populated.
85                 symbols, err := env.Editor.Server.Symbol(env.Ctx, &protocol.WorkspaceSymbolParams{
86                         Query: symbolOptions.query,
87                 })
88                 if err != nil {
89                         t.Fatal(err)
90                 }
91
92                 if symbolOptions.printResults {
93                         fmt.Println("Results:")
94                         for i := 0; i < len(symbols); i++ {
95                                 fmt.Printf("\t%d. %s (%s)\n", i, symbols[i].Name, symbols[i].ContainerName)
96                         }
97                 }
98
99                 results := testing.Benchmark(func(b *testing.B) {
100                         for i := 0; i < b.N; i++ {
101                                 if _, err := env.Editor.Server.Symbol(env.Ctx, &protocol.WorkspaceSymbolParams{
102                                         Query: symbolOptions.query,
103                                 }); err != nil {
104                                         t.Fatal(err)
105                                 }
106                         }
107                 })
108                 printBenchmarkResults(results)
109         })
110 }