forked from inovex/mqtt-stresser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
261 lines (223 loc) · 7.02 KB
/
worker.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
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
type PayloadGenerator func(i int) string
func defaultPayloadGen() PayloadGenerator {
return func(i int) string {
return fmt.Sprintf("this is msg #%d!", i)
}
}
func constantPayloadGenerator(payload string) PayloadGenerator {
return func(i int) string {
return payload
}
}
func filePayloadGenerator(filepath string) PayloadGenerator {
inputPath := strings.Replace(filepath, "@", "", 1)
content, err := ioutil.ReadFile(inputPath)
if err != nil {
fmt.Printf("error reading payload file: %v\n", err)
os.Exit(1)
}
return func(i int) string {
return string(content)
}
}
type Worker struct {
WorkerId int
BrokerUrl string
Username string
Password string
SkipTLSVerification bool
NumberOfMessages int
PayloadGenerator PayloadGenerator
Timeout time.Duration
Retained bool
PublisherQoS byte
SubscriberQoS byte
CA []byte
Cert []byte
Key []byte
PauseBetweenMessages time.Duration
}
func setSkipTLS(o *mqtt.ClientOptions) {
oldTLSCfg := o.TLSConfig
if oldTLSCfg == nil {
oldTLSCfg = &tls.Config{}
}
oldTLSCfg.InsecureSkipVerify = true
o.SetTLSConfig(oldTLSCfg)
}
func NewTLSConfig(ca, certificate, privkey []byte) (*tls.Config, error) {
// Import trusted certificates from CA
certpool := x509.NewCertPool()
ok := certpool.AppendCertsFromPEM(ca)
if !ok {
return nil, fmt.Errorf("CA is invalid")
}
// Import client certificate/key pair
cert, err := tls.X509KeyPair(certificate, privkey)
if err != nil {
return nil, err
}
// Create tls.Config with desired tls properties
return &tls.Config{
// RootCAs = certs used to verify server cert.
RootCAs: certpool,
// ClientAuth = whether to request cert from server.
// Since the server is set up for SSL, this happens
// anyways.
ClientAuth: tls.NoClientCert,
// ClientCAs = certs used to validate client cert.
ClientCAs: nil,
// InsecureSkipVerify = verify that cert contents
// match server. IP matches what is in cert etc.
InsecureSkipVerify: false,
// Certificates = list of certs client sends to server.
Certificates: []tls.Certificate{cert},
}, nil
}
func (w *Worker) Run(ctx context.Context) {
verboseLogger.Printf("[%d] initializing\n", w.WorkerId)
queue := make(chan [2]string)
cid := w.WorkerId
t := randomSource.Int31()
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
topicName := fmt.Sprintf(topicNameTemplate, hostname, w.WorkerId, t)
subscriberClientId := fmt.Sprintf(subscriberClientIdTemplate, hostname, w.WorkerId, t)
publisherClientId := fmt.Sprintf(publisherClientIdTemplate, hostname, w.WorkerId, t)
verboseLogger.Printf("[%d] topic=%s subscriberClientId=%s publisherClientId=%s\n", cid, topicName, subscriberClientId, publisherClientId)
publisherOptions := mqtt.NewClientOptions().SetClientID(publisherClientId).SetUsername(w.Username).SetPassword(w.Password).AddBroker(w.BrokerUrl)
subscriberOptions := mqtt.NewClientOptions().SetClientID(subscriberClientId).SetUsername(w.Username).SetPassword(w.Password).AddBroker(w.BrokerUrl)
subscriberOptions.SetDefaultPublishHandler(func(client mqtt.Client, msg mqtt.Message) {
queue <- [2]string{msg.Topic(), string(msg.Payload())}
})
if len(w.CA) > 0 || len(w.Key) > 0 {
tlsConfig, err := NewTLSConfig(w.CA, w.Cert, w.Key)
if err != nil {
panic(err)
}
subscriberOptions.SetTLSConfig(tlsConfig)
publisherOptions.SetTLSConfig(tlsConfig)
}
if w.SkipTLSVerification {
setSkipTLS(publisherOptions)
setSkipTLS(subscriberOptions)
}
subscriber := mqtt.NewClient(subscriberOptions)
verboseLogger.Printf("[%d] connecting subscriber\n", w.WorkerId)
if token := subscriber.Connect(); token.WaitTimeout(w.Timeout) && token.Error() != nil {
resultChan <- Result{
WorkerId: w.WorkerId,
Event: ConnectFailedEvent,
Error: true,
ErrorMessage: token.Error(),
}
return
}
defer func() {
verboseLogger.Printf("[%d] unsubscribe\n", w.WorkerId)
if token := subscriber.Unsubscribe(topicName); token.WaitTimeout(w.Timeout) && token.Error() != nil {
fmt.Printf("failed to unsubscribe: %v\n", token.Error())
}
subscriber.Disconnect(5)
}()
verboseLogger.Printf("[%d] subscribing to topic\n", w.WorkerId)
if token := subscriber.Subscribe(topicName, w.SubscriberQoS, nil); token.WaitTimeout(w.Timeout) && token.Error() != nil {
resultChan <- Result{
WorkerId: w.WorkerId,
Event: SubscribeFailedEvent,
Error: true,
ErrorMessage: token.Error(),
}
return
}
publisher := mqtt.NewClient(publisherOptions)
verboseLogger.Printf("[%d] connecting publisher\n", w.WorkerId)
if token := publisher.Connect(); token.WaitTimeout(w.Timeout) && token.Error() != nil {
resultChan <- Result{
WorkerId: w.WorkerId,
Event: ConnectFailedEvent,
Error: true,
ErrorMessage: token.Error(),
}
return
}
verboseLogger.Printf("[%d] starting control loop %s\n", w.WorkerId, topicName)
stopWorker := false
receivedCount := 0
publishedCount := 0
t0 := time.Now()
for i := 0; i < w.NumberOfMessages; i++ {
text := w.PayloadGenerator(i)
token := publisher.Publish(topicName, w.PublisherQoS, w.Retained, text)
publishedCount++
token.WaitTimeout(w.Timeout)
time.Sleep(w.PauseBetweenMessages)
}
publisher.Disconnect(5)
publishTime := time.Since(t0)
verboseLogger.Printf("[%d] all messages published\n", w.WorkerId)
t0 = time.Now()
for receivedCount < w.NumberOfMessages && !stopWorker {
select {
case <-queue:
receivedCount++
verboseLogger.Printf("[%d] %d/%d received\n", w.WorkerId, receivedCount, w.NumberOfMessages)
if receivedCount == w.NumberOfMessages {
resultChan <- Result{
WorkerId: w.WorkerId,
Event: CompletedEvent,
PublishTime: publishTime,
ReceiveTime: time.Since(t0),
MessagesReceived: receivedCount,
MessagesPublished: publishedCount,
}
} else {
resultChan <- Result{
WorkerId: w.WorkerId,
Event: ProgressReportEvent,
PublishTime: publishTime,
ReceiveTime: time.Since(t0),
MessagesReceived: receivedCount,
MessagesPublished: publishedCount,
}
}
case <-ctx.Done():
var event string
var isError bool
switch ctx.Err().(type) {
case TimeoutError:
verboseLogger.Printf("[%d] received abort signal due to test timeout", w.WorkerId)
event = TimeoutExceededEvent
isError = true
default:
verboseLogger.Printf("[%d] received abort signal", w.WorkerId)
event = AbortedEvent
isError = false
}
stopWorker = true
resultChan <- Result{
WorkerId: w.WorkerId,
Event: event,
PublishTime: publishTime,
MessagesReceived: receivedCount,
MessagesPublished: publishedCount,
Error: isError,
}
}
}
verboseLogger.Printf("[%d] worker finished\n", w.WorkerId)
}