| 1 |
package pool_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
"gopkg.in/redis.v5/internal/pool" |
| 9 |
) |
| 10 |
|
| 11 |
func benchmarkPoolGetPut(b *testing.B, poolSize int) { |
| 12 |
connPool := pool.NewConnPool(dummyDialer, poolSize, time.Second, time.Hour, time.Hour) |
| 13 |
|
| 14 |
b.ResetTimer() |
| 15 |
|
| 16 |
b.RunParallel(func(pb *testing.PB) { |
| 17 |
for pb.Next() { |
| 18 |
cn, _, err := connPool.Get() |
| 19 |
if err != nil { |
| 20 |
b.Fatal(err) |
| 21 |
} |
| 22 |
if err = connPool.Put(cn); err != nil { |
| 23 |
b.Fatal(err) |
| 24 |
} |
| 25 |
} |
| 26 |
}) |
| 27 |
} |
| 28 |
|
| 29 |
func BenchmarkPoolGetPut10Conns(b *testing.B) { |
| 30 |
benchmarkPoolGetPut(b, 10) |
| 31 |
} |
| 32 |
|
| 33 |
func BenchmarkPoolGetPut100Conns(b *testing.B) { |
| 34 |
benchmarkPoolGetPut(b, 100) |
| 35 |
} |
| 36 |
|
| 37 |
func BenchmarkPoolGetPut1000Conns(b *testing.B) { |
| 38 |
benchmarkPoolGetPut(b, 1000) |
| 39 |
} |
| 40 |
|
| 41 |
func benchmarkPoolGetRemove(b *testing.B, poolSize int) { |
| 42 |
connPool := pool.NewConnPool(dummyDialer, poolSize, time.Second, time.Hour, time.Hour) |
| 43 |
removeReason := errors.New("benchmark") |
| 44 |
|
| 45 |
b.ResetTimer() |
| 46 |
|
| 47 |
b.RunParallel(func(pb *testing.PB) { |
| 48 |
for pb.Next() { |
| 49 |
cn, _, err := connPool.Get() |
| 50 |
if err != nil { |
| 51 |
b.Fatal(err) |
| 52 |
} |
| 53 |
if err := connPool.Remove(cn, removeReason); err != nil { |
| 54 |
b.Fatal(err) |
| 55 |
} |
| 56 |
} |
| 57 |
}) |
| 58 |
} |
| 59 |
|
| 60 |
func BenchmarkPoolGetRemove10Conns(b *testing.B) { |
| 61 |
benchmarkPoolGetRemove(b, 10) |
| 62 |
} |
| 63 |
|
| 64 |
func BenchmarkPoolGetRemove100Conns(b *testing.B) { |
| 65 |
benchmarkPoolGetRemove(b, 100) |
| 66 |
} |
| 67 |
|
| 68 |
func BenchmarkPoolGetRemove1000Conns(b *testing.B) { |
| 69 |
benchmarkPoolGetRemove(b, 1000) |
| 70 |
} |