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"
17 A func(ctx context.Context, a int) (context.Context, func())
18 B func(ctx context.Context, b string) (context.Context, func())
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.")
30 A: func(ctx context.Context, a int) (context.Context, func()) {
33 B: func(ctx context.Context, b string) (context.Context, func()) {
39 A: func(ctx context.Context, a int) (context.Context, func()) {
40 log.Printf("A where a=%d", a)
43 B: func(ctx context.Context, b string) (context.Context, func()) {
44 log.Printf("B where b=%q", b)
50 A: func(ctx context.Context, a int) (context.Context, func()) {
51 core.Log1(ctx, "A", aValue.Of(a))
54 B: func(ctx context.Context, b string) (context.Context, func()) {
55 core.Log1(ctx, "B", bValue.Of(b))
61 A: func(ctx context.Context, a int) (context.Context, func()) {
62 return core.Start1(ctx, "A", aValue.Of(a))
64 B: func(ctx context.Context, b string) (context.Context, func()) {
65 return core.Start1(ctx, "B", bValue.Of(b))
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))
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))
82 initialList = []int{0, 1, 22, 333, 4444, 55555, 666666, 7777777}
83 stringList = []string{
86 "A nice longer value but not too long",
90 "prime count of values",
94 type namedBenchmark struct {
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},
108 event.SetExporter(nil)
109 for _, t := range benchmarks {
110 b.Run(t.name+"NoExporter", t.test)
113 event.SetExporter(noopExporter)
114 for _, t := range benchmarks {
115 b.Run(t.name+"Noop", t.test)
118 event.SetExporter(export.Spans(export.LogWriter(ioutil.Discard, false)))
119 for _, t := range benchmarks {
120 b.Run(t.name, t.test)
124 func A(ctx context.Context, hooks Hooks, a int) int {
125 ctx, done := hooks.A(ctx, a)
127 return B(ctx, hooks, a, stringList[a%len(stringList)])
130 func B(ctx context.Context, hooks Hooks, a int, b string) int {
131 _, done := hooks.B(ctx, b)
136 func (hooks Hooks) runBenchmark(b *testing.B) {
137 ctx := context.Background()
141 for i := 0; i < b.N; i++ {
142 for _, value := range initialList {
143 acc += A(ctx, hooks, value)
149 log.SetOutput(ioutil.Discard)
152 func noopExporter(ctx context.Context, ev core.Event, lm label.Map) context.Context {