-
Notifications
You must be signed in to change notification settings - Fork 3
/
redis_test.go
75 lines (64 loc) · 1.15 KB
/
redis_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package idbenchmark_test
import (
"log"
"testing"
"github.com/go-redis/redis"
)
const (
redisKey = idbenchmarkKey
)
func redisConnect() (db *redis.Client, err error) {
db = redis.NewClient(&redis.Options{
Addr: ":6379",
PoolSize: 10,
})
_, err = db.Ping().Result()
if err != nil {
log.Printf("Sequence redis open error: %v", err)
return nil, err
}
return db, nil
}
func runRedis(b *testing.B, db *redis.Client) {
var id uint64
for n := 0; n < b.N; n++ {
err := db.Incr(redisKey).Err()
if err != nil {
log.Printf("Sequence redis incr error: %v", err)
break
}
id, err = db.Get(redisKey).Uint64()
if err != nil {
log.Printf("Sequence redis get error: %v", err)
break
}
if id == 0 {
log.Printf("id=0")
break
}
}
}
func BenchmarkRedis(b *testing.B) {
db, err := redisConnect()
if err != nil {
return
}
defer db.Close()
b.ResetTimer()
runRedis(b, db)
b.StopTimer()
}
func BenchmarkRedisParallel(b *testing.B) {
db, err := redisConnect()
if err != nil {
return
}
defer db.Close()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
runRedis(b, db)
}
})
b.StopTimer()
}