forked from jacexh/ultron
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstrategy.go
393 lines (336 loc) · 9.36 KB
/
strategy.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package ultron
import (
"context"
"encoding/json"
"errors"
"fmt"
"runtime"
"runtime/debug"
"sync"
"sync/atomic"
"time"
"github.com/wosai/ultron/v2/pkg/genproto"
"github.com/wosai/ultron/v2/pkg/statistics"
"go.uber.org/zap"
)
type (
// AttackStrategyCommander 压测策略
AttackStrategyCommander interface {
Open(context.Context, Task) <-chan statistics.AttackResult
Command(AttackStrategy, Timer)
ConcurrentUsers() int
Close()
}
// AttackStrategy 压测策略描述
AttackStrategy interface {
Spawn() []*RampUpStep
Switch(next AttackStrategy) []*RampUpStep
Split(int) []AttackStrategy
Name() string
}
// RampUpStep 增/降压描述
RampUpStep struct {
N int // 增、降的数量,>0 为加压, <0为降压
Interval time.Duration // 间隔时间
}
// FixedConcurrentUsers 固定goroutine/线程/用户的并发策略
FixedConcurrentUsers struct {
ConcurrentUsers int `json:"concurrent_users"` // 并发用户数
RampUpPeriod int `json:"ramp_up_period,omitempty"` // 增压周期时长
}
attackStrategyConverter struct {
convertDTOFunc map[string]convertAttackStrategyDTOFunc
}
convertAttackStrategyDTOFunc func([]byte) (AttackStrategy, error)
fixedConcurrentUsersStrategyCommander struct {
ctx context.Context
cancel context.CancelFunc
describer AttackStrategy
output chan statistics.AttackResult
timer Timer
task Task
counter uint32
pool map[uint32]*fcuExecutor
closed uint32
inRampUpPeriod uint32
wg sync.WaitGroup
mu sync.Mutex
}
// fcuExecutor FixedConcurrentUsers策略的执行者
fcuExecutor struct {
id uint32
cancel context.CancelFunc
timer Timer
mu sync.RWMutex
}
commanderFactory struct{}
)
var (
_ AttackStrategy = (*FixedConcurrentUsers)(nil)
_ AttackStrategyCommander = (*fixedConcurrentUsersStrategyCommander)(nil)
)
var defaultAttackStrategyConverter *attackStrategyConverter
var defaultCommanderFactory = commanderFactory{}
func (fc *FixedConcurrentUsers) spawn(current, expected, period, interval int) []*RampUpStep {
var ret []*RampUpStep
if current == expected {
return ret
}
if period < interval {
period = interval
}
var steps int
var nPerStep int
for {
steps = period / interval
nPerStep = (expected - current) / steps
if nPerStep != 0 {
break
}
interval++
}
if current < expected {
for current <= expected-nPerStep {
current += nPerStep
ret = append(ret, &RampUpStep{N: nPerStep, Interval: time.Duration(interval) * time.Second})
}
} else {
for current >= expected-nPerStep {
current += nPerStep
ret = append(ret, &RampUpStep{N: nPerStep, Interval: time.Duration(interval) * time.Second})
}
}
if current != expected {
ret[len(ret)-1].N += (expected - current)
}
return ret
}
// Spawn 增压、降压
func (fc *FixedConcurrentUsers) Spawn() []*RampUpStep {
if fc.ConcurrentUsers <= 0 {
panic("the number of concurrent users must be greater than 0")
}
return fc.spawn(0, fc.ConcurrentUsers, fc.RampUpPeriod, 1)
}
// Switch 转入下一个阶段
func (fc *FixedConcurrentUsers) Switch(next AttackStrategy) []*RampUpStep {
n, ok := next.(*FixedConcurrentUsers)
if !ok {
panic("cannot switch to different type of AttackStrategyDescriber")
}
return fc.spawn(fc.ConcurrentUsers, n.ConcurrentUsers, n.RampUpPeriod, 1)
}
// Split 切分配置
func (fx *FixedConcurrentUsers) Split(n int) []AttackStrategy {
if n <= 0 {
panic("bad slices number")
}
ret := make([]AttackStrategy, n)
for i := 0; i < n; i++ {
ret[i] = &FixedConcurrentUsers{
ConcurrentUsers: fx.ConcurrentUsers / n,
RampUpPeriod: fx.RampUpPeriod,
}
}
if remainder := fx.ConcurrentUsers % n; remainder > 0 {
for i := 0; i < remainder; i++ {
ret[i].(*FixedConcurrentUsers).ConcurrentUsers++
}
}
return ret
}
func (fx *FixedConcurrentUsers) Name() string {
return "fixed-concurrent-users"
}
func newAttackStrategyConverter() *attackStrategyConverter {
return &attackStrategyConverter{
convertDTOFunc: map[string]convertAttackStrategyDTOFunc{
"fixed-concurrent-users": func(data []byte) (AttackStrategy, error) {
as := new(FixedConcurrentUsers)
err := json.Unmarshal(data, as)
return as, err
},
},
}
}
func (c *attackStrategyConverter) convertDTO(dto *genproto.AttackStrategyDTO) (AttackStrategy, error) {
fn, ok := c.convertDTOFunc[dto.Type]
if !ok {
return nil, errors.New("cannot found convertion function")
}
return fn(dto.AttackStrategy)
}
func (c *attackStrategyConverter) convertAttackStrategy(as AttackStrategy) (*genproto.AttackStrategyDTO, error) {
data, err := json.Marshal(as)
if err != nil {
return nil, err
}
return &genproto.AttackStrategyDTO{Type: as.Name(), AttackStrategy: data}, nil
}
func newFixedConcurrentUsersStrategyCommander() *fixedConcurrentUsersStrategyCommander {
return &fixedConcurrentUsersStrategyCommander{
ctx: context.TODO(),
output: make(chan statistics.AttackResult, 100),
pool: make(map[uint32]*fcuExecutor),
}
}
func (commander *fixedConcurrentUsersStrategyCommander) clearDeadExector(id uint32) {
commander.mu.Lock()
defer commander.mu.Unlock()
delete(commander.pool, id)
}
func (commander *fixedConcurrentUsersStrategyCommander) Open(ctx context.Context, task Task) <-chan statistics.AttackResult {
commander.ctx, commander.cancel = context.WithCancel(ctx)
commander.task = task
return commander.output
}
func (commander *fixedConcurrentUsersStrategyCommander) Command(d AttackStrategy, t Timer) {
for atomic.LoadUint32(&commander.inRampUpPeriod) == 1 {
runtime.Gosched() // 只且仅有一个增压阶段
}
atomic.CompareAndSwapUint32(&commander.inRampUpPeriod, 0, 1) // 进入该阶段
defer atomic.CompareAndSwapUint32(&commander.inRampUpPeriod, 1, 0)
var rampUpSteps []*RampUpStep
if commander.describer == nil {
rampUpSteps = d.Spawn()
} else {
rampUpSteps = commander.describer.Switch(d)
}
commander.describer = d
if t == nil {
commander.timer = NonstopTimer{}
} else {
commander.timer = t
}
for _, exe := range commander.pool {
exe.renewTimer(commander.timer)
}
killed := 0
spawned := 0
for _, step := range rampUpSteps {
switch {
case step.N < 0: // 降压策略
d := 0
commander.mu.Lock()
for _, e := range commander.pool {
if d > step.N {
delete(commander.pool, e.id) // 主动清理
e.kill()
d--
} else {
break
}
}
commander.mu.Unlock()
killed -= step.N
Logger.Info(fmt.Sprintf("killed %d users in ramp-up peroid", killed))
select {
case <-commander.ctx.Done():
Logger.Warn("commander was canceled, break out the ramp-up period")
return
default:
time.Sleep(step.Interval)
}
case step.N > 0: // 增压策略
for i := 0; i < step.N; i++ {
id := atomic.AddUint32(&commander.counter, 1) - 1
executor := newFCUExecutor(id, commander, t)
select {
case <-commander.ctx.Done():
Logger.Warn("commander was canceled, break out the ramp-up period") // https://pkg.go.dev/sync#WaitGroup.Add
return
default:
commander.wg.Add(1)
commander.mu.Lock()
commander.pool[id] = executor
commander.mu.Unlock()
go func(exe *fcuExecutor) {
defer func() {
commander.clearDeadExector(exe.id)
exe.kill() // 所有清理逻辑
commander.wg.Done()
}()
exe.start(commander.ctx, commander.task, commander.output)
}(executor)
}
}
spawned += step.N
Logger.Info(fmt.Sprintf("spawned %d users in ramp-up period", spawned))
select {
case <-commander.ctx.Done():
Logger.Warn("commander was canceled, break out the ramp-up period")
return
default:
time.Sleep(step.Interval)
}
default:
}
}
}
func (commander *fixedConcurrentUsersStrategyCommander) Close() {
if atomic.CompareAndSwapUint32(&commander.closed, 0, 1) {
commander.cancel()
for atomic.LoadUint32(&commander.inRampUpPeriod) == 1 {
runtime.Gosched()
}
commander.wg.Wait()
close(commander.output)
}
}
func (commander *fixedConcurrentUsersStrategyCommander) ConcurrentUsers() int {
return len(commander.pool)
}
func newFCUExecutor(id uint32, parent *fixedConcurrentUsersStrategyCommander, t Timer) *fcuExecutor {
return &fcuExecutor{
id: id,
timer: t,
}
}
func (e *fcuExecutor) renewTimer(t Timer) {
e.mu.Lock()
defer e.mu.Unlock()
e.timer = t
}
func (e *fcuExecutor) kill() {
e.cancel()
}
func (e *fcuExecutor) start(ctx context.Context, task Task, output chan<- statistics.AttackResult) {
if output == nil {
panic("invalid output channel")
}
ctx, e.cancel = context.WithCancel(ctx)
ctx = newExecutorSharedContext(ctx)
defer func() {
if rec := recover(); rec != nil {
debug.PrintStack()
Logger.DPanic("recovered", zap.Any("panic", rec))
}
}()
for {
select {
case <-ctx.Done():
// Logger.Warn("a executor is quit")
return
default:
}
start := time.Now()
attacker := task.PickUp()
err := attacker.Fire(ctx)
select {
case output <- statistics.AttackResult{Name: attacker.Name(), Duration: time.Since(start), Error: err}:
case <-ctx.Done():
// Logger.Warn("a executor is quit")
return
}
e.mu.RLock()
t := e.timer
e.mu.RUnlock()
t.Sleep()
}
}
func (cf commanderFactory) build(ct string) AttackStrategyCommander {
return newFixedConcurrentUsersStrategyCommander()
}
func init() {
defaultAttackStrategyConverter = newAttackStrategyConverter()
}