.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.6.9 / internal / regtest / bench / stress_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         "context"
9         "flag"
10         "fmt"
11         "testing"
12         "time"
13
14         . "golang.org/x/tools/gopls/internal/regtest"
15 )
16
17 // Pilosa is a repository that has historically caused significant memory
18 // problems for Gopls. We use it for a simple stress test that types
19 // arbitrarily in a file with lots of dependents.
20
21 var pilosaPath = flag.String("pilosa_path", "", "Path to a directory containing "+
22         "github.com/pilosa/pilosa, for stress testing. Do not set this unless you "+
23         "know what you're doing!")
24
25 func stressTestOptions(dir string) []RunOption {
26         return []RunOption{
27                 // Run in an existing directory, since we're trying to simulate known cases
28                 // that cause gopls memory problems.
29                 InExistingDir(dir),
30
31                 // Enable live debugging.
32                 DebugAddress(":8087"),
33
34                 // Skip logs as they buffer up memory unnaturally.
35                 SkipLogs(),
36                 // Similarly to logs: disable hooks so that they don't affect performance.
37                 SkipHooks(true),
38                 // The Debug server only makes sense if running in singleton mode.
39                 Modes(Singleton),
40                 // Set a generous timeout. Individual tests should control their own
41                 // graceful termination.
42                 Timeout(20 * time.Minute),
43
44                 // Use the actual proxy, since we want our builds to succeed.
45                 GOPROXY("https://proxy.golang.org"),
46         }
47 }
48
49 func TestPilosaStress(t *testing.T) {
50         if *pilosaPath == "" {
51                 t.Skip("-pilosa_path not configured")
52         }
53         opts := stressTestOptions(*pilosaPath)
54
55         WithOptions(opts...).Run(t, "", func(t *testing.T, env *Env) {
56                 files := []string{
57                         "cmd.go",
58                         "internal/private.pb.go",
59                         "roaring/roaring.go",
60                         "roaring/roaring_internal_test.go",
61                         "server/handler_test.go",
62                 }
63                 for _, file := range files {
64                         env.OpenFile(file)
65                 }
66                 ctx, cancel := context.WithTimeout(env.Ctx, 10*time.Minute)
67                 defer cancel()
68
69                 i := 1
70                 // MagicNumber is an identifier that occurs in roaring.go. Just change it
71                 // arbitrarily.
72                 env.RegexpReplace("roaring/roaring.go", "MagicNumber", fmt.Sprintf("MagicNumber%d", 1))
73                 for {
74                         select {
75                         case <-ctx.Done():
76                                 return
77                         default:
78                         }
79                         env.RegexpReplace("roaring/roaring.go", fmt.Sprintf("MagicNumber%d", i), fmt.Sprintf("MagicNumber%d", i+1))
80                         time.Sleep(20 * time.Millisecond)
81                         i++
82                 }
83         })
84 }