-
Notifications
You must be signed in to change notification settings - Fork 3
/
spsc_test.go
65 lines (55 loc) · 1.54 KB
/
spsc_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
// Copyright 2022 tink <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package ringbuffer
import (
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSPSCRingBuffer(t *testing.T) {
cap := 16
spsc := NewSpscRingBuffer(cap)
assert.Equal(t, spsc.Length(), 0)
assert.Equal(t, spsc.Capacity(), cap)
for i := 0; i < cap*2; i++ {
err := spsc.Enqueue(i)
if i < cap {
assert.ErrorIs(t, err, nil)
} else {
assert.ErrorIs(t, err, ErrIsFull)
}
}
assert.Equal(t, spsc.Length(), cap)
assert.Equal(t, spsc.Capacity(), cap)
for i := 0; i < cap*2; i++ {
_, err := spsc.Dequeue()
if i < cap {
assert.ErrorIs(t, err, nil)
} else {
assert.ErrorIs(t, err, ErrIsEmpty)
}
}
assert.Equal(t, spsc.Length(), 0)
assert.Equal(t, spsc.Capacity(), cap)
}
func BenchmarkRingSPSC(b *testing.B) {
b.Run("1P1C", func(b *testing.B) {
benchmarkRingBuffer(b, NewSpscRingBuffer(8192), 100, false, false, doneEnv())
})
b.Run("1P1C_1CPU", func(b *testing.B) {
pp := runtime.GOMAXPROCS(1)
benchmarkRingBuffer(b, NewSpscRingBuffer(8192), 4, false, false, doneEnv())
runtime.GOMAXPROCS(pp)
})
}
func BenchmarkChanSPSC(b *testing.B) {
b.Run("1P1C", func(b *testing.B) {
benchmarkRingBuffer(b, newChanRingBuffer(8192), 100, false, false, doneEnv())
})
b.Run("1P1C_1CPU", func(b *testing.B) {
pp := runtime.GOMAXPROCS(1)
benchmarkRingBuffer(b, newChanRingBuffer(8192), 4, false, false, doneEnv())
runtime.GOMAXPROCS(pp)
})
}