2
0

Better benchmarking

This commit is contained in:
Jack Christensen
2018-12-25 19:54:51 -06:00
parent 590f7e07cb
commit 179bc9b8c7
+56 -58
View File
@@ -299,77 +299,75 @@ func TestPoolAcquireReturnsErrorWhenPoolIsClosed(t *testing.T) {
func BenchmarkPoolAcquireAndRelease(b *testing.B) { func BenchmarkPoolAcquireAndRelease(b *testing.B) {
benchmarks := []struct { benchmarks := []struct {
poolSize int poolSize int
concurrentClientCount int clientCount int
loanDuration time.Duration
}{ }{
// Small pool {8, 1},
{10, 1, 0}, {8, 4},
{10, 5, 0}, {8, 8},
{10, 10, 0}, {8, 16},
{10, 20, 0}, {8, 32},
{10, 1, 1 * time.Millisecond}, {8, 64},
{10, 5, 1 * time.Millisecond}, {8, 64},
{10, 10, 1 * time.Millisecond}, {8, 128},
{10, 20, 1 * time.Millisecond}, {8, 256},
{8, 512},
{8, 1024},
{8, 2048},
// large pool {64, 1},
{100, 1, 0}, {64, 4},
{100, 50, 0}, {64, 8},
{100, 100, 0}, {64, 16},
{100, 200, 0}, {64, 32},
{100, 1, 1 * time.Millisecond}, {64, 64},
{100, 50, 1 * time.Millisecond}, {64, 64},
{100, 100, 1 * time.Millisecond}, {64, 128},
{100, 200, 1 * time.Millisecond}, {64, 256},
{64, 512},
{64, 1024},
{64, 2048},
// huge pool {512, 1},
{1000, 1, 0}, {512, 4},
{1000, 500, 0}, {512, 8},
{1000, 1000, 0}, {512, 16},
{1000, 2000, 0}, {512, 32},
{1000, 1, 1 * time.Millisecond}, {512, 64},
{1000, 500, 1 * time.Millisecond}, {512, 64},
{1000, 1000, 1 * time.Millisecond}, {512, 128},
{1000, 2000, 1 * time.Millisecond}, {512, 256},
{512, 512},
{512, 1024},
{512, 2048},
} }
for _, bm := range benchmarks { for _, bm := range benchmarks {
name := fmt.Sprintf("PoolSize=%d/ConcurrentClientCount=%d/LoanDuration=%v", bm.poolSize, bm.concurrentClientCount, bm.loanDuration) name := fmt.Sprintf("PoolSize=%d/ClientCount=%d", bm.poolSize, bm.clientCount)
createFunc, _ := createCreateResourceFunc()
pool := puddle.NewPool(createFunc, stubCloseRes)
pool.SetMaxSize(bm.poolSize)
acquireAndRelease := func() {
res, err := pool.Acquire(context.Background())
if err != nil {
b.Fatal(err)
}
time.Sleep(bm.loanDuration)
res.Release()
}
b.Run(name, func(b *testing.B) { b.Run(name, func(b *testing.B) {
doneChan := make(chan struct{}) wg := &sync.WaitGroup{}
defer close(doneChan)
for i := 0; i < bm.concurrentClientCount-1; i++ {
go func() {
for {
select {
case <-doneChan:
return
default:
}
acquireAndRelease() createFunc, _ := createCreateResourceFunc()
pool := puddle.NewPool(createFunc, stubCloseRes)
pool.SetMaxSize(bm.poolSize)
for i := 0; i < bm.clientCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < b.N; j++ {
res, err := pool.Acquire(context.Background())
if err != nil {
b.Fatal(err)
}
res.Release()
} }
}() }()
} }
for i := 0; i < b.N; i++ { wg.Wait()
acquireAndRelease()
}
}) })
} }
} }