github.com/loov/combiner@v0.1.0/testsuite/benchmarks.go (about)

     1  package testsuite
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  func RunBenchmarks(b *testing.B, setup *Setup) {
     9  	b.Helper()
    10  	setup.Bench(b, "Sum", benchSum)
    11  }
    12  
    13  func benchSum(b *testing.B, setup *Setup) {
    14  	const N = 100
    15  
    16  	_, combiner := setup.Make()
    17  	defer StartClose(combiner)()
    18  
    19  	b.ResetTimer()
    20  
    21  	var wg sync.WaitGroup
    22  	wg.Add(setup.Procs)
    23  
    24  	left := b.N
    25  	for i := 0; i < setup.Procs; i++ {
    26  		chunk := left / (setup.Procs - i)
    27  		go func(n int) {
    28  			for i := 0; i < n; i++ {
    29  				combiner.Do(int64(1))
    30  			}
    31  			wg.Done()
    32  		}(chunk)
    33  		left -= chunk
    34  	}
    35  
    36  	wg.Wait()
    37  }