-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_test.go
184 lines (137 loc) · 3.25 KB
/
multi_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package batch_test
import (
"context"
"math/bits"
"runtime"
"sync"
"testing"
"nikand.dev/go/batch"
)
func TestMulti(tb *testing.T) {
ctx := context.Background()
var sum [2]int
var commitPanics [2]bool
bc := batch.NewMulti(len(sum), func(ctx context.Context, coach int) (int, error) {
if commitPanics[coach] {
tb.Logf("commit PANICS")
panic("commit PaNiC")
}
runtime.Gosched()
tb.Logf("coach %2d commit %2d", coach, sum[coach])
return sum[coach], nil
})
var wg sync.WaitGroup
for j := 0; j < *jobs; j++ {
j := j
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i <= 8; i++ {
i := i
func() {
if j == 1 && (i == 3 || i == 7) {
defer func() {
_ = recover()
}()
}
bc.Queue().In()
runtime.Gosched()
if j == 1 && i == 6 {
bc.Queue().Out()
bc.Notify()
return
}
coach, idx := bc.Enter(j != 0)
if idx < 0 {
tb.Logf("worker %2d iter %2d didn't enter %2d/%2d", j, i, coach, idx)
return
}
defer bc.Exit(coach)
runtime.Gosched()
if idx == 0 {
tb.Logf("coach %2d * * * ", coach)
sum[coach] = 0
}
tb.Logf("coach %2d worker %2d iter %2d enters %2d", coach, j, i, idx)
if j == 1 && i == 1 {
tb.Logf("coach %2d worker %2d iter %2d LEFT", coach, j, i)
return
}
sum[coach] += i
if j == 1 && i == 2 {
_, err := bc.Cancel(ctx, coach, nil)
tb.Logf("coach %2d worker %2d iter %2d CANCEL %v", coach, j, i, err)
return
}
if j == 1 && i == 3 {
tb.Logf("coach %2d worker %2d iter %2d PANICS", coach, j, i)
panic("pAnIc")
}
if j == 1 {
commitPanics[coach] = i == 4
}
if j == 1 && i == 5 {
bc.Trigger(coach)
}
res, err := bc.Commit(ctx, coach)
if err != nil {
_ = err
}
if pe, ok := batch.AsPanicError(err); ok {
tb.Logf("coach %2d worker %2d iter %2d panic %v", coach, j, i, pe)
} else {
tb.Logf("coach %2d worker %2d iter %2d res %2d %v", coach, j, i, res, err)
}
if j == 1 && i == 7 {
tb.Logf("coach %2d worker %2d iter %2d PANICS after commit", coach, j, i)
panic("panIC")
}
}()
}
}()
}
wg.Wait()
}
func BenchmarkMulti(tb *testing.B) {
const N = 8
ctx := context.Background()
var sum [N]int
var bc batch.Multi[int]
bc.Init(N, func(ctx context.Context, coach int) (int, error) {
return sum[coach], nil
})
run := func(tb *testing.PB) {
for tb.Next() {
func() {
bc.Queue().In()
// runtime.Gosched()
coach, idx := bc.Enter(true)
defer bc.Exit(coach)
// tb.Logf("worker %2d iter %2d enters %2d", j, i, idx)
if idx == 0 {
sum[coach] = 0
}
sum[coach] += 1
res, err := bc.Commit(ctx, coach)
if err != nil {
// tb.Errorf("commit: %v", err)
_ = err
}
// tb.Logf("worker %2d iter %2d res %2d %v", j, i, res, err)
_ = res
}()
}
}
tb.Run("Default_8", func(tb *testing.B) {
tb.ReportAllocs()
bc.Balancer = nil
tb.RunParallel(run)
})
tb.Run("Balancer_8", func(tb *testing.B) {
tb.ReportAllocs()
bc.Balancer = func(x []uint64) int {
return bits.Len64(x[0]) - 1 - 1 // highest-1 or -1
}
tb.RunParallel(run)
})
}