Skip to content

Commit 79bb398

Browse files
committed
add a benchmark for ring buffer with 2 read and 1 writer with pinned CPU
1 parent 06befd7 commit 79bb398

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

internal/ringbuffer_benchmark_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package pkg
22

33
import (
4+
"runtime"
45
"sync"
56
"testing"
7+
8+
unix "golang.org/x/sys/unix"
69
)
710

811
func BenchmarkGet(b *testing.B) {
@@ -76,3 +79,64 @@ func BenchmarkRingBuffer(b *testing.B) {
7679

7780
wg.Wait()
7881
}
82+
83+
func BenchmarkRingBufferPinCPU(b *testing.B) {
84+
if runtime.NumCPU() < 4 {
85+
panic("need 4 CPU cores for benchmark !")
86+
}
87+
var cpuset1, cpuset2, cpuset3 = unix.CPUSet{}, unix.CPUSet{}, unix.CPUSet{}
88+
cpuset1.Set(1)
89+
cpuset2.Set(2)
90+
cpuset3.Set(3)
91+
92+
iterations := b.N
93+
seqcer := NewSequencer(1024)
94+
95+
seq1 := NewSequence()
96+
seq2 := NewSequence()
97+
98+
seqcer.addGatingSequences(&seq1)
99+
seqcer.addGatingSequences(&seq2)
100+
101+
ring, _ := NewRingBuffer[int64](1024, seqcer)
102+
103+
wg := sync.WaitGroup{}
104+
wg.Add(3)
105+
106+
reader1 := func() {
107+
runtime.LockOSThread()
108+
unix.SchedSetaffinity(0, &cpuset1)
109+
defer runtime.UnlockOSThread()
110+
defer wg.Done()
111+
for i := 0; i < iterations; i++ {
112+
ring.Get(int64(i))
113+
seq1.Set(int64(i))
114+
}
115+
}
116+
reader2 := func() {
117+
runtime.LockOSThread()
118+
unix.SchedSetaffinity(0, &cpuset2)
119+
defer runtime.UnlockOSThread()
120+
defer wg.Done()
121+
for i := 0; i < iterations; i++ {
122+
ring.Get(int64(i))
123+
seq2.Set(int64(i))
124+
}
125+
}
126+
writer := func() {
127+
runtime.LockOSThread()
128+
unix.SchedSetaffinity(0, &cpuset3)
129+
defer runtime.UnlockOSThread()
130+
defer wg.Done()
131+
for i := 0; i < iterations; i++ {
132+
ring.Get(int64(i))
133+
ring.Publish(int64(i))
134+
}
135+
}
136+
137+
go reader1()
138+
go reader2()
139+
go writer()
140+
141+
wg.Wait()
142+
}

0 commit comments

Comments
 (0)