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.
13 "golang.org/x/tools/internal/event"
14 "golang.org/x/tools/internal/event/core"
15 "golang.org/x/tools/internal/event/export"
16 "golang.org/x/tools/internal/event/keys"
17 "golang.org/x/tools/internal/event/label"
21 A func(ctx context.Context, a int) (context.Context, func())
22 B func(ctx context.Context, b string) (context.Context, func())
26 aValue = keys.NewInt("a", "")
27 bValue = keys.NewString("b", "")
28 aCount = keys.NewInt64("aCount", "Count of time A is called.")
29 aStat = keys.NewInt("aValue", "A value.")
30 bCount = keys.NewInt64("B", "Count of time B is called.")
31 bLength = keys.NewInt("BLen", "B length.")
34 A: func(ctx context.Context, a int) (context.Context, func()) {
37 B: func(ctx context.Context, b string) (context.Context, func()) {
43 A: func(ctx context.Context, a int) (context.Context, func()) {
44 log.Printf("A where a=%d", a)
47 B: func(ctx context.Context, b string) (context.Context, func()) {
48 log.Printf("B where b=%q", b)
54 A: func(ctx context.Context, a int) (context.Context, func()) {
55 core.Log1(ctx, "A", aValue.Of(a))
58 B: func(ctx context.Context, b string) (context.Context, func()) {
59 core.Log1(ctx, "B", bValue.Of(b))
65 A: func(ctx context.Context, a int) (context.Context, func()) {
66 return core.Start1(ctx, "A", aValue.Of(a))
68 B: func(ctx context.Context, b string) (context.Context, func()) {
69 return core.Start1(ctx, "B", bValue.Of(b))
74 A: func(ctx context.Context, a int) (context.Context, func()) {
75 core.Metric1(ctx, aStat.Of(a))
76 core.Metric1(ctx, aCount.Of(1))
79 B: func(ctx context.Context, b string) (context.Context, func()) {
80 core.Metric1(ctx, bLength.Of(len(b)))
81 core.Metric1(ctx, bCount.Of(1))
86 initialList = []int{0, 1, 22, 333, 4444, 55555, 666666, 7777777}
87 stringList = []string{
90 "A nice longer value but not too long",
94 "prime count of values",
98 type namedBenchmark struct {
100 test func(*testing.B)
103 func Benchmark(b *testing.B) {
104 b.Run("Baseline", Baseline.runBenchmark)
105 b.Run("StdLog", StdLog.runBenchmark)
106 benchmarks := []namedBenchmark{
107 {"Log", Log.runBenchmark},
108 {"Trace", Trace.runBenchmark},
109 {"Stats", Stats.runBenchmark},
112 event.SetExporter(nil)
113 for _, t := range benchmarks {
114 b.Run(t.name+"NoExporter", t.test)
117 event.SetExporter(noopExporter)
118 for _, t := range benchmarks {
119 b.Run(t.name+"Noop", t.test)
122 event.SetExporter(export.Spans(export.LogWriter(ioutil.Discard, false)))
123 for _, t := range benchmarks {
124 b.Run(t.name, t.test)
128 func A(ctx context.Context, hooks Hooks, a int) int {
129 ctx, done := hooks.A(ctx, a)
131 return B(ctx, hooks, a, stringList[a%len(stringList)])
134 func B(ctx context.Context, hooks Hooks, a int, b string) int {
135 _, done := hooks.B(ctx, b)
140 func (hooks Hooks) runBenchmark(b *testing.B) {
141 ctx := context.Background()
145 for i := 0; i < b.N; i++ {
146 for _, value := range initialList {
147 acc += A(ctx, hooks, value)
153 log.SetOutput(ioutil.Discard)
156 func noopExporter(ctx context.Context, ev core.Event, lm label.Map) context.Context {