generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstream_bench_test.go
162 lines (129 loc) · 2.79 KB
/
stream_bench_test.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package stream
import (
"context"
"testing"
"go.devnw.com/gen"
)
func Benchmark_Pipe(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c1, c2 := make(chan int), make(chan int)
value := 10
go Pipe(ctx, c1, c2)
for n := 0; n < b.N; n++ {
select {
case <-ctx.Done():
b.Fatal("context canceled")
case c1 <- value:
case out, ok := <-c2:
if !ok {
b.Fatal("c2 closed prematurely")
}
if out != value {
b.Errorf("expected %v, got %v", value, out)
}
}
}
}
func Benchmark_Intercept(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in := make(chan int)
value := 10
out := Intercept(ctx, in, func(_ context.Context, v int) (int, bool) {
return v % 3, true
})
for n := 0; n < b.N; n++ {
select {
case <-ctx.Done():
b.Fatal("context canceled")
case in <- value:
case out, ok := <-out:
if !ok {
b.Fatal("c2 closed prematurely")
}
if out != value%3 {
b.Errorf("expected %v, got %v", value, out)
}
}
}
}
func Benchmark_FanIn(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c1, c2 := make(chan int), make(chan int)
out := FanIn(ctx, c1, c2)
for n := 0; n < b.N; n++ {
c1 <- 1
c2 <- 2
for i := 0; i < 2; i++ {
select {
case <-ctx.Done():
b.Fatal("context canceled")
case _, ok := <-out:
if !ok {
b.Fatal("out closed prematurely")
}
}
}
}
}
func Benchmark_FanOut(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in, out1, out2 := make(chan int), make(chan int), make(chan int)
go FanOut(ctx, in, out1, out2)
for n := 0; n < b.N; n++ {
in <- 1
<-out1
<-out2
}
}
func Benchmark_Distribute(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in, out1, out2 := make(chan int), make(chan int), make(chan int)
go Distribute(ctx, in, out1, out2)
for n := 0; n < b.N; n++ {
in <- 1
select {
case <-out1:
case <-out2:
}
}
}
func Benchmark_Scaler(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testdata := gen.Slice[int](Ints[int](100))
s := Scaler[int, int]{
Fn: func(_ context.Context, in int) (int, bool) {
return in, true
},
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
// Test that the scaler can be used with a nil context.
//nolint:staticcheck // nil context on purpose
out, err := s.Exec(nil, testdata.Chan(ctx))
if err != nil {
b.Errorf("expected no error, got %v", err)
}
seen := 0
tloop:
for {
select {
case <-ctx.Done():
b.Fatal("context closed")
case _, ok := <-out:
if !ok {
break tloop
}
seen++
}
}
if seen != len(testdata) {
b.Errorf("expected %v, got %v", len(testdata), seen)
}
}
}