File tree Expand file tree Collapse file tree 2 files changed +40
-14
lines changed Expand file tree Collapse file tree 2 files changed +40
-14
lines changed Original file line number Diff line number Diff line change @@ -87,7 +87,6 @@ func (rb *RingBuffer) Offer(item interface{}) (bool, error) {
87
87
func (rb * RingBuffer ) put (item interface {}, offer bool ) (bool , error ) {
88
88
var n * node
89
89
pos := atomic .LoadUint64 (& rb .queue )
90
- i := 0
91
90
L:
92
91
for {
93
92
if atomic .LoadUint64 (& rb .disposed ) == 1 {
111
110
return false , nil
112
111
}
113
112
114
- if i == 10000 {
115
- runtime .Gosched () // free up the cpu before the next iteration
116
- i = 0
117
- } else {
118
- i ++
119
- }
113
+ runtime .Gosched () // free up the cpu before the next iteration
120
114
}
121
115
122
116
n .data = item
@@ -141,7 +135,6 @@ func (rb *RingBuffer) Poll(timeout time.Duration) (interface{}, error) {
141
135
var (
142
136
n * node
143
137
pos = atomic .LoadUint64 (& rb .dequeue )
144
- i = 0
145
138
start time.Time
146
139
)
147
140
if timeout > 0 {
170
163
return nil , ErrTimeout
171
164
}
172
165
173
- if i == 10000 {
174
- runtime .Gosched () // free up the cpu before the next iteration
175
- i = 0
176
- } else {
177
- i ++
178
- }
166
+ runtime .Gosched () // free up the cpu before the next iteration
179
167
}
180
168
data := n .data
181
169
n .data = nil
Original file line number Diff line number Diff line change @@ -328,6 +328,44 @@ func BenchmarkRBLifeCycle(b *testing.B) {
328
328
wg .Wait ()
329
329
}
330
330
331
+ func BenchmarkRBLifeCycleContention (b * testing.B ) {
332
+ rb := NewRingBuffer (64 )
333
+
334
+ var wwg sync.WaitGroup
335
+ var rwg sync.WaitGroup
336
+ wwg .Add (10 )
337
+ rwg .Add (10 )
338
+
339
+ for i := 0 ; i < 10 ; i ++ {
340
+ go func () {
341
+ for {
342
+ _ , err := rb .Get ()
343
+ if err == ErrDisposed {
344
+ rwg .Done ()
345
+ return
346
+ } else {
347
+ assert .Nil (b , err )
348
+ }
349
+ }
350
+ }()
351
+ }
352
+
353
+ b .ResetTimer ()
354
+
355
+ for i := 0 ; i < 10 ; i ++ {
356
+ go func () {
357
+ for j := 0 ; j < b .N ; j ++ {
358
+ rb .Put (j )
359
+ }
360
+ wwg .Done ()
361
+ }()
362
+ }
363
+
364
+ wwg .Wait ()
365
+ rb .Dispose ()
366
+ rwg .Wait ()
367
+ }
368
+
331
369
func BenchmarkRBPut (b * testing.B ) {
332
370
rb := NewRingBuffer (uint64 (b .N ))
333
371
You can’t perform that action at this time.
0 commit comments