2020-03-01 16:39:15 -07:00
|
|
|
|
package telemetry_test
|
2019-12-19 09:57:21 -07:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2020-03-30 05:55:16 -06:00
|
|
|
|
"io/ioutil"
|
2020-03-07 16:02:27 -07:00
|
|
|
|
"log"
|
2019-12-19 09:57:21 -07:00
|
|
|
|
"testing"
|
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
2019-12-20 13:10:23 -07:00
|
|
|
|
"golang.org/x/tools/internal/telemetry/export"
|
2019-12-19 09:57:21 -07:00
|
|
|
|
)
|
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
|
type Hooks struct {
|
2020-03-24 18:44:37 -06:00
|
|
|
|
A func(ctx context.Context, a int) (context.Context, func())
|
|
|
|
|
B func(ctx context.Context, b string) (context.Context, func())
|
2020-03-03 15:15:30 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
2020-03-24 18:44:37 -06:00
|
|
|
|
aValue = event.NewIntKey("a", "")
|
2020-03-10 21:52:14 -06:00
|
|
|
|
bValue = event.NewStringKey("b", "")
|
2020-03-17 14:00:16 -06:00
|
|
|
|
aCount = event.NewInt64Key("aCount", "Count of time A is called.")
|
2020-03-24 18:44:37 -06:00
|
|
|
|
aStat = event.NewIntKey("aValue", "A value.")
|
2020-03-17 14:00:16 -06:00
|
|
|
|
bCount = event.NewInt64Key("B", "Count of time B is called.")
|
2020-03-24 18:44:37 -06:00
|
|
|
|
bLength = event.NewIntKey("BLen", "B length.")
|
2020-03-06 11:40:47 -07:00
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
|
Baseline = Hooks{
|
2020-03-24 18:44:37 -06:00
|
|
|
|
A: func(ctx context.Context, a int) (context.Context, func()) {
|
2020-03-03 15:15:30 -07:00
|
|
|
|
return ctx, func() {}
|
|
|
|
|
},
|
2020-03-24 18:44:37 -06:00
|
|
|
|
B: func(ctx context.Context, b string) (context.Context, func()) {
|
2020-03-03 15:15:30 -07:00
|
|
|
|
return ctx, func() {}
|
|
|
|
|
},
|
2019-12-19 09:57:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
|
StdLog = Hooks{
|
2020-03-24 18:44:37 -06:00
|
|
|
|
A: func(ctx context.Context, a int) (context.Context, func()) {
|
|
|
|
|
log.Printf("A where a=%d", a)
|
|
|
|
|
return ctx, func() {}
|
2020-03-03 15:15:30 -07:00
|
|
|
|
},
|
2020-03-24 18:44:37 -06:00
|
|
|
|
B: func(ctx context.Context, b string) (context.Context, func()) {
|
|
|
|
|
log.Printf("B where b=%q", b)
|
|
|
|
|
return ctx, func() {}
|
2020-03-03 15:15:30 -07:00
|
|
|
|
},
|
2019-12-19 09:57:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
|
Log = Hooks{
|
2020-03-24 18:44:37 -06:00
|
|
|
|
A: func(ctx context.Context, a int) (context.Context, func()) {
|
2020-03-25 20:50:00 -06:00
|
|
|
|
event.Print1(ctx, "A", aValue.Of(a))
|
2020-03-24 18:44:37 -06:00
|
|
|
|
return ctx, func() {}
|
2020-03-03 15:15:30 -07:00
|
|
|
|
},
|
2020-03-24 18:44:37 -06:00
|
|
|
|
B: func(ctx context.Context, b string) (context.Context, func()) {
|
2020-03-25 20:50:00 -06:00
|
|
|
|
event.Print1(ctx, "B", bValue.Of(b))
|
2020-03-24 18:44:37 -06:00
|
|
|
|
return ctx, func() {}
|
2020-03-03 15:15:30 -07:00
|
|
|
|
},
|
2019-12-19 09:57:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
|
Trace = Hooks{
|
2020-03-24 18:44:37 -06:00
|
|
|
|
A: func(ctx context.Context, a int) (context.Context, func()) {
|
2020-03-25 20:50:00 -06:00
|
|
|
|
return event.StartSpan1(ctx, "A", aValue.Of(a))
|
2020-03-03 15:15:30 -07:00
|
|
|
|
},
|
2020-03-24 18:44:37 -06:00
|
|
|
|
B: func(ctx context.Context, b string) (context.Context, func()) {
|
2020-03-25 20:50:00 -06:00
|
|
|
|
return event.StartSpan1(ctx, "B", bValue.Of(b))
|
2020-03-03 15:15:30 -07:00
|
|
|
|
},
|
2019-12-19 09:57:21 -07:00
|
|
|
|
}
|
2020-03-06 11:40:47 -07:00
|
|
|
|
|
|
|
|
|
Stats = Hooks{
|
2020-03-24 18:44:37 -06:00
|
|
|
|
A: func(ctx context.Context, a int) (context.Context, func()) {
|
2020-03-25 20:50:00 -06:00
|
|
|
|
event.Record1(ctx, aStat.Of(a))
|
|
|
|
|
event.Record1(ctx, aCount.Of(1))
|
2020-03-24 18:44:37 -06:00
|
|
|
|
return ctx, func() {}
|
2020-03-06 11:40:47 -07:00
|
|
|
|
},
|
2020-03-24 18:44:37 -06:00
|
|
|
|
B: func(ctx context.Context, b string) (context.Context, func()) {
|
2020-03-25 20:50:00 -06:00
|
|
|
|
event.Record1(ctx, bLength.Of(len(b)))
|
|
|
|
|
event.Record1(ctx, bCount.Of(1))
|
2020-03-24 18:44:37 -06:00
|
|
|
|
return ctx, func() {}
|
2020-03-06 11:40:47 -07:00
|
|
|
|
},
|
|
|
|
|
}
|
2020-03-24 18:44:37 -06:00
|
|
|
|
|
|
|
|
|
initialList = []int{0, 1, 22, 333, 4444, 55555, 666666, 7777777}
|
|
|
|
|
stringList = []string{
|
|
|
|
|
"A value",
|
|
|
|
|
"Some other value",
|
|
|
|
|
"A nice longer value but not too long",
|
|
|
|
|
"V",
|
|
|
|
|
"",
|
|
|
|
|
"ı",
|
|
|
|
|
"prime count of values",
|
|
|
|
|
}
|
2020-03-03 15:15:30 -07:00
|
|
|
|
)
|
|
|
|
|
|
2020-03-24 18:44:37 -06:00
|
|
|
|
type namedBenchmark struct {
|
|
|
|
|
name string
|
|
|
|
|
test func(*testing.B)
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
|
func Benchmark(b *testing.B) {
|
|
|
|
|
b.Run("Baseline", Baseline.runBenchmark)
|
|
|
|
|
b.Run("StdLog", StdLog.runBenchmark)
|
2020-03-24 18:44:37 -06:00
|
|
|
|
benchmarks := []namedBenchmark{
|
|
|
|
|
{"Log", Log.runBenchmark},
|
|
|
|
|
{"Trace", Trace.runBenchmark},
|
|
|
|
|
{"Stats", Stats.runBenchmark},
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
|
event.SetExporter(nil)
|
2020-03-24 18:44:37 -06:00
|
|
|
|
for _, t := range benchmarks {
|
|
|
|
|
b.Run(t.name+"NoExporter", t.test)
|
|
|
|
|
}
|
2020-03-03 15:15:30 -07:00
|
|
|
|
|
2020-03-18 21:28:57 -06:00
|
|
|
|
event.SetExporter(noopExporter)
|
2020-03-30 05:55:16 -06:00
|
|
|
|
for _, t := range benchmarks {
|
|
|
|
|
b.Run(t.name+"Noop", t.test)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event.SetExporter(export.Spans(export.LogWriter(ioutil.Discard, false)))
|
2020-03-24 18:44:37 -06:00
|
|
|
|
for _, t := range benchmarks {
|
|
|
|
|
b.Run(t.name, t.test)
|
|
|
|
|
}
|
2019-12-19 09:57:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
|
func A(ctx context.Context, hooks Hooks, a int) int {
|
2020-03-24 18:44:37 -06:00
|
|
|
|
ctx, done := hooks.A(ctx, a)
|
2020-03-01 16:39:15 -07:00
|
|
|
|
defer done()
|
2020-03-24 18:44:37 -06:00
|
|
|
|
return B(ctx, hooks, a, stringList[a%len(stringList)])
|
2020-03-01 16:39:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
|
func B(ctx context.Context, hooks Hooks, a int, b string) int {
|
2020-03-24 18:44:37 -06:00
|
|
|
|
_, done := hooks.B(ctx, b)
|
2020-03-01 16:39:15 -07:00
|
|
|
|
defer done()
|
2020-03-03 15:15:30 -07:00
|
|
|
|
return a + len(b)
|
2020-03-01 16:39:15 -07:00
|
|
|
|
}
|
2020-03-03 15:15:30 -07:00
|
|
|
|
|
|
|
|
|
func (hooks Hooks) runBenchmark(b *testing.B) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
var acc int
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-03-24 18:44:37 -06:00
|
|
|
|
for _, value := range initialList {
|
2020-03-03 15:15:30 -07:00
|
|
|
|
acc += A(ctx, hooks, value)
|
|
|
|
|
}
|
2019-12-19 09:57:21 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
|
func init() {
|
2020-03-30 05:55:16 -06:00
|
|
|
|
log.SetOutput(ioutil.Discard)
|
2019-12-19 09:57:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 05:55:16 -06:00
|
|
|
|
func noopExporter(ctx context.Context, ev event.Event, tagMap event.TagMap) context.Context {
|
|
|
|
|
return ctx
|
2020-03-03 15:15:30 -07:00
|
|
|
|
}
|