Add high contention benchmark.

This commit is contained in:
Sam Fredrickson 2023-03-01 19:31:36 -08:00
parent ab364c31bb
commit 5e23a92314
2 changed files with 98 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package priorityq_test
import (
"math/rand"
"runtime"
"sync"
"testing"
@ -177,3 +178,38 @@ func BenchmarkConcSendRecv(b *testing.B) {
close(start)
wg.Wait()
}
func BenchmarkHighContention(b *testing.B) {
q := priorityq.Make[int, int](b.N)
var wg sync.WaitGroup
start := make(chan struct{})
done := make(chan struct{})
numProducers := runtime.NumCPU()
sendsPerProducer := b.N / numProducers
wg.Add(numProducers)
for i := 0; i < numProducers; i++ {
go func() {
ps := make([]int, sendsPerProducer)
for i := 0; i < sendsPerProducer; i++ {
ps[i] = rand.Int()
}
<-start
for i := 0; i < sendsPerProducer; i++ {
q.Send(ps[i], 1)
}
wg.Done()
}()
}
go func() {
ok := true
for ok {
_, _, ok = q.Recv()
}
close(done)
}()
b.ResetTimer()
close(start)
wg.Wait()
q.Close()
<-done
}

View File

@ -2,6 +2,7 @@ package mq_test
import (
"math/rand"
"runtime"
"sync"
"testing"
@ -226,3 +227,64 @@ func BenchmarkConcSendRecvChan(b *testing.B) {
close(start)
wg.Wait()
}
func BenchmarkHighContention(b *testing.B) {
q := mq.Make[int](b.N)
var wg sync.WaitGroup
start := make(chan struct{})
done := make(chan struct{})
numProducers := runtime.NumCPU()
sendsPerProducer := b.N / numProducers
wg.Add(numProducers)
for i := 0; i < numProducers; i++ {
go func() {
<-start
for i := 0; i < sendsPerProducer; i++ {
q.Send(1)
}
wg.Done()
}()
}
go func() {
ok := true
for ok {
_, ok = q.Recv()
}
close(done)
}()
b.ResetTimer()
close(start)
wg.Wait()
q.Close()
<-done
}
func BenchmarkHighContentionChan(b *testing.B) {
c := make(chan int, b.N)
var wg sync.WaitGroup
start := make(chan struct{})
done := make(chan struct{})
numProducers := runtime.NumCPU()
sendsPerProducer := b.N / numProducers
wg.Add(numProducers)
for i := 0; i < numProducers; i++ {
go func() {
<-start
for i := 0; i < sendsPerProducer; i++ {
c <- 1
}
wg.Done()
}()
}
go func() {
for n := range c {
_ = n
}
close(done)
}()
b.ResetTimer()
close(start)
wg.Wait()
close(c)
<-done
}