2
0

More benchmarks

This commit is contained in:
Jack Christensen
2018-12-24 13:08:32 -06:00
parent 1396b8ec46
commit 98fd1f3693
+49 -21
View File
@@ -3,6 +3,7 @@ package puddle_test
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"sync" "sync"
"testing" "testing"
"time" "time"
@@ -489,30 +490,63 @@ func TestPoolReturnClosesResourcePoolIsAlreadyClosedErrorIsReported(t *testing.T
} }
} }
func BenchmarkPoolGetAndReturnNoContention(b *testing.B) { func BenchmarkPoolGetAndReturn(b *testing.B) {
benchmarks := []struct {
poolSize int
concurrentClientCount int
loanDuration time.Duration
}{
// Small pool
{10, 1, 0},
{10, 5, 0},
{10, 10, 0},
{10, 20, 0},
{10, 1, 1 * time.Millisecond},
{10, 5, 1 * time.Millisecond},
{10, 10, 1 * time.Millisecond},
{10, 20, 1 * time.Millisecond},
// large pool
{100, 1, 0},
{100, 50, 0},
{100, 100, 0},
{100, 200, 0},
{100, 1, 1 * time.Millisecond},
{100, 50, 1 * time.Millisecond},
{100, 100, 1 * time.Millisecond},
{100, 200, 1 * time.Millisecond},
// huge pool
{1000, 1, 0},
{1000, 500, 0},
{1000, 1000, 0},
{1000, 2000, 0},
{1000, 1, 1 * time.Millisecond},
{1000, 500, 1 * time.Millisecond},
{1000, 1000, 1 * time.Millisecond},
{1000, 2000, 1 * time.Millisecond},
}
for _, bm := range benchmarks {
name := fmt.Sprintf("PoolSize=%d/ConcurrentClientCount=%d/LoanDuration=%v", bm.poolSize, bm.concurrentClientCount, bm.loanDuration)
createFunc, _ := createCreateResourceFunc() createFunc, _ := createCreateResourceFunc()
pool := puddle.NewPool(createFunc, stubCloseRes) pool := puddle.NewPool(createFunc, stubCloseRes)
pool.SetMaxSize(bm.poolSize)
for i := 0; i < b.N; i++ { borrowAndReturn := func() {
res, err := pool.Get(context.Background()) res, err := pool.Get(context.Background())
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
time.Sleep(bm.loanDuration)
pool.Return(res) pool.Return(res)
} }
}
func BenchmarkPoolGetAndReturnHeavyContention(b *testing.B) {
poolSize := 8
contentionClients := 15
createFunc, _ := createCreateResourceFunc()
pool := puddle.NewPool(createFunc, stubCloseRes)
pool.SetMaxSize(poolSize)
b.Run(name, func(b *testing.B) {
doneChan := make(chan struct{}) doneChan := make(chan struct{})
defer close(doneChan) defer close(doneChan)
for i := 0; i < contentionClients; i++ { for i := 0; i < bm.concurrentClientCount-1; i++ {
go func() { go func() {
for { for {
select { select {
@@ -521,20 +555,14 @@ func BenchmarkPoolGetAndReturnHeavyContention(b *testing.B) {
default: default:
} }
res, err := pool.Get(context.Background()) borrowAndReturn()
if err != nil {
b.Fatal(err)
}
pool.Return(res)
} }
}() }()
} }
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
res, err := pool.Get(context.Background()) borrowAndReturn()
if err != nil {
b.Fatal(err)
} }
pool.Return(res) })
} }
} }