@@ -7,10 +7,24 @@ import (
7
7
"fortio.org/memstore/cb"
8
8
)
9
9
10
+ const capacity = 5
11
+
10
12
func TestBoundaryConditions (t * testing.T ) {
11
- const capacity = 5
12
- buffer := cb.New [int ](capacity )
13
+ testCases := []struct {
14
+ name string
15
+ cb cb.Queue [int ]
16
+ }{
17
+ {name : "CircBuffer" , cb : cb.New [int ](capacity )},
18
+ {name : "Channel" , cb : cb.NewC [int ](capacity )},
19
+ }
20
+ for _ , tc := range testCases {
21
+ t .Run (tc .name , func (tt * testing.T ) {
22
+ testBoundaryConditions (tt , tc .cb )
23
+ })
24
+ }
25
+ }
13
26
27
+ func testBoundaryConditions (t * testing.T , buffer cb.Queue [int ]) {
14
28
// Test empty buffer
15
29
if ! buffer .Empty () {
16
30
t .Error ("Buffer should be empty" )
@@ -91,7 +105,22 @@ func TestBoundaryConditions(t *testing.T) {
91
105
}
92
106
93
107
func BenchmarkCircularBuffer (b * testing.B ) {
94
- c := cb.New [int ](100 )
108
+ capacity := 100
109
+ testCases := []struct {
110
+ name string
111
+ cb cb.Queue [int ]
112
+ }{
113
+ {name : "CircBuffer" , cb : cb.New [int ](capacity )},
114
+ {name : "Channel" , cb : cb.NewC [int ](capacity )},
115
+ }
116
+ for _ , tc := range testCases {
117
+ b .Run (tc .name , func (bb * testing.B ) {
118
+ benchmarkCircularBuffer (bb , tc .cb )
119
+ })
120
+ }
121
+ }
122
+
123
+ func benchmarkCircularBuffer (b * testing.B , c cb.Queue [int ]) {
95
124
var x int
96
125
var ok bool
97
126
for i := 0 ; i < b .N ; i ++ {
@@ -105,8 +134,20 @@ func BenchmarkCircularBuffer(b *testing.B) {
105
134
}
106
135
107
136
func TestProducerConsumerScenario (t * testing.T ) {
108
- buffer := cb.New [int ](10 )
109
-
137
+ testCases := []struct {
138
+ name string
139
+ cb cb.Queue [int ]
140
+ }{
141
+ {name : "CircBuffer" , cb : cb.New [int ](10 )},
142
+ {name : "Channel" , cb : cb.NewC [int ](10 )},
143
+ }
144
+ for _ , tc := range testCases {
145
+ t .Run (tc .name , func (tt * testing.T ) {
146
+ testProducerConsumerScenario (tt , tc .cb )
147
+ })
148
+ }
149
+ }
150
+ func testProducerConsumerScenario (t * testing.T , buffer cb.Queue [int ]) {
110
151
var wg sync.WaitGroup
111
152
wg .Add (11 ) // 10 producers + 1 consumer
112
153
@@ -143,7 +184,21 @@ func TestProducerConsumerScenario(t *testing.T) {
143
184
}
144
185
145
186
func BenchmarkCircularBufferBlocking (b * testing.B ) {
146
- c := cb.New [int ](100 )
187
+ capacity := 100
188
+ testCases := []struct {
189
+ name string
190
+ cb cb.Queue [int ]
191
+ }{
192
+ {name : "CircBuffer" , cb : cb.New [int ](capacity )},
193
+ {name : "Channel" , cb : cb.NewC [int ](capacity )},
194
+ }
195
+ for _ , tc := range testCases {
196
+ b .Run (tc .name , func (bb * testing.B ) {
197
+ benchmarkCircularBufferBlocking (bb , tc .cb )
198
+ })
199
+ }
200
+ }
201
+ func benchmarkCircularBufferBlocking (b * testing.B , c cb.Queue [int ]) {
147
202
var x int
148
203
for i := 0 ; i < b .N ; i ++ {
149
204
c .PushBlocking (i )
@@ -155,9 +210,7 @@ func BenchmarkCircularBufferBlocking(b *testing.B) {
155
210
b .Logf ("x=%d" , x )
156
211
}
157
212
158
- func benchmarkPushBlocking (b * testing.B , numProducers , numConsumers int ) {
159
- buffer := cb.New [int ](20 ) // small queue, higher contention
160
-
213
+ func benchmarkPushBlocking (b * testing.B , buffer cb.Queue [int ], numProducers , numConsumers int ) {
161
214
var wg sync.WaitGroup
162
215
wg .Add (numProducers + numConsumers )
163
216
prodN := b .N * numConsumers
@@ -191,11 +244,20 @@ func benchmarkPushBlocking(b *testing.B, numProducers, numConsumers int) {
191
244
wg .Wait ()
192
245
}
193
246
194
- func BenchmarkPushBlocking (b * testing.B ) {
247
+ func BenchmarkHighContention (b * testing.B ) {
195
248
numProducers := 13
196
249
numConsumers := 7
197
250
198
- b .Run ("BenchmarkPushBlocking 7,5" , func (b * testing.B ) {
199
- benchmarkPushBlocking (b , numProducers , numConsumers )
200
- })
251
+ testCases := []struct {
252
+ name string
253
+ cb cb.Queue [int ]
254
+ }{
255
+ {name : "CircBuffer" , cb : cb.New [int ](capacity )},
256
+ {name : "Channel" , cb : cb.NewC [int ](capacity )},
257
+ }
258
+ for _ , tc := range testCases {
259
+ b .Run (tc .name , func (bb * testing.B ) {
260
+ benchmarkPushBlocking (bb , tc .cb , numProducers , numConsumers )
261
+ })
262
+ }
201
263
}
0 commit comments