Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / internal / event / bench_test.go
1 package event_test
2
3 import (
4         "context"
5         "io/ioutil"
6         "log"
7         "testing"
8
9         "golang.org/x/tools/internal/event"
10         "golang.org/x/tools/internal/event/core"
11         "golang.org/x/tools/internal/event/export"
12         "golang.org/x/tools/internal/event/keys"
13         "golang.org/x/tools/internal/event/label"
14 )
15
16 type Hooks struct {
17         A func(ctx context.Context, a int) (context.Context, func())
18         B func(ctx context.Context, b string) (context.Context, func())
19 }
20
21 var (
22         aValue  = keys.NewInt("a", "")
23         bValue  = keys.NewString("b", "")
24         aCount  = keys.NewInt64("aCount", "Count of time A is called.")
25         aStat   = keys.NewInt("aValue", "A value.")
26         bCount  = keys.NewInt64("B", "Count of time B is called.")
27         bLength = keys.NewInt("BLen", "B length.")
28
29         Baseline = Hooks{
30                 A: func(ctx context.Context, a int) (context.Context, func()) {
31                         return ctx, func() {}
32                 },
33                 B: func(ctx context.Context, b string) (context.Context, func()) {
34                         return ctx, func() {}
35                 },
36         }
37
38         StdLog = Hooks{
39                 A: func(ctx context.Context, a int) (context.Context, func()) {
40                         log.Printf("A where a=%d", a)
41                         return ctx, func() {}
42                 },
43                 B: func(ctx context.Context, b string) (context.Context, func()) {
44                         log.Printf("B where b=%q", b)
45                         return ctx, func() {}
46                 },
47         }
48
49         Log = Hooks{
50                 A: func(ctx context.Context, a int) (context.Context, func()) {
51                         core.Log1(ctx, "A", aValue.Of(a))
52                         return ctx, func() {}
53                 },
54                 B: func(ctx context.Context, b string) (context.Context, func()) {
55                         core.Log1(ctx, "B", bValue.Of(b))
56                         return ctx, func() {}
57                 },
58         }
59
60         Trace = Hooks{
61                 A: func(ctx context.Context, a int) (context.Context, func()) {
62                         return core.Start1(ctx, "A", aValue.Of(a))
63                 },
64                 B: func(ctx context.Context, b string) (context.Context, func()) {
65                         return core.Start1(ctx, "B", bValue.Of(b))
66                 },
67         }
68
69         Stats = Hooks{
70                 A: func(ctx context.Context, a int) (context.Context, func()) {
71                         core.Metric1(ctx, aStat.Of(a))
72                         core.Metric1(ctx, aCount.Of(1))
73                         return ctx, func() {}
74                 },
75                 B: func(ctx context.Context, b string) (context.Context, func()) {
76                         core.Metric1(ctx, bLength.Of(len(b)))
77                         core.Metric1(ctx, bCount.Of(1))
78                         return ctx, func() {}
79                 },
80         }
81
82         initialList = []int{0, 1, 22, 333, 4444, 55555, 666666, 7777777}
83         stringList  = []string{
84                 "A value",
85                 "Some other value",
86                 "A nice longer value but not too long",
87                 "V",
88                 "",
89                 "ı",
90                 "prime count of values",
91         }
92 )
93
94 type namedBenchmark struct {
95         name string
96         test func(*testing.B)
97 }
98
99 func Benchmark(b *testing.B) {
100         b.Run("Baseline", Baseline.runBenchmark)
101         b.Run("StdLog", StdLog.runBenchmark)
102         benchmarks := []namedBenchmark{
103                 {"Log", Log.runBenchmark},
104                 {"Trace", Trace.runBenchmark},
105                 {"Stats", Stats.runBenchmark},
106         }
107
108         event.SetExporter(nil)
109         for _, t := range benchmarks {
110                 b.Run(t.name+"NoExporter", t.test)
111         }
112
113         event.SetExporter(noopExporter)
114         for _, t := range benchmarks {
115                 b.Run(t.name+"Noop", t.test)
116         }
117
118         event.SetExporter(export.Spans(export.LogWriter(ioutil.Discard, false)))
119         for _, t := range benchmarks {
120                 b.Run(t.name, t.test)
121         }
122 }
123
124 func A(ctx context.Context, hooks Hooks, a int) int {
125         ctx, done := hooks.A(ctx, a)
126         defer done()
127         return B(ctx, hooks, a, stringList[a%len(stringList)])
128 }
129
130 func B(ctx context.Context, hooks Hooks, a int, b string) int {
131         _, done := hooks.B(ctx, b)
132         defer done()
133         return a + len(b)
134 }
135
136 func (hooks Hooks) runBenchmark(b *testing.B) {
137         ctx := context.Background()
138         b.ReportAllocs()
139         b.ResetTimer()
140         var acc int
141         for i := 0; i < b.N; i++ {
142                 for _, value := range initialList {
143                         acc += A(ctx, hooks, value)
144                 }
145         }
146 }
147
148 func init() {
149         log.SetOutput(ioutil.Discard)
150 }
151
152 func noopExporter(ctx context.Context, ev core.Event, lm label.Map) context.Context {
153         return ctx
154 }