forked from myzhan/boomer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boomer_test.go
350 lines (285 loc) · 7.88 KB
/
boomer_test.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
package boomer
import (
"flag"
"fmt"
"log"
"math"
"math/rand"
"os"
"runtime"
"sync/atomic"
"testing"
"time"
)
func TestNewBoomer(t *testing.T) {
b := NewBoomer("0.0.0.0", 1234)
if b.masterHost != "0.0.0.0" {
t.Error("masterHost should be 0.0.0.0")
}
if b.masterPort != 1234 {
t.Error("masterPort should be 1234")
}
if b.mode != DistributedMode {
t.Error("mode should be DistributedMode")
}
}
func TestNewStandaloneBoomer(t *testing.T) {
b := NewStandaloneBoomer(100, 10)
if b.spawnCount != 100 {
t.Error("spawnCount should be 100")
}
if b.spawnRate != 10 {
t.Error("spawnRate should be 10")
}
if b.mode != StandaloneMode {
t.Error("mode should be StandaloneMode")
}
}
func TestSetRateLimiter(t *testing.T) {
b := NewStandaloneBoomer(100, 10)
limiter, _ := NewRampUpRateLimiter(10, "10/1s", time.Second)
b.SetRateLimiter(limiter)
if b.rateLimiter == nil {
t.Error("b.rateLimiter should not be nil")
}
}
func TestSetMode(t *testing.T) {
b := NewStandaloneBoomer(100, 10)
b.SetMode(DistributedMode)
if b.mode != DistributedMode {
t.Error("mode should be DistributedMode")
}
b.SetMode(StandaloneMode)
if b.mode != StandaloneMode {
t.Error("mode should be StandaloneMode")
}
b.SetMode(3)
if b.mode != StandaloneMode {
t.Error("mode should be StandaloneMode")
}
}
func TestAddOutput(t *testing.T) {
b := NewStandaloneBoomer(100, 10)
b.AddOutput(NewConsoleOutput())
b.AddOutput(NewConsoleOutput())
if len(b.outputs) != 2 {
t.Error("length of outputs should be 2")
}
}
func TestEnableCPUProfile(t *testing.T) {
b := NewStandaloneBoomer(100, 10)
b.EnableCPUProfile("cpu.prof", time.Second)
if b.cpuProfile != "cpu.prof" {
t.Error("cpuProfile should be cpu.prof")
}
if b.cpuProfileDuration != time.Second {
t.Error("cpuProfileDuration should 1 second")
}
}
func TestEnableMemoryProfile(t *testing.T) {
b := NewStandaloneBoomer(100, 10)
b.EnableMemoryProfile("mem.prof", time.Second)
if b.memoryProfile != "mem.prof" {
t.Error("memoryProfile should be mem.prof")
}
if b.memoryProfileDuration != time.Second {
t.Error("memoryProfileDuration should 1 second")
}
}
func TestStandaloneRun(t *testing.T) {
b := NewStandaloneBoomer(10, 10)
b.EnableCPUProfile("cpu.pprof", 2*time.Second)
b.EnableMemoryProfile("mem.pprof", 2*time.Second)
count := int64(0)
taskA := &Task{
Name: "increaseCount",
Fn: func() {
atomic.AddInt64(&count, 1)
runtime.Goexit()
},
}
go b.Run(taskA)
time.Sleep(5 * time.Second)
b.Quit()
if count != 10 {
t.Error("count is", count, "expected: 10")
}
if _, err := os.Stat("cpu.pprof"); os.IsNotExist(err) {
t.Error("File cpu.pprof is not generated")
} else {
os.Remove("cpu.pprof")
}
if _, err := os.Stat("mem.pprof"); os.IsNotExist(err) {
t.Error("File mem.pprof is not generated")
} else {
os.Remove("mem.pprof")
}
}
func TestDistributedRun(t *testing.T) {
masterHost := "0.0.0.0"
rand.Seed(Now())
masterPort := rand.Intn(1000) + 10240
server := newTestServer(masterHost, masterPort)
defer server.close()
log.Println(fmt.Sprintf("Starting to serve on %s:%d", masterHost, masterPort))
server.start()
time.Sleep(20 * time.Millisecond)
b := NewBoomer(masterHost, masterPort)
count := int64(0)
taskA := &Task{
Name: "increaseCount",
Fn: func() {
atomic.AddInt64(&count, 1)
runtime.Goexit()
},
}
b.Run(taskA)
server.toClient <- newGenericMessage("spawn", map[string]interface{}{
"user_classes_count": map[interface{}]interface{}{
"Dummy": int64(5),
"Dummy2": int64(5),
},
}, b.slaveRunner.nodeID)
time.Sleep(4 * time.Second)
b.Quit()
if count != 10 {
t.Error("count is", count, "expected: 10")
}
}
func TestRunTasksForTest(t *testing.T) {
count := 0
taskA := &Task{
Name: "increaseCount",
Fn: func() {
count++
},
}
taskWithoutName := &Task{
Name: "",
Fn: func() {
count++
},
}
runTasks = "increaseCount,foobar"
runTasksForTest(taskA, taskWithoutName)
if count != 1 {
t.Error("count is", count, "expected: 1")
}
runTasks = ""
}
func TestRunTasksWithBoomerReport(t *testing.T) {
taskA := &Task{
Name: "report",
Fn: func() {
// it should not panic.
RecordSuccess("http", "foo", int64(1), int64(10))
RecordFailure("udp", "bar", int64(1), "udp error")
},
}
runTasks = "report"
runTasksForTest(taskA)
runTasks = ""
}
func TestCreateRatelimiter(t *testing.T) {
rateLimiter, _ := createRateLimiter(100, "-1")
if stableRateLimiter, ok := rateLimiter.(*StableRateLimiter); !ok {
t.Error("Expected stableRateLimiter")
} else {
if stableRateLimiter.threshold != 100 {
t.Error("threshold should be equals to math.MaxInt64, was", stableRateLimiter.threshold)
}
}
rateLimiter, _ = createRateLimiter(0, "1")
if rampUpRateLimiter, ok := rateLimiter.(*RampUpRateLimiter); !ok {
t.Error("Expected rampUpRateLimiter")
} else {
if rampUpRateLimiter.maxThreshold != math.MaxInt64 {
t.Error("maxThreshold should be equals to math.MaxInt64, was", rampUpRateLimiter.maxThreshold)
}
if rampUpRateLimiter.rampUpRate != "1" {
t.Error("rampUpRate should be equals to \"1\", was", rampUpRateLimiter.rampUpRate)
}
}
rateLimiter, _ = createRateLimiter(10, "2/2s")
if rampUpRateLimiter, ok := rateLimiter.(*RampUpRateLimiter); !ok {
t.Error("Expected rampUpRateLimiter")
} else {
if rampUpRateLimiter.maxThreshold != 10 {
t.Error("maxThreshold should be equals to 10, was", rampUpRateLimiter.maxThreshold)
}
if rampUpRateLimiter.rampUpRate != "2/2s" {
t.Error("rampUpRate should be equals to \"2/2s\", was", rampUpRateLimiter.rampUpRate)
}
if rampUpRateLimiter.rampUpStep != 2 {
t.Error("rampUpStep should be equals to 2, was", rampUpRateLimiter.rampUpStep)
}
if rampUpRateLimiter.rampUpPeroid != 2*time.Second {
t.Error("rampUpPeroid should be equals to 2 seconds, was", rampUpRateLimiter.rampUpPeroid)
}
}
}
func TestRun(t *testing.T) {
flag.Parse()
masterHost = "0.0.0.0"
rand.Seed(Now())
masterPort = rand.Intn(1000) + 10240
server := newTestServer(masterHost, masterPort)
defer server.close()
log.Println(fmt.Sprintf("Starting to serve on %s:%d", masterHost, masterPort))
server.start()
time.Sleep(20 * time.Millisecond)
count := int64(0)
taskA := &Task{
Name: "increaseCount",
Fn: func() {
atomic.AddInt64(&count, 1)
runtime.Goexit()
},
}
go Run(taskA)
time.Sleep(20 * time.Millisecond)
server.toClient <- newGenericMessage("spawn", map[string]interface{}{
"user_classes_count": map[interface{}]interface{}{
"Dummy": int64(5),
"Dummy2": int64(5),
},
}, defaultBoomer.slaveRunner.nodeID)
time.Sleep(4 * time.Second)
defaultBoomer.Quit()
if count != 10 {
t.Error("count is", count, "expected: 10")
}
}
func TestRecordSuccess(t *testing.T) {
masterHost := "127.0.0.1"
masterPort := 5557
defaultBoomer = NewBoomer(masterHost, masterPort)
defaultBoomer.slaveRunner = newSlaveRunner(masterHost, masterPort, nil, nil)
RecordSuccess("http", "foo", int64(1), int64(10))
requestSuccessMsg := <-defaultBoomer.slaveRunner.stats.requestSuccessChan
if requestSuccessMsg.requestType != "http" {
t.Error("Expected: http, got:", requestSuccessMsg.requestType)
}
if requestSuccessMsg.responseTime != int64(1) {
t.Error("Expected: 1, got:", requestSuccessMsg.responseTime)
}
defaultBoomer = nil
}
func TestRecordFailure(t *testing.T) {
masterHost := "127.0.0.1"
masterPort := 5557
defaultBoomer = NewBoomer(masterHost, masterPort)
defaultBoomer.slaveRunner = newSlaveRunner(masterHost, masterPort, nil, nil)
RecordFailure("udp", "bar", int64(2), "udp error")
requestFailureMsg := <-defaultBoomer.slaveRunner.stats.requestFailureChan
if requestFailureMsg.requestType != "udp" {
t.Error("Expected: udp, got:", requestFailureMsg.requestType)
}
if requestFailureMsg.responseTime != int64(2) {
t.Error("Expected: 2, got:", requestFailureMsg.responseTime)
}
if requestFailureMsg.error != "udp error" {
t.Error("Expected: udp error, got:", requestFailureMsg.error)
}
defaultBoomer = nil
}