Debian Codegraph @

event/adapter/stdlib/bench_test.go

raw symbols SCIP
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package stdlib_test
6
7 import (
8 "context"
9 "fmt"
10 "io"
11 "log"
12 "testing"
13
14 "golang.org/x/exp/event/eventtest"
15 )
16
17 var (
18 baseline = eventtest.Hooks{
19 AStart: func(ctx context.Context, a int) context.Context { return ctx },
20 AEnd: func(ctx context.Context) {},
21 BStart: func(ctx context.Context, b string) context.Context { return ctx },
22 BEnd: func(ctx context.Context) {},
23 }
24
25 stdlibLog = eventtest.Hooks{
26 AStart: func(ctx context.Context, a int) context.Context {
27 logCtx(ctx).Printf(eventtest.A.Msgf, a)
28 return ctx
29 },
30 AEnd: func(ctx context.Context) {},
31 BStart: func(ctx context.Context, b string) context.Context {
32 logCtx(ctx).Printf(eventtest.B.Msgf, b)
33 return ctx
34 },
35 BEnd: func(ctx context.Context) {},
36 }
37
38 stdlibPrintf = eventtest.Hooks{
39 AStart: func(ctx context.Context, a int) context.Context {
40 ctxPrintf(ctx, eventtest.A.Msgf, a)
41 return ctx
42 },
43 AEnd: func(ctx context.Context) {},
44 BStart: func(ctx context.Context, b string) context.Context {
45 ctxPrintf(ctx, eventtest.B.Msgf, b)
46 return ctx
47 },
48 BEnd: func(ctx context.Context) {},
49 }
50 )
51
52 func BenchmarkBaseline(b *testing.B) {
53 eventtest.RunBenchmark(b, context.Background(), eventtest.Hooks{
54 AStart: func(ctx context.Context, a int) context.Context { return ctx },
55 AEnd: func(ctx context.Context) {},
56 BStart: func(ctx context.Context, b string) context.Context { return ctx },
57 BEnd: func(ctx context.Context) {},
58 })
59 }
60
61 type stdlibLogKey struct{}
62
63 func logCtx(ctx context.Context) *log.Logger {
64 return ctx.Value(stdlibLogKey{}).(*log.Logger)
65 }
66
67 func stdlibLogger(w io.Writer) context.Context {
68 logger := log.New(w, "", log.LstdFlags)
69 return context.WithValue(context.Background(), stdlibLogKey{}, logger)
70 }
71
72 func stdlibLoggerNoTime(w io.Writer) context.Context {
73 // there is no way to fixup the time, so we have to suppress it
74 logger := log.New(w, "", 0)
75 return context.WithValue(context.Background(), stdlibLogKey{}, logger)
76 }
77
78 type writerKey struct{}
79
80 func ctxPrintf(ctx context.Context, msg string, args ...interface{}) {
81 ctx.Value(writerKey{}).(func(string, ...interface{}))(msg, args...)
82 }
83
84 func stdlibWriter(w io.Writer) context.Context {
85 now := eventtest.ExporterOptions().Now
86 return context.WithValue(context.Background(), writerKey{},
87 func(msg string, args ...interface{}) {
88 fmt.Fprintf(w, "time=%q level=info msg=%q\n",
89 now().Format(eventtest.TimeFormat),
90 fmt.Sprintf(msg, args...))
91 },
92 )
93 }
94
95 func BenchmarkStdlibLogfDiscard(b *testing.B) {
96 eventtest.RunBenchmark(b, stdlibLogger(io.Discard), stdlibLog)
97 }
98
99 func BenchmarkStdlibPrintfDiscard(b *testing.B) {
100 eventtest.RunBenchmark(b, stdlibWriter(io.Discard), stdlibPrintf)
101 }
102
103 func TestLogStdlib(t *testing.T) {
104 eventtest.TestBenchmark(t, stdlibLoggerNoTime, stdlibLog, `
105 a where A=0
106 b where B="A value"
107 a where A=1
108 b where B="Some other value"
109 a where A=22
110 b where B="Some other value"
111 a where A=333
112 b where B=" "
113 a where A=4444
114 b where B="prime count of values"
115 a where A=55555
116 b where B="V"
117 a where A=666666
118 b where B="A value"
119 a where A=7777777
120 b where B="A value"
121 `)
122 }
123
124 func TestLogPrintf(t *testing.T) {
125 eventtest.TestBenchmark(t, stdlibWriter, stdlibPrintf, eventtest.LogfOutput)
126 }