blob: f2783cafc4391443793ec43b57b177f3f78f3c42 [file] [log] [blame]
package benchmarks
import (
"runtime"
"testing"
)
func BenchmarkBlockingOneGoroutine(b *testing.B) {
runtime.GOMAXPROCS(1)
benchmarkBlocking(b, 1)
}
func BenchmarkBlockingTwoGoroutines(b *testing.B) {
runtime.GOMAXPROCS(2)
benchmarkBlocking(b, 1)
runtime.GOMAXPROCS(1)
}
func BenchmarkBlockingThreeGoroutinesWithContendedWrite(b *testing.B) {
runtime.GOMAXPROCS(3)
benchmarkBlocking(b, 2)
runtime.GOMAXPROCS(1)
}
func benchmarkBlocking(b *testing.B, writers int64) {
iterations := int64(b.N)
channel := make(chan int64, 1024*16)
b.ReportAllocs()
b.ResetTimer()
for x := int64(0); x < writers; x++ {
go func() {
for i := int64(0); i < iterations; i++ {
channel <- i
}
}()
}
for i := int64(0); i < iterations*writers; i++ {
msg := <-channel
if writers == 1 && msg != i {
panic("Out of sequence")
}
}
}