Skip to content

Instantly share code, notes, and snippets.

@blanchonvincent
Created July 12, 2019 14:17
Show Gist options
  • Save blanchonvincent/ce180b8c032f59dfb21179a5c36d6b91 to your computer and use it in GitHub Desktop.
Save blanchonvincent/ce180b8c032f59dfb21179a5c36d6b91 to your computer and use it in GitHub Desktop.
Medium - Channel - Buffer Size Benchmark
package bench
import (
"sync"
"sync/atomic"
"testing"
)
func BenchmarkWithNoBuffer(b *testing.B) {
benchmarkWithBuffer(b, 0)
}
func BenchmarkWithBufferSizeOf1(b *testing.B) {
benchmarkWithBuffer(b, 1)
}
func BenchmarkWithBufferSizeEqualsToNumberOfWorker(b *testing.B) {
benchmarkWithBuffer(b, 5)
}
func BenchmarkWithBufferSizeExceedsNumberOfWorker(b *testing.B) {
benchmarkWithBuffer(b, 25)
}
func benchmarkWithBuffer(b *testing.B, size int) {
for i := 0; i < b.N; i++ {
c := make(chan uint32, size)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for i := uint32(0); i < 1000; i++ {
c <- i%2
}
close(c)
}()
var total uint32
for w := 0; w < 5; w++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
v, ok := <-c
if !ok {
break
}
atomic.AddUint32(&total, v)
}
}()
}
wg.Wait()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment