1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package event_test
import (
"context"
"io"
"testing"
"golang.org/x/exp/event"
"golang.org/x/exp/event/adapter/logfmt"
"golang.org/x/exp/event/eventtest"
"golang.org/x/exp/event/severity"
)
var (
eventLog = eventtest.Hooks{
AStart: func(ctx context.Context, a int) context.Context {
severity.Info.Log(ctx, eventtest.A.Msg, event.Int64(eventtest.A.Name, int64(a)))
return ctx
},
AEnd: func(ctx context.Context) {},
BStart: func(ctx context.Context, b string) context.Context {
severity.Info.Log(ctx, eventtest.B.Msg, event.String(eventtest.B.Name, b))
return ctx
},
BEnd: func(ctx context.Context) {},
}
eventLogf = eventtest.Hooks{
AStart: func(ctx context.Context, a int) context.Context {
severity.Info.Logf(ctx, eventtest.A.Msgf, a)
return ctx
},
AEnd: func(ctx context.Context) {},
BStart: func(ctx context.Context, b string) context.Context {
severity.Info.Logf(ctx, eventtest.B.Msgf, b)
return ctx
},
BEnd: func(ctx context.Context) {},
}
eventTrace = eventtest.Hooks{
AStart: func(ctx context.Context, a int) context.Context {
ctx = event.Start(ctx, eventtest.A.Msg, event.Int64(eventtest.A.Name, int64(a)))
return ctx
},
AEnd: func(ctx context.Context) {
event.End(ctx)
},
BStart: func(ctx context.Context, b string) context.Context {
ctx = event.Start(ctx, eventtest.B.Msg, event.String(eventtest.B.Name, b))
return ctx
},
BEnd: func(ctx context.Context) {
event.End(ctx)
},
}
eventMetric = eventtest.Hooks{
AStart: func(ctx context.Context, a int) context.Context {
gauge.Record(ctx, 1, event.Int64("aValue", int64(a)))
gauge.Record(ctx, 1, event.Int64("aCount", 1))
return ctx
},
AEnd: func(ctx context.Context) {},
BStart: func(ctx context.Context, b string) context.Context {
gauge.Record(ctx, 1, event.Int64("BLen", int64(len(b))))
gauge.Record(ctx, 1, event.Int64("B", 1))
return ctx
},
BEnd: func(ctx context.Context) {},
}
)
func eventNoExporter() context.Context {
return event.WithExporter(context.Background(), nil)
}
func eventNoop() context.Context {
return event.WithExporter(context.Background(), event.NewExporter(nopHandler{}, eventtest.ExporterOptions()))
}
func eventPrint(w io.Writer) context.Context {
return event.WithExporter(context.Background(), event.NewExporter(logfmt.NewHandler(w), eventtest.ExporterOptions()))
}
func eventPrintSource(w io.Writer) context.Context {
opts := eventtest.ExporterOptions()
opts.EnableNamespaces = true
return event.WithExporter(context.Background(), event.NewExporter(logfmt.NewHandler(w), opts))
}
type nopHandler struct{}
func (nopHandler) Event(ctx context.Context, _ *event.Event) context.Context { return ctx }
func BenchmarkEventLogNoExporter(b *testing.B) {
eventtest.RunBenchmark(b, eventNoExporter(), eventLog)
}
func BenchmarkEventLogNoop(b *testing.B) {
eventtest.RunBenchmark(b, eventNoop(), eventLog)
}
func BenchmarkEventLogDiscard(b *testing.B) {
eventtest.RunBenchmark(b, eventPrint(io.Discard), eventLog)
}
func BenchmarkEventLogSourceDiscard(b *testing.B) {
eventtest.RunBenchmark(b, eventPrintSource(io.Discard), eventLog)
}
func BenchmarkEventLogfDiscard(b *testing.B) {
eventtest.RunBenchmark(b, eventPrint(io.Discard), eventLogf)
}
func BenchmarkEventTraceNoop(b *testing.B) {
eventtest.RunBenchmark(b, eventNoop(), eventTrace)
}
func BenchmarkEventTraceDiscard(b *testing.B) {
eventtest.RunBenchmark(b, eventPrint(io.Discard), eventTrace)
}
func BenchmarkEventMetricNoop(b *testing.B) {
eventtest.RunBenchmark(b, eventNoop(), eventMetric)
}
func BenchmarkEventMetricDiscard(b *testing.B) {
eventtest.RunBenchmark(b, eventPrint(io.Discard), eventMetric)
}
|