Skip to content

Commit

Permalink
Add a very crude comparative benchmark.
Browse files Browse the repository at this point in the history
Roughly compare the performance of accumulating values with a separate
goroutine via a channel, vs. accumulating them directly under a lock.
  • Loading branch information
creachadair committed Mar 19, 2024
1 parent 373d0f1 commit aad792e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package taskgroup_test

import (
"math/rand"
"sync"
"testing"
)

// A very rough benchmark comparing the performance of accumulating values with
// a separate goroutine via a channel, vs. accumulating them directly under a
// lock. The workload here is intentionally minimal, so the benchmark is
// measuring more or less just the overhead.

func BenchmarkChan(b *testing.B) {
ch := make(chan int)
done := make(chan struct{})
var total int
go func() {
defer close(done)
for v := range ch {
total += v
}
}()
b.ResetTimer() // discount the setup time.

var wg sync.WaitGroup
wg.Add(b.N)
for i := 0; i < b.N; i++ {
go func() {
defer wg.Done()
ch <- rand.Intn(1000)
}()
}
wg.Wait()
close(ch)
<-done
}

func BenchmarkLock(b *testing.B) {
var μ sync.Mutex
var total int
report := func(v int) {
μ.Lock()
defer μ.Unlock()
total += v
}
b.ResetTimer() // discount the setup time.

var wg sync.WaitGroup
wg.Add(b.N)
for i := 0; i < b.N; i++ {
go func() {
defer wg.Done()
report(rand.Intn(1000))
}()
}
wg.Wait()
}

0 comments on commit aad792e

Please sign in to comment.