2020-03-01 16:39:15 -07:00
|
|
|
package telemetry_test
|
2019-12-19 09:57:21 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-07 16:02:27 -07:00
|
|
|
"log"
|
2019-12-19 09:57:21 -07:00
|
|
|
"strings"
|
|
|
|
"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"
|
2020-03-06 11:40:47 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/stats"
|
|
|
|
"golang.org/x/tools/internal/telemetry/unit"
|
2019-12-19 09:57:21 -07:00
|
|
|
)
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
type Hooks struct {
|
|
|
|
A func(ctx context.Context, a *int) (context.Context, func())
|
|
|
|
B func(ctx context.Context, b *string) (context.Context, func())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-03-10 21:52:14 -06:00
|
|
|
aValue = event.NewInt64Key("a", "")
|
|
|
|
bValue = event.NewStringKey("b", "")
|
2020-03-06 11:40:47 -07:00
|
|
|
aCount = stats.Int64("aCount", "Count of time A is called.", unit.Dimensionless)
|
2020-03-07 22:03:39 -07:00
|
|
|
aStat = stats.Int64("aValue", "A value.", unit.Dimensionless)
|
2020-03-06 11:40:47 -07:00
|
|
|
bCount = stats.Int64("B", "Count of time B is called.", unit.Dimensionless)
|
|
|
|
bLength = stats.Int64("BLen", "B length.", unit.Dimensionless)
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
Baseline = Hooks{
|
|
|
|
A: func(ctx context.Context, a *int) (context.Context, func()) {
|
|
|
|
return ctx, func() {}
|
|
|
|
},
|
|
|
|
B: func(ctx context.Context, b *string) (context.Context, func()) {
|
|
|
|
return ctx, func() {}
|
|
|
|
},
|
2019-12-19 09:57:21 -07:00
|
|
|
}
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
StdLog = Hooks{
|
|
|
|
A: func(ctx context.Context, a *int) (context.Context, func()) {
|
2020-03-07 16:02:27 -07:00
|
|
|
log.Printf("start A where a=%d", *a)
|
2020-03-03 15:15:30 -07:00
|
|
|
return ctx, func() {
|
2020-03-07 16:02:27 -07:00
|
|
|
log.Printf("end A where a=%d", *a)
|
2020-03-03 15:15:30 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
B: func(ctx context.Context, b *string) (context.Context, func()) {
|
2020-03-07 16:02:27 -07:00
|
|
|
log.Printf("start B where b=%q", *b)
|
2020-03-03 15:15:30 -07:00
|
|
|
return ctx, func() {
|
2020-03-07 16:02:27 -07:00
|
|
|
log.Printf("end B where b=%q", *b)
|
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{
|
|
|
|
A: func(ctx context.Context, a *int) (context.Context, func()) {
|
2020-03-10 21:52:14 -06:00
|
|
|
event.Print(ctx, "start A", aValue.Of(int64(*a)))
|
2020-03-03 15:15:30 -07:00
|
|
|
return ctx, func() {
|
2020-03-10 21:52:14 -06:00
|
|
|
event.Print(ctx, "end A", aValue.Of(int64(*a)))
|
2020-03-03 15:15:30 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
B: func(ctx context.Context, b *string) (context.Context, func()) {
|
2020-03-07 22:03:39 -07:00
|
|
|
event.Print(ctx, "start B", bValue.Of(*b))
|
2020-03-03 15:15:30 -07:00
|
|
|
return ctx, func() {
|
2020-03-07 22:03:39 -07:00
|
|
|
event.Print(ctx, "end B", bValue.Of(*b))
|
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{
|
|
|
|
A: func(ctx context.Context, a *int) (context.Context, func()) {
|
2020-03-07 16:02:27 -07:00
|
|
|
return event.StartSpan(ctx, "A")
|
2020-03-03 15:15:30 -07:00
|
|
|
},
|
|
|
|
B: func(ctx context.Context, b *string) (context.Context, func()) {
|
2020-03-07 16:02:27 -07:00
|
|
|
return event.StartSpan(ctx, "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{
|
|
|
|
A: func(ctx context.Context, a *int) (context.Context, func()) {
|
|
|
|
aCount.Record(ctx, 1)
|
|
|
|
return ctx, func() {
|
2020-03-07 22:03:39 -07:00
|
|
|
aStat.Record(ctx, int64(*a))
|
2020-03-06 11:40:47 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
B: func(ctx context.Context, b *string) (context.Context, func()) {
|
|
|
|
bCount.Record(ctx, 1)
|
|
|
|
return ctx, func() {
|
|
|
|
bLength.Record(ctx, int64(len(*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-07 16:02:27 -07:00
|
|
|
event.SetExporter(nil)
|
2020-03-03 15:15:30 -07:00
|
|
|
b.Run("LogNoExporter", Log.runBenchmark)
|
|
|
|
b.Run("TraceNoExporter", Trace.runBenchmark)
|
2020-03-06 11:40:47 -07:00
|
|
|
b.Run("StatsNoExporter", Stats.runBenchmark)
|
2020-03-03 15:15:30 -07:00
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
event.SetExporter(newExporter())
|
2020-03-03 15:15:30 -07:00
|
|
|
b.Run("Log", Log.runBenchmark)
|
|
|
|
b.Run("Trace", Trace.runBenchmark)
|
2020-03-06 11:40:47 -07:00
|
|
|
b.Run("Stats", Stats.runBenchmark)
|
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 {
|
|
|
|
ctx, done := hooks.A(ctx, &a)
|
2020-03-01 16:39:15 -07:00
|
|
|
defer done()
|
|
|
|
if a > 0 {
|
2020-03-03 15:15:30 -07:00
|
|
|
a = a * 10
|
2020-03-01 16:39:15 -07:00
|
|
|
}
|
2020-03-03 15:15:30 -07:00
|
|
|
return B(ctx, hooks, a, "Called from A")
|
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 {
|
|
|
|
_, done := hooks.B(ctx, &b)
|
2020-03-01 16:39:15 -07:00
|
|
|
defer done()
|
|
|
|
b = strings.ToUpper(b)
|
|
|
|
if len(b) > 1024 {
|
|
|
|
b = strings.ToLower(b)
|
|
|
|
}
|
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++ {
|
|
|
|
for _, value := range []int{0, 10, 20, 100, 1000} {
|
|
|
|
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-07 16:02:27 -07:00
|
|
|
log.SetOutput(new(noopWriter))
|
2019-12-19 09:57:21 -07:00
|
|
|
}
|
|
|
|
|
2020-03-03 15:15:30 -07:00
|
|
|
type noopWriter int
|
|
|
|
|
|
|
|
func (nw *noopWriter) Write(b []byte) (int, error) {
|
|
|
|
return len(b), nil
|
|
|
|
}
|
2019-12-20 13:10:23 -07:00
|
|
|
|
2020-03-01 16:39:15 -07:00
|
|
|
type loggingExporter struct {
|
2020-03-07 16:02:27 -07:00
|
|
|
logger event.Exporter
|
2020-03-01 16:39:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func newExporter() *loggingExporter {
|
|
|
|
return &loggingExporter{
|
|
|
|
logger: export.LogWriter(new(noopWriter), false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 08:01:57 -06:00
|
|
|
func (e *loggingExporter) ProcessEvent(ctx context.Context, ev event.Event) (context.Context, event.Event) {
|
|
|
|
ctx, ev = export.ContextSpan(ctx, ev)
|
2020-03-07 16:02:27 -07:00
|
|
|
return e.logger.ProcessEvent(ctx, ev)
|
2020-03-01 16:39:15 -07:00
|
|
|
}
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
func (e *loggingExporter) Metric(ctx context.Context, data event.MetricData) {
|
2020-03-01 16:39:15 -07:00
|
|
|
e.logger.Metric(ctx, data)
|
|
|
|
}
|