Message payload (#12)

Added an option to specify message payload
This commit is contained in:
Tomasz
2021-06-26 17:28:54 +02:00
committed by GitHub
parent dcc9f89a84
commit 6be645ba24
3 changed files with 47 additions and 7 deletions

View File

@@ -30,6 +30,8 @@ Usage of ./mqtt-benchmark:
Output format: text|json (default "text") Output format: text|json (default "text")
-password string -password string
MQTT client password (empty if auth disabled) MQTT client password (empty if auth disabled)
-payload string
MQTT message payload. If empty, then payload is generated based on the size parameter
-qos int -qos int
QoS for published messages (default 1) QoS for published messages (default 1)
-quiet -quiet
@@ -75,6 +77,33 @@ Average Bandwidth (msg/sec): 6.761
Total Bandwidth (msg/sec): 676.112 Total Bandwidth (msg/sec): 676.112
``` ```
With payload specified:
```sh
> mqtt-benchmark --broker tcp://broker.local:1883 --count 100 --clients 10 --qos 1 --topic house/bedroom/temperature --payload {\"temperature\":20,\"timeStamp\":1597314150}
....
======= CLIENT 0 =======
Ratio: 1.000 (100/100)
Runtime (s): 0.725
Msg time min (ms): 1.999
Msg time max (ms): 22.997
Msg time mean (ms): 6.955
Msg time std (ms): 3.523
Bandwidth (msg/sec): 137.839
========= TOTAL (1) =========
Total Ratio: 1.000 (100/100)
Total Runtime (sec): 0.736
Average Runtime (sec): 0.725
Msg time min (ms): 1.999
Msg time max (ms): 22.997
Msg time mean mean (ms): 6.955
Msg time mean std (ms): 0.000
Average Bandwidth (msg/sec): 137.839
Total Bandwidth (msg/sec): 137.839
```
Similarly, in JSON: Similarly, in JSON:
```json ```json

View File

@@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/GaryBoone/GoStats/stats" "github.com/GaryBoone/GoStats/stats"
mqtt "github.com/eclipse/paho.mqtt.golang" mqtt "github.com/eclipse/paho.mqtt.golang"
) )
@@ -18,6 +19,7 @@ type Client struct {
BrokerUser string BrokerUser string
BrokerPass string BrokerPass string
MsgTopic string MsgTopic string
MsgPayload string
MsgSize int MsgSize int
MsgCount int MsgCount int
MsgQoS byte MsgQoS byte
@@ -74,13 +76,20 @@ func (c *Client) Run(res chan *RunResults) {
} }
func (c *Client) genMessages(ch chan *Message, done chan bool) { func (c *Client) genMessages(ch chan *Message, done chan bool) {
for i := 0; i < c.MsgCount; i++ { var payload interface{}
ch <- &Message{ // set payload if specified
Topic: c.MsgTopic, if c.MsgPayload != "" {
QoS: c.MsgQoS, payload = c.MsgPayload
Payload: make([]byte, c.MsgSize), } else {
} payload = make([]byte, c.MsgSize)
} }
ch <- &Message{
Topic: c.MsgTopic,
QoS: c.MsgQoS,
Payload: payload,
}
done <- true done <- true
// log.Printf("CLIENT %v is done generating messages\n", c.ID) // log.Printf("CLIENT %v is done generating messages\n", c.ID)
return return

View File

@@ -60,6 +60,7 @@ func main() {
var ( var (
broker = flag.String("broker", "tcp://localhost:1883", "MQTT broker endpoint as scheme://host:port") broker = flag.String("broker", "tcp://localhost:1883", "MQTT broker endpoint as scheme://host:port")
topic = flag.String("topic", "/test", "MQTT topic for outgoing messages") topic = flag.String("topic", "/test", "MQTT topic for outgoing messages")
payload = flag.String("payload", "", "MQTT message payload. If empty, then payload is generated based on the size parameter")
username = flag.String("username", "", "MQTT client username (empty if auth disabled)") username = flag.String("username", "", "MQTT client username (empty if auth disabled)")
password = flag.String("password", "", "MQTT client password (empty if auth disabled)") password = flag.String("password", "", "MQTT client password (empty if auth disabled)")
qos = flag.Int("qos", 1, "QoS for published messages") qos = flag.Int("qos", 1, "QoS for published messages")
@@ -109,6 +110,7 @@ func main() {
BrokerUser: *username, BrokerUser: *username,
BrokerPass: *password, BrokerPass: *password,
MsgTopic: *topic, MsgTopic: *topic,
MsgPayload: *payload,
MsgSize: *size, MsgSize: *size,
MsgCount: *count, MsgCount: *count,
MsgQoS: byte(*qos), MsgQoS: byte(*qos),