concurrencylabs / mqtt-locust

Use locust.io to load test IoT applications running on the AWS IoT service
MIT License
50 stars 18 forks source link

For more details, read this article: https://www.concurrencylabs.com/blog/hatch-a-swarm-of-things-using-locust-and-ec2/

Load test for AWS IoT applications using Locust.io and Paho MQTT client

This repo is based on code found originally here

Architecture

IoT Locust Architecture

This setup creates virtual IoT devices using Locust, which publishes messages to the AWS IoT message broker. Once messages arrive to AWS IoT, you can configure rules that integrate with other AWS services, such as Dynamo DB, Kinesis, Lambda, S3, SNS, SQS or CloudWatch.

Dependencies

mqtt_locust.py

This file contains logic to create an MQTT client, initiate a connection, publish messages to a topic and subscribe to a topic. It also reports failure or success results to Locust.

At the time, it only supports QoS=0, which is what is needed for connecting to the AWS IoT service.

Another important feature is the support for TLSv1.2, which is required by AWS IoT in order to establish a secure connection. You must create certificate files for AWS IoT, attach an IAM policy to them in AWS and your locust tests must indicate the location of the certificate files in your system.

aws-iot-publish.py

This file creates a basic locust test that publishes messages to an AWS IoT topic. The payload is a random value between 0 and 10, which represents a temperature reading.

Certificates

Follow the instructions in the AWS IoT documentation in order to create certificate files. Follow the instructions here to create a CA certificate. Set environment variables CA_CERT, IOT_CERT and IOT_PRIVATE_KEY with the full location of your .pem files

Topics

Set the environment variable MQTT_TOPIC with the topic you want to publish messages to.

Running the test

First you need to get the endpoint AWS IoT has assigned to your account, using the AWS CLI: aws iot describe-endpoint

then run the locust command from your environment:

locust --host=<my AWSIoT endpoint> -f aws-iot-publish.py