mqtt

package module
v0.0.0-...-aba974a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

README

xk6-mqtt

This is a k6 extension using the xk6 system.

❗ This is a proof of concept for tls connection without client certificate, isn't supported by the k6 team, and may break in the future. USE AT YOUR OWN RISK!

This project is a k6 extension that can be used to load test Mqtt. Per each connection to Mqtt, many messages can be sent. These messages are an array of strings. There is also a consumer for testing purposes, i.e. to make sure you send the correct data to Mqtt. The consumer is not meant to be used for testing Mqtt under load.

In order to build the source, you should have the latest version of Go (go1.15) installed. I recommend you to have gvm installed.

k6 version

This extension is uptodate with k6 version v0.38.0 (breaking change) previous version is tagged at tag v0.37.0

Build

To build a k6 binary with this extension, first ensure you have the prerequisites:

Then, install xk6 and build your custom k6 binary with the Mqtt extension:

  1. Install xk6:
$ go install go.k6.io/xk6/cmd/xk6@latest
  1. Build the binary: from latest version
$ xk6 build --with github.com/ddegasperi/xk6-mqtt@latest
  1. Build the binary: from source code
git clone github.com/ddegasperi/xk6-mqtt
cd xk6-mqtt
$ xk6 build --with github.com/ddegasperi/xk6-mqtt=.

Run & Test

First, you need to have your Mqtt development environment setup.

For example you can use vernemq

docker run -p 1883:1883 -e "DOCKER_VERNEMQ_ACCEPT_EULA=yes" -e DOCKER_VERNEMQ_ALLOW_ANONYMOUS=on --name vernemq -d vernemq/vernemq
k6 Test

The test file does the following.

Creates per VU (concurrent clients) :

  • one topic
  • one subscribe connection (done at first iteration)
  • one publish connection (done at first iteration)

Per iteration : Subscribe to topic Publish to topic Read and check message result

You can run the test using the following command:

./k6 run --vus 50 --duration 1m test.js

And here's the test result output:

          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  (‾)  |
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: test.js
     output: -

  scenarios: (100.00%) 1 scenario, 50 max VUs, 1m30s max duration (incl. graceful stop):
           * default: 50 looping VUs for 1m0s (gracefulStop: 30s)


running (1m00.0s), 00/50 VUs, 326051 complete and 0 interrupted iterations
default ✓ [======================================] 50 VUs  1m0s

     ✓ is pub connected
     ✓ is sub connected
     ✓ is subscribed
     ✓ is sent
     ✓ is received
     ✓ is content correct

     █ teardown

     checks...............: 100.00% ✓ 1956306     ✗ 0
     data_received........: 14 MB   238 kB/s
     data_sent............: 14 MB   238 kB/s
     iteration_duration...: avg=9.19ms min=9.17µs med=8.39ms max=141.6ms p(90)=12.8ms p(95)=16.3ms
     iterations...........: 326051  5433.374851/s
     publish_time.........: avg=3.23ms min=0s     med=3ms    max=41ms    p(90)=5ms    p(95)=6ms
     subscribe_time.......: avg=4.94ms min=0s     med=5ms    max=51ms    p(90)=7ms    p(95)=9ms
     vus..................: 50      min=50        max=50
     vus_max..............: 50      min=50        max=50


Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorConnect      = errors.New("connection failed")
	ErrorState        = errors.New("invalid state")
	ErrorClient       = errors.New("invalid client given")
	ErrorTimeout      = errors.New("operation timeout")
	ErrorSubscribe    = errors.New("subscribe failure")
	ErrorConsumeToken = errors.New("invalid consume token")
	ErrorPublish      = errors.New("publish failure")
)

Functions

This section is empty.

Types

type Mqtt

type Mqtt struct {
	// contains filtered or unexported fields
}

TODO improve this StartTime is not a good way Mqtt is the objet to be used in tests

func (*Mqtt) Close

func (m *Mqtt) Close(

	client paho.Client,

	timeout uint,
)

Close the given client

func (*Mqtt) Connect

func (m *Mqtt) Connect(

	servers []string,

	user,

	password string,

	cleansess bool,

	clientid string,

	timeout uint,

	enableSsl bool,

	certPath string,

) paho.Client

Connect create a connection to mqtt

func (*Mqtt) Consume

func (m *Mqtt) Consume(
	token chan paho.Message,

	timeout uint,
) string

Consume will wait for one message to arrive

func (*Mqtt) Publish

func (m *Mqtt) Publish(
	client paho.Client,
	topic string,
	qos int,
	message string,
	retain bool,
	timeout int,
)

Publish allow to publish one message

func (*Mqtt) Subscribe

func (m *Mqtt) Subscribe(

	client paho.Client,

	topic string,

	qos,

	timeout uint,
) chan paho.Message

Subscribe to the given topic and returns a channel waiting for the message

type MqttInstance

type MqttInstance struct {

	// Mqtt is the exported module instance.
	*Mqtt
	// contains filtered or unexported fields
}

Mqtt represents an instance of the JS module.

func (*MqttInstance) Exports

func (m *MqttInstance) Exports() modules.Exports

type MqttModule

type MqttModule struct{}

MqttModule is the global module instance that will create Mqtt instances for each VU.

func New

func New() *MqttModule

New returns a pointer to a new RootModule instance.

func (*MqttModule) NewModuleInstance

func (*MqttModule) NewModuleInstance(vu modules.VU) modules.Instance

a new instance for each VU.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL