This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvu_sleep_mock.go
81 lines (73 loc) · 1.85 KB
/
vu_sleep_mock.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
package wasp
import (
"errors"
"math/rand"
"time"
)
// MockVirtualUserConfig configures a mock virtual user
type MockVirtualUserConfig struct {
// FailRatio in percentage, 0-100
FailRatio int
// TimeoutRatio in percentage, 0-100
TimeoutRatio int
// CallSleep time spent waiting inside a call
CallSleep time.Duration
SetupSleep time.Duration
SetupFailure bool
TeardownSleep time.Duration
TeardownFailure bool
}
// MockVirtualUser is a mock virtual user
type MockVirtualUser struct {
*VUControl
cfg *MockVirtualUserConfig
Data []string
}
// NewMockVU create a mock virtual user
func NewMockVU(cfg *MockVirtualUserConfig) *MockVirtualUser {
return &MockVirtualUser{
VUControl: NewVUControl(),
cfg: cfg,
Data: make([]string, 0),
}
}
func (m *MockVirtualUser) Clone(_ *Generator) VirtualUser {
return &MockVirtualUser{
VUControl: NewVUControl(),
cfg: m.cfg,
Data: make([]string, 0),
}
}
func (m *MockVirtualUser) Setup(_ *Generator) error {
if m.cfg.SetupFailure {
return errors.New("setup failure")
}
time.Sleep(m.cfg.SetupSleep)
return nil
}
func (m *MockVirtualUser) Teardown(_ *Generator) error {
if m.cfg.TeardownFailure {
return errors.New("teardown failure")
}
time.Sleep(m.cfg.TeardownSleep)
return nil
}
func (m *MockVirtualUser) Call(l *Generator) {
startedAt := time.Now()
time.Sleep(m.cfg.CallSleep)
if m.cfg.FailRatio > 0 && m.cfg.FailRatio <= 100 {
//nolint
r := rand.Intn(100)
if r <= m.cfg.FailRatio {
l.ResponsesChan <- &Response{StartedAt: &startedAt, Data: "failedCallData", Error: "error", Failed: true}
}
}
if m.cfg.TimeoutRatio > 0 && m.cfg.TimeoutRatio <= 100 {
//nolint
r := rand.Intn(100)
if r <= m.cfg.TimeoutRatio {
time.Sleep(m.cfg.CallSleep + 20*time.Millisecond)
}
}
l.ResponsesChan <- &Response{StartedAt: &startedAt, Data: "successCallData"}
}