Skip to content

Commit 618e647

Browse files
authored
Merge pull request #835 from go-redis/fix/sub-benchmarks
Use sub-benchmarks
2 parents 24ba3cf + 92123d4 commit 618e647

File tree

2 files changed

+109
-114
lines changed

2 files changed

+109
-114
lines changed

bench_test.go

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package redis_test
22

33
import (
44
"bytes"
5+
"fmt"
6+
"strings"
57
"testing"
68
"time"
79

@@ -37,23 +39,6 @@ func BenchmarkRedisPing(b *testing.B) {
3739
})
3840
}
3941

40-
func BenchmarkRedisSetString(b *testing.B) {
41-
client := benchmarkRedisClient(10)
42-
defer client.Close()
43-
44-
value := string(bytes.Repeat([]byte{'1'}, 10000))
45-
46-
b.ResetTimer()
47-
48-
b.RunParallel(func(pb *testing.PB) {
49-
for pb.Next() {
50-
if err := client.Set("key", value, 0).Err(); err != nil {
51-
b.Fatal(err)
52-
}
53-
}
54-
})
55-
}
56-
5742
func BenchmarkRedisGetNil(b *testing.B) {
5843
client := benchmarkRedisClient(10)
5944
defer client.Close()
@@ -69,53 +54,46 @@ func BenchmarkRedisGetNil(b *testing.B) {
6954
})
7055
}
7156

72-
func benchmarkSetRedis(b *testing.B, poolSize, payloadSize int) {
73-
client := benchmarkRedisClient(poolSize)
74-
defer client.Close()
75-
76-
value := string(bytes.Repeat([]byte{'1'}, payloadSize))
77-
78-
b.ResetTimer()
79-
80-
b.RunParallel(func(pb *testing.PB) {
81-
for pb.Next() {
82-
if err := client.Set("key", value, 0).Err(); err != nil {
83-
b.Fatal(err)
84-
}
85-
}
86-
})
87-
}
88-
89-
func BenchmarkSetRedis10Conns64Bytes(b *testing.B) {
90-
benchmarkSetRedis(b, 10, 64)
91-
}
92-
93-
func BenchmarkSetRedis100Conns64Bytes(b *testing.B) {
94-
benchmarkSetRedis(b, 100, 64)
95-
}
96-
97-
func BenchmarkSetRedis10Conns1KB(b *testing.B) {
98-
benchmarkSetRedis(b, 10, 1024)
99-
}
100-
101-
func BenchmarkSetRedis100Conns1KB(b *testing.B) {
102-
benchmarkSetRedis(b, 100, 1024)
57+
type setStringBenchmark struct {
58+
poolSize int
59+
valueSize int
10360
}
10461

105-
func BenchmarkSetRedis10Conns10KB(b *testing.B) {
106-
benchmarkSetRedis(b, 10, 10*1024)
62+
func (bm setStringBenchmark) String() string {
63+
return fmt.Sprintf("pool=%d value=%d", bm.poolSize, bm.valueSize)
10764
}
10865

109-
func BenchmarkSetRedis100Conns10KB(b *testing.B) {
110-
benchmarkSetRedis(b, 100, 10*1024)
111-
}
112-
113-
func BenchmarkSetRedis10Conns1MB(b *testing.B) {
114-
benchmarkSetRedis(b, 10, 1024*1024)
115-
}
116-
117-
func BenchmarkSetRedis100Conns1MB(b *testing.B) {
118-
benchmarkSetRedis(b, 100, 1024*1024)
66+
func BenchmarkRedisSetString(b *testing.B) {
67+
benchmarks := []setStringBenchmark{
68+
{10, 64},
69+
{10, 1024},
70+
{10, 64 * 1024},
71+
{10, 1024 * 1024},
72+
73+
{100, 64},
74+
{100, 1024},
75+
{100, 64 * 1024},
76+
{100, 1024 * 1024},
77+
}
78+
for _, bm := range benchmarks {
79+
b.Run(bm.String(), func(b *testing.B) {
80+
client := benchmarkRedisClient(bm.poolSize)
81+
defer client.Close()
82+
83+
value := strings.Repeat("1", bm.valueSize)
84+
85+
b.ResetTimer()
86+
87+
b.RunParallel(func(pb *testing.PB) {
88+
for pb.Next() {
89+
err := client.Set("key", value, 0).Err()
90+
if err != nil {
91+
b.Fatal(err)
92+
}
93+
}
94+
})
95+
})
96+
}
11997
}
12098

12199
func BenchmarkRedisSetGetBytes(b *testing.B) {

internal/pool/bench_test.go

Lines changed: 71 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,93 @@
11
package pool_test
22

33
import (
4+
"fmt"
45
"testing"
56
"time"
67

78
"github.com/go-redis/redis/internal/pool"
89
)
910

10-
func benchmarkPoolGetPut(b *testing.B, poolSize int) {
11-
connPool := pool.NewConnPool(&pool.Options{
12-
Dialer: dummyDialer,
13-
PoolSize: poolSize,
14-
PoolTimeout: time.Second,
15-
IdleTimeout: time.Hour,
16-
IdleCheckFrequency: time.Hour,
17-
})
18-
19-
b.ResetTimer()
20-
21-
b.RunParallel(func(pb *testing.PB) {
22-
for pb.Next() {
23-
cn, err := connPool.Get()
24-
if err != nil {
25-
b.Fatal(err)
26-
}
27-
connPool.Put(cn)
28-
}
29-
})
30-
}
31-
32-
func BenchmarkPoolGetPut10Conns(b *testing.B) {
33-
benchmarkPoolGetPut(b, 10)
11+
type poolGetPutBenchmark struct {
12+
poolSize int
3413
}
3514

36-
func BenchmarkPoolGetPut100Conns(b *testing.B) {
37-
benchmarkPoolGetPut(b, 100)
15+
func (bm poolGetPutBenchmark) String() string {
16+
return fmt.Sprintf("pool=%d", bm.poolSize)
3817
}
3918

40-
func BenchmarkPoolGetPut1000Conns(b *testing.B) {
41-
benchmarkPoolGetPut(b, 1000)
42-
}
43-
44-
func benchmarkPoolGetRemove(b *testing.B, poolSize int) {
45-
connPool := pool.NewConnPool(&pool.Options{
46-
Dialer: dummyDialer,
47-
PoolSize: poolSize,
48-
PoolTimeout: time.Second,
49-
IdleTimeout: time.Hour,
50-
IdleCheckFrequency: time.Hour,
51-
})
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+
})
5237

53-
b.ResetTimer()
38+
b.ResetTimer()
5439

55-
b.RunParallel(func(pb *testing.PB) {
56-
for pb.Next() {
57-
cn, err := connPool.Get()
58-
if err != nil {
59-
b.Fatal(err)
60-
}
61-
connPool.Remove(cn)
62-
}
63-
})
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+
}
6451
}
6552

66-
func BenchmarkPoolGetRemove10Conns(b *testing.B) {
67-
benchmarkPoolGetRemove(b, 10)
53+
type poolGetRemoveBenchmark struct {
54+
poolSize int
6855
}
6956

70-
func BenchmarkPoolGetRemove100Conns(b *testing.B) {
71-
benchmarkPoolGetRemove(b, 100)
57+
func (bm poolGetRemoveBenchmark) String() string {
58+
return fmt.Sprintf("pool=%d", bm.poolSize)
7259
}
7360

74-
func BenchmarkPoolGetRemove1000Conns(b *testing.B) {
75-
benchmarkPoolGetRemove(b, 1000)
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)
89+
}
90+
})
91+
})
92+
}
7693
}

0 commit comments

Comments
 (0)