-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf.go
243 lines (211 loc) · 4.45 KB
/
perf.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
package main
import (
"log"
"os"
"time"
"unsafe"
"sync/atomic"
"github.com/garyburd/redigo/redis"
)
// Result ...
type Result struct {
QPS int64
Num int64
Delay int64
Err int64
}
// BucketStatus ...
type BucketStatus struct {
Num int64
Delay int64
Err int64
}
// Perf ...
type Perf struct {
addr string
loop int64
qps int64
connTotalNum int64
needReconnect int64
}
// GetConn ...
func (p *Perf) GetConn() redis.Conn {
reconnect := false
for {
conn, err := redis.Dial("tcp", p.addr, redis.DialConnectTimeout(time.Second))
if err != nil {
if reconnect == false {
reconnect = true
atomic.AddInt64(&p.needReconnect, 1)
}
if atomic.LoadInt64(&p.needReconnect) == p.connTotalNum {
log.Println(err)
os.Exit(1)
}
time.Sleep(time.Millisecond * 10)
continue
}
if reconnect {
atomic.AddInt64(&p.needReconnect, -1)
}
return conn
}
}
// TokenBucketWorker ...
type TokenBucketWorker struct {
id int
token chan int64
bucketStatus unsafe.Pointer
perf *Perf
}
// NewTokenBucketWorker ...
func NewTokenBucketWorker(id int, perf *Perf) (w *TokenBucketWorker) {
w = &TokenBucketWorker{
id: id,
token: make(chan int64, 100),
perf: perf,
bucketStatus: unsafe.Pointer(&BucketStatus{}),
}
tasks := w.LoopWriter()
w.LoopReader(tasks)
return w
}
// GetAndResetBucketStatus ...
func (w *TokenBucketWorker) GetAndResetBucketStatus() (status *BucketStatus) {
return (*BucketStatus)(atomic.SwapPointer(&w.bucketStatus, unsafe.Pointer(&BucketStatus{})))
}
// GetBucketStatus ...
func (w *TokenBucketWorker) GetBucketStatus() (status *BucketStatus) {
return (*BucketStatus)(atomic.LoadPointer(&w.bucketStatus))
}
// LoopWriter ...
func (w *TokenBucketWorker) LoopWriter() (tasks chan *Request) {
tasks = make(chan *Request, 100000)
go func() {
var conn redis.Conn
var integral int64
loop := w.perf.loop
id := w.id
for n := range w.token {
integral += n
for integral > 0 {
if conn == nil || conn.Err() != nil {
conn = w.perf.GetConn()
loop = w.perf.loop
}
rs := AllExecutor.Execute(conn, id)
integral -= int64(len(rs))
for _, r := range rs {
r.Conn = conn
r.RecordStart()
}
if w.perf.loop > 0 {
loop -= int64(len(rs))
if loop <= 0 {
conn = nil
rs[len(rs)-1].Last = true
}
}
for _, r := range rs {
tasks <- r
}
}
}
}()
return tasks
}
// LoopReader ...
func (w *TokenBucketWorker) LoopReader(tasks chan *Request) {
go func() {
for r := range tasks {
reply, err := r.Conn.Receive()
r.Err = r.valid(reply, err)
r.RecordStop()
if r.Last {
r.Conn.Close()
}
status := w.GetBucketStatus()
status.Num++
status.Delay += r.ResponseTime()
if r.Err != nil {
if Conf.Debug {
log.Println(r)
}
status.Err++
}
}
}()
}
// NewPerfGen ...
func NewPerfGen(addr string, qps, num, loop int64) (result chan *Result) {
perf := &Perf{
addr: addr,
loop: loop,
qps: qps,
connTotalNum: num,
}
result = make(chan *Result, 100)
workers := make([]*TokenBucketWorker, num)
for index := range workers {
workers[index] = NewTokenBucketWorker(index, perf)
}
go BucketGenToken(workers, perf)
return GenResult(workers)
}
// BucketGenToken ...
func BucketGenToken(workers []*TokenBucketWorker, perf *Perf) {
t := time.NewTicker(time.Millisecond)
clock := 0
qps := perf.qps
num := perf.connTotalNum
for {
select {
case <-t.C:
allocation := qps / 1000
s0 := allocation / num
s1 := s0 + 1
n0 := s1*num - allocation
n1 := allocation - s0*num
for i := 0; i < int(n1); i++ {
select {
case workers[clock].token <- s1:
}
clock = (clock + 1) % int(num)
}
if s0 > 0 {
for i := 0; i < int(n0); i++ {
select {
case workers[(clock+i)%int(num)].token <- s0:
}
}
}
}
}
}
// GenResult ...
func GenResult(workers []*TokenBucketWorker) (result chan *Result) {
result = make(chan *Result, 100)
t := time.NewTicker(time.Second)
go func() {
for range t.C {
sl := make([]*BucketStatus, len(workers))
for index, worker := range workers {
sl[index] = worker.GetAndResetBucketStatus()
}
r := &Result{}
for _, s := range sl {
r.Delay += s.Delay
r.Err += s.Err
r.Num += s.Num
}
r.QPS = r.Num
if r.Num > 0 {
r.Delay = r.Delay / r.Num
}
select {
case result <- r:
}
}
}()
return result
}