| 1 |
package redis_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
"gopkg.in/redis.v5" |
| 9 |
) |
| 10 |
|
| 11 |
func benchmarkRedisClient(poolSize int) *redis.Client { |
| 12 |
client := redis.NewClient(&redis.Options{ |
| 13 |
Addr: ":6379", |
| 14 |
DialTimeout: time.Second, |
| 15 |
ReadTimeout: time.Second, |
| 16 |
WriteTimeout: time.Second, |
| 17 |
PoolSize: poolSize, |
| 18 |
}) |
| 19 |
if err := client.FlushDb().Err(); err != nil { |
| 20 |
panic(err) |
| 21 |
} |
| 22 |
return client |
| 23 |
} |
| 24 |
|
| 25 |
func BenchmarkRedisPing(b *testing.B) { |
| 26 |
client := benchmarkRedisClient(10) |
| 27 |
defer client.Close() |
| 28 |
|
| 29 |
b.ResetTimer() |
| 30 |
|
| 31 |
b.RunParallel(func(pb *testing.PB) { |
| 32 |
for pb.Next() { |
| 33 |
if err := client.Ping().Err(); err != nil { |
| 34 |
b.Fatal(err) |
| 35 |
} |
| 36 |
} |
| 37 |
}) |
| 38 |
} |
| 39 |
|
| 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 |
|
| 57 |
func BenchmarkRedisGetNil(b *testing.B) { |
| 58 |
client := benchmarkRedisClient(10) |
| 59 |
defer client.Close() |
| 60 |
|
| 61 |
b.ResetTimer() |
| 62 |
|
| 63 |
b.RunParallel(func(pb *testing.PB) { |
| 64 |
for pb.Next() { |
| 65 |
if err := client.Get("key").Err(); err != redis.Nil { |
| 66 |
b.Fatal(err) |
| 67 |
} |
| 68 |
} |
| 69 |
}) |
| 70 |
} |
| 71 |
|
| 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) |
| 103 |
} |
| 104 |
|
| 105 |
func BenchmarkSetRedis10Conns10KB(b *testing.B) { |
| 106 |
benchmarkSetRedis(b, 10, 10*1024) |
| 107 |
} |
| 108 |
|
| 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) |
| 119 |
} |
| 120 |
|
| 121 |
func BenchmarkRedisSetGetBytes(b *testing.B) { |
| 122 |
client := benchmarkRedisClient(10) |
| 123 |
defer client.Close() |
| 124 |
|
| 125 |
value := bytes.Repeat([]byte{'1'}, 10000) |
| 126 |
|
| 127 |
b.ResetTimer() |
| 128 |
|
| 129 |
b.RunParallel(func(pb *testing.PB) { |
| 130 |
for pb.Next() { |
| 131 |
if err := client.Set("key", value, 0).Err(); err != nil { |
| 132 |
b.Fatal(err) |
| 133 |
} |
| 134 |
|
| 135 |
got, err := client.Get("key").Bytes() |
| 136 |
if err != nil { |
| 137 |
b.Fatal(err) |
| 138 |
} |
| 139 |
if !bytes.Equal(got, value) { |
| 140 |
b.Fatalf("got != value") |
| 141 |
} |
| 142 |
} |
| 143 |
}) |
| 144 |
} |
| 145 |
|
| 146 |
func BenchmarkRedisMGet(b *testing.B) { |
| 147 |
client := benchmarkRedisClient(10) |
| 148 |
defer client.Close() |
| 149 |
|
| 150 |
if err := client.MSet("key1", "hello1", "key2", "hello2").Err(); err != nil { |
| 151 |
b.Fatal(err) |
| 152 |
} |
| 153 |
|
| 154 |
b.ResetTimer() |
| 155 |
|
| 156 |
b.RunParallel(func(pb *testing.PB) { |
| 157 |
for pb.Next() { |
| 158 |
if err := client.MGet("key1", "key2").Err(); err != nil { |
| 159 |
b.Fatal(err) |
| 160 |
} |
| 161 |
} |
| 162 |
}) |
| 163 |
} |
| 164 |
|
| 165 |
func BenchmarkSetExpire(b *testing.B) { |
| 166 |
client := benchmarkRedisClient(10) |
| 167 |
defer client.Close() |
| 168 |
|
| 169 |
b.ResetTimer() |
| 170 |
|
| 171 |
b.RunParallel(func(pb *testing.PB) { |
| 172 |
for pb.Next() { |
| 173 |
if err := client.Set("key", "hello", 0).Err(); err != nil { |
| 174 |
b.Fatal(err) |
| 175 |
} |
| 176 |
if err := client.Expire("key", time.Second).Err(); err != nil { |
| 177 |
b.Fatal(err) |
| 178 |
} |
| 179 |
} |
| 180 |
}) |
| 181 |
} |
| 182 |
|
| 183 |
func BenchmarkPipeline(b *testing.B) { |
| 184 |
client := benchmarkRedisClient(10) |
| 185 |
defer client.Close() |
| 186 |
|
| 187 |
b.ResetTimer() |
| 188 |
|
| 189 |
b.RunParallel(func(pb *testing.PB) { |
| 190 |
for pb.Next() { |
| 191 |
_, err := client.Pipelined(func(pipe *redis.Pipeline) error { |
| 192 |
pipe.Set("key", "hello", 0) |
| 193 |
pipe.Expire("key", time.Second) |
| 194 |
return nil |
| 195 |
}) |
| 196 |
if err != nil { |
| 197 |
b.Fatal(err) |
| 198 |
} |
| 199 |
} |
| 200 |
}) |
| 201 |
} |
| 202 |
|
| 203 |
func BenchmarkZAdd(b *testing.B) { |
| 204 |
client := benchmarkRedisClient(10) |
| 205 |
defer client.Close() |
| 206 |
|
| 207 |
b.ResetTimer() |
| 208 |
|
| 209 |
b.RunParallel(func(pb *testing.PB) { |
| 210 |
for pb.Next() { |
| 211 |
if err := client.ZAdd("key", redis.Z{float64(1), "hello"}).Err(); err != nil { |
| 212 |
b.Fatal(err) |
| 213 |
} |
| 214 |
} |
| 215 |
}) |
| 216 |
} |