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

Feature/allow random size payload generation #81

Open
wants to merge 2 commits 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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
appname := mqtt-stresser
namespace := inovex
namespace = ${IMG_NAMESPACE}
namespace ?= inovex
sources := vendor $(wildcard *.go)

build = GO111MODULE=on GOOS=$(1) GOARCH=$(2) go build -mod=vendor -o build/$(appname)-$(1)-$(2)$(3)
Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ var (
argKey = flag.String("key", "", "client private key for authentication, if required by server.")
argCert = flag.String("cert", "", "client certificate for authentication, if required by server.")
argPauseBetweenMessages = flag.String("pause-between-messages", "0s", "Adds a pause between sending messages to simulate sensors sending messages infrequently")
argTopicBasePath = flag.String("topic-base-path", "", "topic base path, if empty the default is internal/mqtt-stresser")
argTopicBasePath = flag.String("topic-base-path", "", "topic base path, if empty the default is internal/mqtt-stresser")
argMinMessageChars = flag.Int("min-msg-chars", 0, "Minimum number of characters in message payload")
argMaxMessageChars = flag.Int("max-msg-chars", 0, "Maximum number of characters in message payload")
)

type Result struct {
Expand Down Expand Up @@ -181,11 +183,16 @@ func main() {
}

payloadGenerator := defaultPayloadGen()
minChars := *argMinMessageChars
maxChars := *argMaxMessageChars
if minChars > 0 && maxChars > 0 && minChars < maxChars {
payloadGenerator = randomMessageGenerator(minChars, maxChars)
}
if len(*argConstantPayload) > 0 {
if strings.HasPrefix(*argConstantPayload, "@") {
verboseLogger.Printf("Set constant payload from file %s\n", *argConstantPayload)
payloadGenerator = filePayloadGenerator(*argConstantPayload)
}else {
} else {
verboseLogger.Printf("Set constant payload to %s\n", *argConstantPayload)
payloadGenerator = constantPayloadGenerator(*argConstantPayload)
}
Expand Down
19 changes: 19 additions & 0 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
Expand All @@ -21,6 +22,24 @@ func defaultPayloadGen() PayloadGenerator {
}
}

func randomMessageGenerator(min, max int) PayloadGenerator {
rand.Seed(time.Now().UnixNano())
maxPayload := generateMaxPayload(max)
return func(i int) string {
size := rand.Intn(max-min+1) + min
return maxPayload[:size]
}
}

func generateMaxPayload(size int) string {
chars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
buffer := make([]rune, size)
for i := range buffer {
buffer[i] = chars[i%len(chars)]
}
return string(buffer)
}

func constantPayloadGenerator(payload string) PayloadGenerator {
return func(i int) string {
return payload
Expand Down