blob: 5ad93589c8712695e7be6355b68ff0a2be9a7856 [file] [log] [blame]
Rob Findley17fd2f22020-08-26 09:30:25 -04001// 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
5package regtest
6
7import (
8 "flag"
9 "fmt"
10 "testing"
11
Rob Findley17fd2f22020-08-26 09:30:25 -040012 "golang.org/x/tools/internal/lsp/protocol"
13)
14
Danish Dua06f3a462020-09-22 18:04:19 -040015func printBenchmarkResults(result testing.BenchmarkResult) {
16 fmt.Println("Benchmark Statistics:")
17 fmt.Println(result.String())
18 fmt.Println(result.MemString())
19}
20
21var iwlOptions struct {
Rob Findley06cc1d02020-08-26 12:54:06 -040022 workdir string
Danish Dua06f3a462020-09-22 18:04:19 -040023}
Rob Findley06cc1d02020-08-26 12:54:06 -040024
25func init() {
Danish Dua06f3a462020-09-22 18:04:19 -040026 flag.StringVar(&iwlOptions.workdir, "iwl_workdir", "", "if set, run IWL benchmark in this directory")
Rob Findley06cc1d02020-08-26 12:54:06 -040027}
28
29func TestBenchmarkIWL(t *testing.T) {
Danish Dua06f3a462020-09-22 18:04:19 -040030 if iwlOptions.workdir == "" {
Rob Findley06cc1d02020-08-26 12:54:06 -040031 t.Skip("-iwl_workdir not configured")
32 }
Danish Dua06f3a462020-09-22 18:04:19 -040033
34 opts := stressTestOptions(iwlOptions.workdir)
Rob Findley06cc1d02020-08-26 12:54:06 -040035 // Don't skip hooks, so that we can wait for IWL.
36 opts = append(opts, SkipHooks(false))
Danish Dua06f3a462020-09-22 18:04:19 -040037
38 results := testing.Benchmark(func(b *testing.B) {
Rob Findley06cc1d02020-08-26 12:54:06 -040039 for i := 0; i < b.N; i++ {
Rob Findleyd7fc70a2020-09-28 16:19:01 -040040 withOptions(opts...).run(t, "", func(t *testing.T, env *Env) {})
Rob Findley06cc1d02020-08-26 12:54:06 -040041 }
42 })
Danish Dua06f3a462020-09-22 18:04:19 -040043
44 printBenchmarkResults(results)
Rob Findley06cc1d02020-08-26 12:54:06 -040045}
46
Danish Dua06f3a462020-09-22 18:04:19 -040047var symbolOptions struct {
Rob Findley17fd2f22020-08-26 09:30:25 -040048 workdir, query, matcher, style string
49 printResults bool
Danish Dua06f3a462020-09-22 18:04:19 -040050}
Rob Findley17fd2f22020-08-26 09:30:25 -040051
52func init() {
Danish Dua06f3a462020-09-22 18:04:19 -040053 flag.StringVar(&symbolOptions.workdir, "symbol_workdir", "", "if set, run symbol benchmark in this directory")
54 flag.StringVar(&symbolOptions.query, "symbol_query", "test", "symbol query to use in benchmark")
55 flag.StringVar(&symbolOptions.matcher, "symbol_matcher", "", "symbol matcher to use in benchmark")
56 flag.StringVar(&symbolOptions.style, "symbol_style", "", "symbol style to use in benchmark")
57 flag.BoolVar(&symbolOptions.printResults, "symbol_print_results", false, "whether to print symbol query results")
Rob Findley17fd2f22020-08-26 09:30:25 -040058}
59
60func TestBenchmarkSymbols(t *testing.T) {
Danish Dua06f3a462020-09-22 18:04:19 -040061 if symbolOptions.workdir == "" {
Rob Findley17fd2f22020-08-26 09:30:25 -040062 t.Skip("-symbol_workdir not configured")
63 }
Danish Dua06f3a462020-09-22 18:04:19 -040064
65 opts := stressTestOptions(symbolOptions.workdir)
Rob Findley797bd0f2020-09-15 22:04:47 -040066 conf := EditorConfig{}
Danish Dua06f3a462020-09-22 18:04:19 -040067 if symbolOptions.matcher != "" {
68 conf.SymbolMatcher = &symbolOptions.matcher
Rob Findley17fd2f22020-08-26 09:30:25 -040069 }
Danish Dua06f3a462020-09-22 18:04:19 -040070 if symbolOptions.style != "" {
71 conf.SymbolStyle = &symbolOptions.style
Rob Findley17fd2f22020-08-26 09:30:25 -040072 }
Rob Findley797bd0f2020-09-15 22:04:47 -040073 opts = append(opts, conf)
Danish Dua06f3a462020-09-22 18:04:19 -040074
Rob Findley17fd2f22020-08-26 09:30:25 -040075 withOptions(opts...).run(t, "", func(t *testing.T, env *Env) {
76 // We can't Await in this test, since we have disabled hooks. Instead, run
77 // one symbol request to completion to ensure all necessary cache entries
78 // are populated.
Danish Dua06f3a462020-09-22 18:04:19 -040079 symbols, err := env.Editor.Server.Symbol(env.Ctx, &protocol.WorkspaceSymbolParams{
80 Query: symbolOptions.query,
Rob Findley17fd2f22020-08-26 09:30:25 -040081 })
82 if err != nil {
83 t.Fatal(err)
84 }
Danish Dua06f3a462020-09-22 18:04:19 -040085
86 if symbolOptions.printResults {
Rob Findley17fd2f22020-08-26 09:30:25 -040087 fmt.Println("Results:")
Danish Dua06f3a462020-09-22 18:04:19 -040088 for i := 0; i < len(symbols); i++ {
89 fmt.Printf("\t%d. %s (%s)\n", i, symbols[i].Name, symbols[i].ContainerName)
Rob Findley17fd2f22020-08-26 09:30:25 -040090 }
91 }
Danish Dua06f3a462020-09-22 18:04:19 -040092
93 results := testing.Benchmark(func(b *testing.B) {
Rob Findley17fd2f22020-08-26 09:30:25 -040094 for i := 0; i < b.N; i++ {
95 if _, err := env.Editor.Server.Symbol(env.Ctx, &protocol.WorkspaceSymbolParams{
Danish Dua06f3a462020-09-22 18:04:19 -040096 Query: symbolOptions.query,
Rob Findley17fd2f22020-08-26 09:30:25 -040097 }); err != nil {
98 t.Fatal(err)
99 }
100 }
101 })
Danish Dua06f3a462020-09-22 18:04:19 -0400102 printBenchmarkResults(results)
Rob Findley17fd2f22020-08-26 09:30:25 -0400103 })
104}