-
Notifications
You must be signed in to change notification settings - Fork 42
/
controller.go
211 lines (189 loc) · 5.04 KB
/
controller.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
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package simulcontroller
import (
"errors"
"fmt"
"sync"
"time"
"github.com/mattermost/mattermost-load-test-ng/defaults"
"github.com/mattermost/mattermost-load-test-ng/loadtest/control"
"github.com/mattermost/mattermost-load-test-ng/loadtest/user"
)
// SimulController is a simulative implementation of a UserController.
type SimulController struct {
id int
user user.User
status chan<- control.UserStatus
rate float64
config *Config
stopChan chan struct{} // this channel coordinates the stop sequence of the controller
stoppedChan chan struct{} // blocks until controller cleans up everything
disconnectChan chan struct{} // notifies disconnection to the ws and periodic goroutines
connectedFlag int32 // indicates that the controller is connected
wg *sync.WaitGroup // to keep the track of every goroutine created by the controller
}
// New creates and initializes a new SimulController with given parameters.
// An id is provided to identify the controller, a User is passed as the entity to be controlled and
// a UserStatus channel is passed to communicate errors and information about the user's status.
func New(id int, user user.User, config *Config, status chan<- control.UserStatus) (*SimulController, error) {
if config == nil || user == nil {
return nil, errors.New("nil params passed")
}
if err := defaults.Validate(config); err != nil {
return nil, fmt.Errorf("could not validate configuration: %w", err)
}
return &SimulController{
id: id,
user: user,
status: status,
rate: 1.0,
config: config,
disconnectChan: make(chan struct{}),
stopChan: make(chan struct{}),
stoppedChan: make(chan struct{}),
wg: &sync.WaitGroup{},
}, nil
}
// Run begins performing a set of user actions in a loop.
// It keeps on doing it until Stop() is invoked.
// This is also a blocking function, so it is recommended to invoke it
// inside a goroutine.
func (c *SimulController) Run() {
if c.user == nil {
c.sendFailStatus("controller was not initialized")
return
}
c.status <- control.UserStatus{ControllerId: c.id, User: c.user, Info: "user started", Code: control.USER_STATUS_STARTED}
defer func() {
if resp := c.logout(); resp.Err != nil {
c.status <- c.newErrorStatus(resp.Err)
}
c.user.ClearUserData()
c.sendStopStatus()
close(c.stoppedChan)
}()
initActions := []userAction{
{
run: control.SignUp,
},
{
run: c.login,
},
{
run: c.joinTeam,
},
{
run: c.joinChannel,
},
}
for i := 0; i < len(initActions); i++ {
select {
case <-c.stopChan:
return
case <-time.After(pickIdleTimeMs(c.config.MinIdleTimeMs, c.config.AvgIdleTimeMs, 1.0)):
}
if resp := initActions[i].run(c.user); resp.Err != nil {
c.status <- c.newErrorStatus(resp.Err)
i--
} else {
c.status <- c.newInfoStatus(resp.Info)
}
}
actions := []userAction{
{
run: switchChannel,
frequency: 70,
},
{
run: c.switchTeam,
frequency: 30,
},
{
run: openDirectOrGroupChannel,
frequency: 30,
},
{
run: unreadCheck,
frequency: 25,
},
{
run: c.createPost,
frequency: 25,
},
{
run: c.createPostReply,
frequency: 15,
},
{
run: c.joinChannel,
frequency: 8,
},
{
run: editPost,
frequency: 8,
},
{
run: searchChannels,
frequency: 5,
},
{
run: c.addReaction,
frequency: 5,
},
{
run: c.fullReload,
frequency: 4,
},
{
run: c.createDirectChannel,
frequency: 2,
},
{
run: c.createGroupChannel,
frequency: 1,
},
{
run: c.logoutLogin,
frequency: 1,
},
}
for {
action, err := pickAction(actions)
if err != nil {
panic(fmt.Sprintf("simulcontroller: failed to pick action %s", err.Error()))
}
if resp := action.run(c.user); resp.Err != nil {
c.status <- c.newErrorStatus(resp.Err)
} else {
c.status <- c.newInfoStatus(resp.Info)
}
select {
case <-c.stopChan:
return
case <-time.After(pickIdleTimeMs(c.config.MinIdleTimeMs, c.config.AvgIdleTimeMs, c.rate)):
}
}
}
// SetRate sets the relative speed of execution of actions by the user.
func (c *SimulController) SetRate(rate float64) error {
if rate < 0 {
return errors.New("rate should be a positive value")
}
c.rate = rate
return nil
}
// Stop stops the controller.
func (c *SimulController) Stop() {
close(c.stopChan)
<-c.stoppedChan
// re-initialize for the next use
c.stopChan = make(chan struct{})
c.stoppedChan = make(chan struct{})
}
func (c *SimulController) sendFailStatus(reason string) {
c.status <- control.UserStatus{ControllerId: c.id, User: c.user, Code: control.USER_STATUS_FAILED, Err: errors.New(reason)}
}
func (c *SimulController) sendStopStatus() {
c.status <- control.UserStatus{ControllerId: c.id, User: c.user, Info: "user stopped", Code: control.USER_STATUS_STOPPED}
}