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