...
1 package pool_test
2
3 import (
4 "fmt"
5 "testing"
6 "time"
7
8 "github.com/go-redis/redis/internal/pool"
9 )
10
11 type poolGetPutBenchmark struct {
12 poolSize int
13 }
14
15 func (bm poolGetPutBenchmark) String() string {
16 return fmt.Sprintf("pool=%d", bm.poolSize)
17 }
18
19 func BenchmarkPoolGetPut(b *testing.B) {
20 benchmarks := []poolGetPutBenchmark{
21 {1},
22 {2},
23 {8},
24 {32},
25 {64},
26 {128},
27 }
28 for _, bm := range benchmarks {
29 b.Run(bm.String(), func(b *testing.B) {
30 connPool := pool.NewConnPool(&pool.Options{
31 Dialer: dummyDialer,
32 PoolSize: bm.poolSize,
33 PoolTimeout: time.Second,
34 IdleTimeout: time.Hour,
35 IdleCheckFrequency: time.Hour,
36 })
37
38 b.ResetTimer()
39
40 b.RunParallel(func(pb *testing.PB) {
41 for pb.Next() {
42 cn, err := connPool.Get()
43 if err != nil {
44 b.Fatal(err)
45 }
46 connPool.Put(cn)
47 }
48 })
49 })
50 }
51 }
52
53 type poolGetRemoveBenchmark struct {
54 poolSize int
55 }
56
57 func (bm poolGetRemoveBenchmark) String() string {
58 return fmt.Sprintf("pool=%d", bm.poolSize)
59 }
60
61 func BenchmarkPoolGetRemove(b *testing.B) {
62 benchmarks := []poolGetRemoveBenchmark{
63 {1},
64 {2},
65 {8},
66 {32},
67 {64},
68 {128},
69 }
70 for _, bm := range benchmarks {
71 b.Run(bm.String(), func(b *testing.B) {
72 connPool := pool.NewConnPool(&pool.Options{
73 Dialer: dummyDialer,
74 PoolSize: bm.poolSize,
75 PoolTimeout: time.Second,
76 IdleTimeout: time.Hour,
77 IdleCheckFrequency: time.Hour,
78 })
79
80 b.ResetTimer()
81
82 b.RunParallel(func(pb *testing.PB) {
83 for pb.Next() {
84 cn, err := connPool.Get()
85 if err != nil {
86 b.Fatal(err)
87 }
88 connPool.Remove(cn, nil)
89 }
90 })
91 })
92 }
93 }
94
View as plain text