Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqtt5 support #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ module github.com/inovex/mqtt-stresser

go 1.12

require (
github.com/eclipse/paho.mqtt.golang v1.2.0
golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6 // indirect
golang.org/x/text v0.3.2 // indirect
)
require github.com/eclipse/paho.golang v0.10.0
30 changes: 21 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
github.com/eclipse/paho.mqtt.golang v1.2.0 h1:1F8mhG9+aO5/xpdtFkW4SxOJB67ukuDC3t2y2qayIX0=
github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6 h1:FP8hkuE6yUEaJnK7O2eTuejKWwW+Rhfj80dQ2JcKxCU=
golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eclipse/paho.golang v0.10.0 h1:oUGPjRwWcZQRgDD9wVDV7y7i7yBSxts3vcvcNJo8B4Q=
github.com/eclipse/paho.golang v0.10.0/go.mod h1:rhrV37IEwauUyx8FHrvmXOKo+QRKng5ncoN1vJiJMcs=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
129 changes: 74 additions & 55 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"crypto/x509"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
"time"

mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/eclipse/paho.golang/autopaho"
"github.com/eclipse/paho.golang/paho"
)

type PayloadGenerator func(i int) string
Expand Down Expand Up @@ -57,13 +59,13 @@ type Worker struct {
PauseBetweenMessages time.Duration
}

func setSkipTLS(o *mqtt.ClientOptions) {
oldTLSCfg := o.TLSConfig
func setSkipTLS(o *autopaho.ClientConfig) {
oldTLSCfg := o.TlsCfg
if oldTLSCfg == nil {
oldTLSCfg = &tls.Config{}
}
oldTLSCfg.InsecureSkipVerify = true
o.SetTLSConfig(oldTLSCfg)
o.TlsCfg = oldTLSCfg
}

func NewTLSConfig(ca, certificate, privkey []byte) (*tls.Config, error) {
Expand Down Expand Up @@ -115,93 +117,110 @@ func (w *Worker) Run(ctx context.Context) {
subscriberClientId := fmt.Sprintf(subscriberClientIdTemplate, hostname, w.WorkerId, t)
publisherClientId := fmt.Sprintf(publisherClientIdTemplate, hostname, w.WorkerId, t)

burl, err := url.Parse(w.BrokerUrl)
if err != nil {
panic(err)
}
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())}
})

sc := autopaho.ClientConfig{
BrokerUrls: []*url.URL{burl},
KeepAlive: uint16(w.Timeout.Seconds()),
OnConnectionUp: func(cm *autopaho.ConnectionManager, _ *paho.Connack) {
verboseLogger.Printf("[%d] subscribing to topic\n", w.WorkerId)
if _, err := cm.Subscribe(ctx, &paho.Subscribe{Subscriptions: map[string]paho.SubscribeOptions{topicName: paho.SubscribeOptions{QoS: w.SubscriberQoS}}}); err != nil {
resultChan <- Result{
WorkerId: w.WorkerId,
Event: SubscribeFailedEvent,
Error: true,
ErrorMessage: err,
}
return
}
},
OnConnectError: func(err error) {
resultChan <- Result{
WorkerId: w.WorkerId,
Event: ConnectFailedEvent,
Error: true,
ErrorMessage: fmt.Errorf("sub: %s", err),
}
verboseLogger.Printf("[%d] connecting subscriber\n", w.WorkerId)
},
Debug: paho.NOOPLogger{},
ClientConfig: paho.ClientConfig{
ClientID: subscriberClientId,
OnClientError: func(err error) { fmt.Printf("server requested disconnect: %s\n", err) },
OnServerDisconnect: func(d *paho.Disconnect) {
if d.Properties != nil {
fmt.Printf("server requested disconnect: %s\n", d.Properties.ReasonString)
} else {
fmt.Printf("server requested disconnect; reason code: %d\n", d.ReasonCode)
}
},
Router: paho.NewSingleHandlerRouter(func(msgpub *paho.Publish) {
queue <- [2]string{msgpub.Topic, string(msgpub.Payload)}
}),
},
}
sc.SetUsernamePassword(w.Username, []byte(w.Password))
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)
sc.TlsCfg = tlsConfig
}

if w.SkipTLSVerification {
setSkipTLS(publisherOptions)
setSkipTLS(subscriberOptions)
setSkipTLS(&sc)
}

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
sub, err := autopaho.NewConnection(ctx, sc)
if err != nil {
panic(err)
}

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())
if _, err := sub.Unsubscribe(ctx, &paho.Unsubscribe{Topics: []string{topicName}}); err != nil {
fmt.Printf("failed to unsubscribe: %v\n", err)
}

subscriber.Disconnect(5)
//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 {
sc.ClientConfig.ClientID = publisherClientId
sc.OnConnectError = func(err error) {
resultChan <- Result{
WorkerId: w.WorkerId,
Event: ConnectFailedEvent,
Error: true,
ErrorMessage: token.Error(),
ErrorMessage: fmt.Errorf("pub: %s", err),
}
return
verboseLogger.Printf("[%d] connecting publisher\n", w.WorkerId)
}
pub, err := autopaho.NewConnection(ctx, sc)
if err != nil {
panic(err)
}

verboseLogger.Printf("[%d] starting control loop %s\n", w.WorkerId, topicName)

stopWorker := false
receivedCount := 0
publishedCount := 0

pub.AwaitConnection(ctx)
sub.AwaitConnection(ctx)
t0 := time.Now()
for i := 0; i < w.NumberOfMessages; i++ {
text := w.PayloadGenerator(i)
token := publisher.Publish(topicName, w.PublisherQoS, w.Retained, text)
_, err = pub.Publish(ctx, &paho.Publish{Topic: topicName, QoS: w.PublisherQoS, Retain: w.Retained, Payload: []byte(text)})
if err != nil {
verboseLogger.Printf("[%d][%s] error publish: %s\n", w.WorkerId, topicName, err)
}
publishedCount++
token.WaitTimeout(w.Timeout)
time.Sleep(w.PauseBetweenMessages)
}
publisher.Disconnect(5)
//publisher.Disconnect(5)

publishTime := time.Since(t0)
verboseLogger.Printf("[%d] all messages published\n", w.WorkerId)
Expand Down