-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
274 lines (239 loc) · 6.68 KB
/
generator.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package main
import (
"context"
"errors"
"fmt"
"go.uber.org/zap"
"load-generator/helper"
"load-generator/lib"
"math"
"sync/atomic"
"time"
)
const (
STATUS_INIT uint32 = 0
STATUS_STARTING uint32 = 1
STATUS_STARTED uint32 = 2
STATUS_STOPPING uint32 = 3
STATUS_STOPPED uint32 = 4
CALL_STATUS_INIT = 0
CALL_STATUS_DONE = 1
CALL_STATUS_TIMEOUT = 2
)
type loadGenerator struct {
status uint32
ctx context.Context
ctxCancelFunc context.CancelFunc
pps uint64 // payloads per second
processingDurationNS time.Duration
timeoutDurationNS time.Duration
concurrency uint64
callCount uint64
resultChan chan *lib.CallResult
ticketsImpl lib.GoroutinePoolTickets
callerImpl lib.Caller
}
func (receiver *loadGenerator) init() error {
helper.Logger.Info("Initializing loadGenerator...")
interval := 1e9 / receiver.pps
if interval == 0 {
helper.Logger.Info("Set interval to default value 10")
interval = 10
}
total := uint64(int64(receiver.timeoutDurationNS)/int64(interval) + 1)
if total > math.MaxUint64 {
helper.Logger.Info("Set concurrency to MaxUint64")
total = math.MaxUint64
}
receiver.concurrency = total
tickets, err := lib.NewGoroutinePoolTickets(receiver.concurrency)
if err != nil {
helper.Logger.Error("Create NewGoroutinePoolTickets", zap.String("err", err.Error()))
return err
}
receiver.ticketsImpl = tickets
helper.Logger.Info("loadGenerator inited")
return nil
}
func (receiver *loadGenerator) callOne(rawReq *lib.RawRequest) *lib.RawResponse {
atomic.AddUint64(&receiver.callCount, 1)
if rawReq == nil {
helper.Logger.Warn("rawReq is nil")
return &lib.RawResponse{ID: -1, Err: errors.New("Invalid raw request")}
}
var rawResp *lib.RawResponse
startTime := time.Now().UnixNano()
resp, err := receiver.callerImpl.Call(rawReq.Req, receiver.timeoutDurationNS)
endTime := time.Now().UnixNano()
duration := time.Duration(startTime - endTime)
if err != nil {
errMsg := fmt.Sprintf("Sync CallOne Error: %s.", err)
rawResp = &lib.RawResponse{
ID: rawReq.ID,
Err: errors.New(errMsg),
Elapse: duration,
}
} else {
rawResp = &lib.RawResponse{
ID: rawReq.ID,
Resp: resp,
Elapse: duration,
}
}
return rawResp
}
func (receiver *loadGenerator) asyncCall() {
receiver.ticketsImpl.Take()
go func() {
defer func() {
receiver.ticketsImpl.PutBack()
}()
rawReq := receiver.callerImpl.BuildReq()
var callStatus uint32
timer := time.AfterFunc(receiver.timeoutDurationNS, func() {
if !atomic.CompareAndSwapUint32(&callStatus, CALL_STATUS_INIT, CALL_STATUS_TIMEOUT) {
return
}
result := &lib.CallResult{
ID: rawReq.ID,
Req: rawReq,
Code: lib.RET_CODE_WARNING_TIMEOUT,
Msg: fmt.Sprintf("Timeout! Expected < %v", receiver.timeoutDurationNS),
Elapse: receiver.timeoutDurationNS,
}
receiver.sendResult(result)
})
resp := receiver.callOne(&rawReq)
if !atomic.CompareAndSwapUint32(&callStatus, CALL_STATUS_INIT, CALL_STATUS_DONE) {
return
}
timer.Stop()
var result *lib.CallResult
if resp.Err != nil {
result = &lib.CallResult{
ID: resp.ID,
Req: rawReq,
Code: lib.RET_CODE_ERR_CALL,
Msg: resp.Err.Error(),
Elapse: resp.Elapse,
}
} else {
result = receiver.callerImpl.CheckResp(rawReq, *resp)
result.Elapse = resp.Elapse
}
receiver.sendResult(result)
}()
}
func (receiver *loadGenerator) sendResult(result *lib.CallResult) bool {
if receiver.Status() != STATUS_STARTED {
receiver.printIgnoredResult(result, "load generator stopped")
return false
}
select {
case receiver.resultChan <- result:
return true
default:
receiver.printIgnoredResult(result, "result channel is full")
return false
}
return true
}
func (receiver *loadGenerator) printIgnoredResult(result *lib.CallResult, cause string) {
helper.Logger.Info("Ignored result", zap.Int64("ID", result.ID), zap.Int("Code", int(result.Code)), zap.String("Msg", result.Msg), zap.Duration("Elapse", result.Elapse), zap.String("cause", cause))
}
func (receiver *loadGenerator) prepareToStop(err error) {
helper.Logger.Info("loadGenerator prepareToStop")
atomic.CompareAndSwapUint32(&receiver.status, STATUS_STARTED, STATUS_STOPPING)
close(receiver.resultChan)
atomic.StoreUint32(&receiver.status, STATUS_STOPPED)
}
func (receiver *loadGenerator) genLoad(throttle <-chan time.Time) {
helper.Logger.Info("loadGenerator generating payloads...")
for {
select {
case <-receiver.ctx.Done():
receiver.prepareToStop(receiver.ctx.Err())
return
default:
}
receiver.asyncCall()
if receiver.pps > 0 {
select {
case <-throttle:
case <-receiver.ctx.Done():
receiver.prepareToStop(receiver.ctx.Err())
return
}
}
}
}
func (receiver *loadGenerator) Start() bool {
helper.Logger.Info("loadGenerator Starting...")
if !atomic.CompareAndSwapUint32(&receiver.status, STATUS_INIT, STATUS_STARTING) {
if !atomic.CompareAndSwapUint32(&receiver.status, STATUS_STOPPED, STATUS_STARTING) {
return false
}
}
var throttle <-chan time.Time
if receiver.pps > 0 {
interval := time.Duration(1e9 / receiver.pps)
throttle = time.Tick(interval)
}
receiver.ctx, receiver.ctxCancelFunc = context.WithTimeout(context.Background(), receiver.processingDurationNS)
receiver.callCount = 0
atomic.StoreUint32(&receiver.status, STATUS_STARTED)
go func() {
receiver.genLoad(throttle)
}()
helper.Logger.Info("loadGenerator Started")
return true
}
func (receiver *loadGenerator) Stop() bool {
helper.Logger.Info("loadGenerator Stopping...")
if !atomic.CompareAndSwapUint32(&receiver.status, STATUS_STARTED, STATUS_STOPPING) {
return false
}
receiver.ctxCancelFunc()
for {
if atomic.LoadUint32(&receiver.status) == STATUS_STOPPED {
break
}
time.Sleep(time.Microsecond)
}
helper.Logger.Info("loadGenerator Stopped")
return true
}
func (receiver *loadGenerator) Status() uint32 {
return atomic.LoadUint32(&receiver.status)
}
func (receiver *loadGenerator) CallCount() uint64 {
return atomic.LoadUint64(&receiver.callCount)
}
type Generator interface {
Start() bool
Stop() bool
Status() uint32
CallCount() uint64
}
// NewLoadGenerator ...
func NewLoadGenerator(
params NewLoadGeneratorParams,
) (Generator, error) {
helper.Logger.Info("Constructing NewLoadGenerator...")
if err := params.Check(); err != nil {
return nil, err
}
gen := &loadGenerator{
callerImpl: params.Caller,
pps: params.PPS,
processingDurationNS: params.ProcessingDurationNS,
timeoutDurationNS: params.TimeoutNS,
resultChan: params.ResultChan,
status: STATUS_INIT,
}
if err := gen.init(); err != nil {
return nil, err
}
helper.Logger.Info("NewLoadGenerator constructed")
return gen, nil
}