Skip to content

Commit

Permalink
errCh for benchmark goroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
alrs committed Apr 4, 2023
1 parent adca7e3 commit 1ba0e51
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"math/rand"
"strconv"
"sync"
"testing"
)

Expand Down Expand Up @@ -93,21 +92,28 @@ func BenchmarkLoadingCacheCapped_MultiThread(b *testing.B) {
func(key interface{}) (interface{}, error) {
return key, nil
}, benchLoadingCacheCap)
wg := sync.WaitGroup{}
wg.Add(benchLoadingCacheConcurrency)
errCh := make(chan error, benchLoadingCacheConcurrency)
defer close(errCh)
for i := 0; i < benchLoadingCacheConcurrency; i++ {
go func() {
for j := 0; j < b.N; j++ {
k := rand.Intn(benchLoadingCacheKeyRange)
v, _ := c.get(k)
if k != v {
b.FailNow()
errCh <- fmt.Errorf("key:%q != value:%q", k, v)
return
}
}
defer wg.Done()
errCh <- nil
return
}()
}
wg.Wait()
for i := 0; i < benchLoadingCacheConcurrency; i++ {
if err := <-errCh; err != nil {
b.Fatal(err)
}
}

b.Logf("N=%d, concurrency=%d, reset=%d", b.N, benchLoadingCacheConcurrency, c.reset)
}

Expand All @@ -133,21 +139,27 @@ func BenchmarkLoadingCacheNoCap_MultiThread(b *testing.B) {
func(key interface{}) (interface{}, error) {
return key, nil
}, 0) // 0 => no cap
wg := sync.WaitGroup{}
wg.Add(benchLoadingCacheConcurrency)
errCh := make(chan error, benchLoadingCacheConcurrency)
defer close(errCh)
for i := 0; i < benchLoadingCacheConcurrency; i++ {
go func() {
for j := 0; j < b.N; j++ {
k := rand.Intn(benchLoadingCacheKeyRange)
v, _ := c.get(k)
if k != v {
b.FailNow()
errCh <- fmt.Errorf("key:%q != value:%q", k, v)
return
}
}
defer wg.Done()
errCh <- nil
return
}()
}
wg.Wait()
for i := 0; i < benchLoadingCacheConcurrency; i++ {
if err := <-errCh; err != nil {
b.Fatal(err)
}
}
b.Logf("N=%d, concurrency=%d, reset=%d", b.N, benchLoadingCacheConcurrency, c.reset)
}

Expand Down

0 comments on commit 1ba0e51

Please sign in to comment.