Debian Codegraph @

event/eventtest/bench.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 eventtest
6
7 import (
8 "context"
9 "io"
10 "strings"
11 "testing"
12
13 "github.com/google/go-cmp/cmp"
14 "golang.org/x/exp/event/adapter/logfmt"
15 )
16
17 type Info struct {
18 Name string
19 Msg string
20 Msgf string
21 }
22
23 type Hooks struct {
24 AStart func(ctx context.Context, a int) context.Context
25 AEnd func(ctx context.Context)
26 BStart func(ctx context.Context, b string) context.Context
27 BEnd func(ctx context.Context)
28 }
29
30 var (
31 initialList = []int{0, 1, 22, 333, 4444, 55555, 666666, 7777777}
32 stringList = []string{
33 "A value",
34 "Some other value",
35 "A nice longer value but not too long",
36 "V",
37 " ",
38 "ı",
39 "prime count of values",
40 }
41
42 A = Info{
43 Name: "A",
44 Msg: "a",
45 Msgf: "a where A=%d",
46 }
47
48 B = Info{
49 Name: "B",
50 Msg: "b",
51 Msgf: "b where B=%q",
52 }
53 )
54
55 const (
56 TimeFormat = logfmt.TimeFormat
57
58 LogfmtOutput = `
59 time="2020/03/05 14:27:48" level=info A=0 msg=a
60 time="2020/03/05 14:27:49" level=info B="A value" msg=b
61 time="2020/03/05 14:27:50" level=info A=1 msg=a
62 time="2020/03/05 14:27:51" level=info B="Some other value" msg=b
63 time="2020/03/05 14:27:52" level=info A=22 msg=a
64 time="2020/03/05 14:27:53" level=info B="Some other value" msg=b
65 time="2020/03/05 14:27:54" level=info A=333 msg=a
66 time="2020/03/05 14:27:55" level=info B=" " msg=b
67 time="2020/03/05 14:27:56" level=info A=4444 msg=a
68 time="2020/03/05 14:27:57" level=info B="prime count of values" msg=b
69 time="2020/03/05 14:27:58" level=info A=55555 msg=a
70 time="2020/03/05 14:27:59" level=info B=V msg=b
71 time="2020/03/05 14:28:00" level=info A=666666 msg=a
72 time="2020/03/05 14:28:01" level=info B="A value" msg=b
73 time="2020/03/05 14:28:02" level=info A=7777777 msg=a
74 time="2020/03/05 14:28:03" level=info B="A value" msg=b
75 `
76
77 LogfOutput = `
78 time="2020/03/05 14:27:48" level=info msg="a where A=0"
79 time="2020/03/05 14:27:49" level=info msg="b where B=\"A value\""
80 time="2020/03/05 14:27:50" level=info msg="a where A=1"
81 time="2020/03/05 14:27:51" level=info msg="b where B=\"Some other value\""
82 time="2020/03/05 14:27:52" level=info msg="a where A=22"
83 time="2020/03/05 14:27:53" level=info msg="b where B=\"Some other value\""
84 time="2020/03/05 14:27:54" level=info msg="a where A=333"
85 time="2020/03/05 14:27:55" level=info msg="b where B=\" \""
86 time="2020/03/05 14:27:56" level=info msg="a where A=4444"
87 time="2020/03/05 14:27:57" level=info msg="b where B=\"prime count of values\""
88 time="2020/03/05 14:27:58" level=info msg="a where A=55555"
89 time="2020/03/05 14:27:59" level=info msg="b where B=\"V\""
90 time="2020/03/05 14:28:00" level=info msg="a where A=666666"
91 time="2020/03/05 14:28:01" level=info msg="b where B=\"A value\""
92 time="2020/03/05 14:28:02" level=info msg="a where A=7777777"
93 time="2020/03/05 14:28:03" level=info msg="b where B=\"A value\""
94 `
95 )
96
97 type namedBenchmark struct {
98 name string
99 test func(ctx context.Context) func(*testing.B)
100 }
101
102 func benchA(ctx context.Context, hooks Hooks, a int) int {
103 ctx = hooks.AStart(ctx, a)
104 defer hooks.AEnd(ctx)
105 return benchB(ctx, hooks, a, stringList[a%len(stringList)])
106 }
107
108 func benchB(ctx context.Context, hooks Hooks, a int, b string) int {
109 ctx = hooks.BStart(ctx, b)
110 defer hooks.BEnd(ctx)
111 return a + len(b)
112 }
113
114 func runOnce(ctx context.Context, hooks Hooks) {
115 var acc int
116 for _, value := range initialList {
117 acc += benchA(ctx, hooks, value)
118 }
119 }
120
121 func RunBenchmark(b *testing.B, ctx context.Context, hooks Hooks) {
122 b.ResetTimer()
123 for i := 0; i < b.N; i++ {
124 runOnce(ctx, hooks)
125 }
126 }
127
128 func TestBenchmark(t *testing.T, f func(io.Writer) context.Context, hooks Hooks, expect string) {
129 buf := strings.Builder{}
130 ctx := f(&buf)
131 runOnce(ctx, hooks)
132 got := strings.TrimSpace(buf.String())
133 expect = strings.TrimSpace(expect)
134 if diff := cmp.Diff(got, expect); diff != "" {
135 t.Error(diff)
136 }
137 }
138
139 func TestAllocs(t *testing.T, f func(io.Writer) context.Context, hooks Hooks, expect int) {
140 t.Helper()
141 var acc int
142 ctx := f(io.Discard)
143 got := int(testing.AllocsPerRun(5, func() {
144 for _, value := range initialList {
145 acc += benchA(ctx, hooks, value)
146 }
147 }))
148 if got != expect {
149 t.Errorf("Got %d allocs, expect %d", got, expect)
150 }
151 }