-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadtest.go
291 lines (244 loc) · 6.12 KB
/
loadtest.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
package ltt
import (
"context"
"fmt"
"log"
"reflect"
"strings"
"sync"
"time"
)
type loadtestContextKeyType int
var loadtestContextKey loadtestContextKeyType
type StatusType int
var statusTypes = map[StatusType]string{
StatusStopped: "stopped",
StatusSpawning: "spawning",
StatusRunning: "running",
StatusStopping: "stopping",
}
var statusTypesFromString = map[string]StatusType{
"stopped": StatusStopped,
"spawning": StatusSpawning,
"running": StatusRunning,
"stopping": StatusStopping,
}
func (s *StatusType) UnmarshalJSON(bytes []byte) error {
*s = statusTypesFromString[strings.Trim(string(bytes), "\"")]
return nil
}
func (s StatusType) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, statusTypes[s])), nil
}
func (s StatusType) String() string {
return statusTypes[s]
}
const (
StatusStopped StatusType = iota
StatusSpawning
StatusRunning
StatusStopping
)
type TaskRun struct {
Task *Task
Duration time.Duration
Error error
}
type LoadTest struct {
Config Config `json:"config"`
Status StatusType `json:"status"`
// Map of created users
UserMap map[int64]User `json:"-"`
UserMapLock sync.Mutex `json:"-"`
TaskRunChan chan *TaskRun `json:"-"`
Stats *Statistics `json:"stats"`
Log *log.Logger `json:"-"`
// Target number of user to spawn
TargetUserNum int `json:"target_user_num"`
}
func (lt *LoadTest) handleTaskRun(tr *TaskRun) {
// Only collect stats if we're in a clean running state
if lt.Status != StatusRunning {
return
}
name := tr.Task.FullName()
lt.Stats.Lock()
lt.Stats.RPSMap[time.Now().Unix()]++
lt.Stats.NumTotal++
lt.Stats.TotalDuration += tr.Duration.Milliseconds()
if tr.Error != nil {
lt.Stats.NumFailed++
} else {
lt.Stats.NumSuccessful++
}
if _, ok := lt.Stats.Tasks[name]; !ok {
lt.Stats.Tasks[name] = NewTaskStat(name)
}
taskStat := lt.Stats.Tasks[name]
lt.Stats.Unlock()
taskStat.Lock()
durationMS := tr.Duration.Milliseconds()
taskStat.Metrics[durationMS]++
taskStat.TotalRuns++
taskStat.TotalDuration += durationMS
if tr.Error != nil {
taskStat.NumFailed++
taskStat.Errors[tr.Error.Error()]++
} else {
taskStat.NumSuccessful++
}
taskStat.Unlock()
}
func (lt *LoadTest) usersJob(entryTask *Task) {
var lastNRU int
for {
if lastNRU != lt.Stats.RunningUsers {
lt.Stats.Lock()
if lt.Stats.RunningUsers == 0 {
lt.Status = StatusStopped
lt.Stats.EndTime = time.Now()
lt.Log.Printf("All users have been stopped, status changed to stopped\n")
} else if lt.Stats.RunningUsers == lt.TargetUserNum {
lt.Status = StatusRunning
lt.Stats.StartTime = time.Now()
lt.Log.Printf("All %d users have been spawned, status changed to running\n", lt.TargetUserNum)
}
lt.Stats.Unlock()
}
lastNRU = lt.Stats.RunningUsers
lt.UserMapLock.Lock()
numUsers := len(lt.UserMap)
lt.UserMapLock.Unlock()
var diff int
if numUsers > lt.TargetUserNum {
if lt.TargetUserNum == 0 {
lt.Status = StatusStopping
}
diff = numUsers - lt.TargetUserNum
lt.stopUsers(diff)
} else if numUsers < lt.TargetUserNum {
lt.Status = StatusSpawning
diff = lt.TargetUserNum - numUsers
lt.spawnUsers(diff, entryTask)
}
time.Sleep(time.Second)
}
}
func (lt *LoadTest) stopUsers(num int) {
defer lt.UserMapLock.Unlock()
lt.UserMapLock.Lock()
i := 0
for _, u := range lt.UserMap {
if i >= num {
break
}
u.SetStatus(UserStatusStopping)
i++
}
}
func (lt *LoadTest) spawnUsers(num int, entryTask *Task) {
for i := 0; i < num; i++ {
// Create a new User instance
var u User
uv := reflect.ValueOf(lt.Config.UserType)
if !uv.IsValid() {
u = NewDefaultUser(entryTask)
} else {
ok := false
u, ok = reflect.New(uv.Type()).Interface().(User)
if !ok {
lt.Log.Fatalf("failed to cast LoadTest.User to User\n")
}
}
// Each user has their own context
ctx := NewLoadTestContext(context.Background(), lt)
// Save a ref to the user
ctx = NewUserContext(ctx, u)
// Setup the user instance's local storage
ctx = NewStorageContext(ctx, NewStorage())
u.SetID(int64(i))
u.SetContext(ctx)
lt.UserMapLock.Lock()
lt.UserMap[u.ID()] = u
lt.UserMapLock.Unlock()
go func() {
u.SetStatus(UserStatusSpawning)
// Sleep according to user ID to ramp up the user spawns
sleepTime := (int(u.ID()) % lt.TargetUserNum) / lt.Config.NumSpawnPerSecond
if lt.Config.Verbose {
lt.Log.Printf("pre-spawn sleep time: %d for user %d\n", sleepTime, u.ID())
}
u.SleepSeconds(sleepTime)
lt.Stats.Lock()
lt.Stats.RunningUsers++
lt.Stats.Unlock()
u.Spawn()
u.SetStatus(UserStatusRunning)
for u.Status() == UserStatusRunning {
u.Tick()
u.Sleep()
}
lt.Stats.Lock()
lt.Stats.RunningUsers--
lt.Stats.Unlock()
lt.UserMapLock.Lock()
delete(lt.UserMap, u.ID())
lt.UserMapLock.Unlock()
u.SetStatus(UserStatusStopped)
}()
}
}
func (lt *LoadTest) cleanRPSJob() {
for {
lt.Stats.Lock()
lt.Stats.CleanRPSMap()
lt.Stats.Unlock()
time.Sleep(time.Second * 30)
}
}
func (lt *LoadTest) runAPIJob() {
err := RunAPIServer(lt)
if err != nil {
lt.Log.Println("failed to start api server: %s", err.Error())
}
}
func (lt *LoadTest) taskRunsJob() {
for {
select {
case tr := <-lt.TaskRunChan:
lt.handleTaskRun(tr)
}
}
}
func (lt *LoadTest) Run(entryTask *Task) {
lt.Log.Println("Starting Load Testing Tool")
if lt.Config.SpawnOnStartup {
lt.TargetUserNum = lt.Config.NumUsers
}
go lt.taskRunsJob()
go lt.runAPIJob()
go lt.cleanRPSJob()
go lt.usersJob(entryTask)
// Run forever
c := make(chan struct{})
<-c
}
func NewLoadTest(config Config) *LoadTest {
return &LoadTest{
Config: config,
Status: StatusStopped,
UserMap: make(map[int64]User, config.NumUsers),
Stats: NewStatistics(),
TaskRunChan: make(chan *TaskRun, config.NumUsers),
Log: log.New(config.LogOutput, config.LogPrefix, config.LogFlags),
}
}
func FromContext(ctx context.Context) *LoadTest {
if lt, ok := ctx.Value(loadtestContextKey).(*LoadTest); ok {
return lt
}
return nil
}
func NewLoadTestContext(ctx context.Context, lt *LoadTest) context.Context {
return context.WithValue(ctx, loadtestContextKey, lt)
}