/
main.go
254 lines (241 loc) · 7.07 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/gomodule/redigo/redis"
"github.com/google/gops/agent"
redispipeline "github.com/lolmourne/r-pipeline"
rediscli "github.com/lolmourne/r-pipeline/client"
)
func main() {
/*
Warning :
Redis Pipeline is useful for retrieving data or updating data in batch. This library is intended to execute as much commands as possible in single round trip time.
if the pipeline session is timeout then all commands in the session won't get executed. If the timeout is not reached when all commands sent to redis server,
it would wait until the response get returned from redis.
*/
//gops!
if err := agent.Listen(agent.Options{}); err != nil {
log.Fatal(err)
}
log.SetFlags(log.Llongfile | log.Ldate | log.Ltime)
redisHost := "127.0.0.1:30001;127.0.0.1:30002;127.0.0.1:30003;127.0.0.1:30004;127.0.0.1:30005;127.0.0.1:30006"
maxConn := 100
maxIntervalInMs := uint64(10)
maxCommandsBatch := uint64(100)
client := rediscli.New(rediscli.CLUSTER_MODE, redisHost, maxConn,
redis.DialReadTimeout(1*time.Second),
redis.DialWriteTimeout(1*time.Second),
redis.DialConnectTimeout(5*time.Second))
rbc := redispipeline.NewRedisPipeline(client, maxIntervalInMs, maxCommandsBatch)
//NewTicker
ticker := time.NewTicker(1000 * time.Millisecond)
// create term so the app didn't exit
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
for {
select {
case <-ticker.C:
Do(rbc)
case <-term:
fmt.Println("terminate app")
return
}
}
}
func Do(rbc redispipeline.RedisPipeline) {
contextTimeout := 300
var requestTimeout uint64
requests := 3000
redisJobPerRequest := 4
fmt.Println("starting SET session")
now := time.Now()
wg := &sync.WaitGroup{}
requestTimeout = uint64(0)
for x := 1; x <= requests; x++ {
wg.Add(1)
go func(x int) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(contextTimeout)*time.Millisecond)
defer cancel()
var requestTimeoutError error
for y := 1; y <= redisJobPerRequest; y++ {
err := setKeyToRedis(ctx, rbc, fmt.Sprintf("%d%d", x, y))
if err != nil {
requestTimeoutError = err
}
}
if requestTimeoutError != nil {
atomic.AddUint64(&requestTimeout, 1)
}
}(x)
}
wg.Wait()
fmt.Println("timeout requests on SET :", requestTimeout)
fmt.Println(time.Since(now))
fmt.Println("ending SET session")
fmt.Println("starting GET session")
requestTimeout = uint64(0)
for x := 1; x <= requests; x++ {
wg.Add(1)
go func(x int) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(contextTimeout)*time.Millisecond)
defer cancel()
var requestTimeoutError error
for y := 1; y <= redisJobPerRequest; y++ {
err := getKeyFromRedis(ctx, rbc, fmt.Sprintf("%d%d", x, y))
if err != nil {
requestTimeoutError = err
}
}
if requestTimeoutError != nil {
atomic.AddUint64(&requestTimeout, 1)
}
}(x)
}
wg.Wait()
fmt.Println("timeout requests on GET :", requestTimeout)
fmt.Println(time.Since(now))
fmt.Println("ending GET session")
fmt.Println("starting GET FROM SLAVE session")
requestTimeout = uint64(0)
for x := 1; x <= requests; x++ {
wg.Add(1)
go func(x int) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(contextTimeout)*time.Millisecond)
defer cancel()
var requestTimeoutError error
for y := 1; y <= redisJobPerRequest; y++ {
err := getKeyFromRedisSlave(ctx, rbc, fmt.Sprintf("%d%d", x, y))
if err != nil {
requestTimeoutError = err
}
}
if requestTimeoutError != nil {
atomic.AddUint64(&requestTimeout, 1)
}
}(x)
}
wg.Wait()
fmt.Println("timeout requests on GET FROM SLAVE :", requestTimeout)
fmt.Println(time.Since(now))
fmt.Println("ending GET from slave session")
}
func getKeyFromRedis(ctx context.Context, rb redispipeline.RedisPipeline, i string) error {
_, err := rb.NewSession(ctx).
PushCommand("GET", fmt.Sprintf("testA%s", i)).
PushCommand("GET", fmt.Sprintf("testB%s", i)).
PushCommand("GET", fmt.Sprintf("testC%s", i)).
PushCommand("GET", fmt.Sprintf("testD%s", i)).
PushCommand("GET", fmt.Sprintf("testE%s", i)).
Execute()
if err != nil {
return err
}
/*
Need to cast the response and error accordingly in sequential order
*/
// for _, resp := range resps {
// if resp.Err != nil {
// fmt.Println(resp.Err)
// continue
// }
// switch reply := resp.Value.(type) {
// case string:
// fmt.Println("cast to string")
// fmt.Println("expect", i, "get", reply)
// case int64:
// fmt.Println("cast to int")
// fmt.Println("expect", i, "get", reply)
// case float64:
// fmt.Println("cast to float")
// fmt.Println("expect", i, "get", reply)
// case []byte:
// fmt.Println("cast to byte")
// fmt.Println("expect", i, "get", string(reply))
// }
// }
return nil
}
func setKeyToRedis(ctx context.Context, rb redispipeline.RedisPipeline, i string) error {
_, err := rb.NewSession(ctx).
PushCommand("SET", fmt.Sprintf("testA%s", i), i).
PushCommand("SET", fmt.Sprintf("testB%s", i), i).
PushCommand("SET", fmt.Sprintf("testC%s", i), i).
PushCommand("SET", fmt.Sprintf("testD%s", i), i).
PushCommand("SET", fmt.Sprintf("testE%s", i), i).
Execute()
if err != nil {
return err
}
// return nil
/*
Need to cast the response and error accordingly in sequential order
*/
// for _, resp := range resps {
// if resp.Err != nil {
// fmt.Println(resp.Err)
// continue
// }
// switch reply := resp.Value.(type) {
// case string:
// fmt.Println("cast to string")
// fmt.Println("expect", i, "get", reply)
// case int64:
// fmt.Println("cast to int")
// fmt.Println("expect", i, "get", reply)
// case float64:
// fmt.Println("cast to float")
// fmt.Println("expect", i, "get", reply)
// case []byte:
// fmt.Println("cast to byte")
// fmt.Println("expect", i, "get", string(reply))
// }
// }
return nil
}
func getKeyFromRedisSlave(ctx context.Context, rb redispipeline.RedisPipeline, i string) error {
_, err := rb.NewSession(ctx).
PushCommandReadOnly("GET", fmt.Sprintf("testA%s", i)).
PushCommandReadOnly("GET", fmt.Sprintf("testB%s", i)).
PushCommandReadOnly("GET", fmt.Sprintf("testC%s", i)).
PushCommandReadOnly("GET", fmt.Sprintf("testD%s", i)).
PushCommandReadOnly("GET", fmt.Sprintf("testE%s", i)).
Execute()
if err != nil {
return err
}
/*
Need to cast the response and error accordingly in sequential order
*/
// for _, resp := range resps {
// if resp.Err != nil {
// fmt.Println(resp.Err)
// continue
// }
// switch reply := resp.Value.(type) {
// case string:
// fmt.Println("cast to string")
// fmt.Println("expect", i, "get", reply)
// case int64:
// fmt.Println("cast to int")
// fmt.Println("expect", i, "get", reply)
// case float64:
// fmt.Println("cast to float")
// fmt.Println("expect", i, "get", reply)
// case []byte:
// fmt.Println("cast to byte")
// fmt.Println("expect", i, "get", string(reply))
// }
// }
return nil
}