Run the CLI to automatically deploy the API on an AWS instance with Docker/Terraform and load test the API with Locust.
This is intended so you can test the same API across multiple instance types to identify the optimal instance type to use for your userbase.
Make sure you have locust
, terraform
, gcloud
, and aws
already installed and configured.
- For
gcloud
, make sure to have already rungcloud init
- For
aws
, make sure to have already runaws init
# Call python to run cli.py and specify the config.yaml path
# For AWS EC2
python ./cli.py -c ./aws_ec2.yaml
# For Google Compute Engine VM instance
python ./cli.py -c ./gcp_gce.yaml
This will create and start the AWS/GCE instance and load test it with the settings defined in config.yaml
The CLI is a simple wrapper around Locust and Terraform. It goes through the following steps:
- Parses configuration.
- Starts an AWS/GCE instance based on the config with Terraform.
- Waits until the instance is ready for load testing.
- Load tests with Locust according to the provided config.
- Writes stats and logs to files relative to this directory.
- Clean up the created AWS/GCE resources and instances with Terraform (
terraform destroy
).
This means that if you cancel the CLI midway, you could run into a scenario where resources are not cleaned up properly. If you do CTRL + C
during load testing, it will cancel load testing and go straight to Terraform cleanup steps.
If that still does not work, you could manually cd
into deploy/server
and call the terraform destroy
command yourself.
You can also run the Locust file locally:
# in this directory
locust
Or, you can locust headless:
# For local testing, we use localhost and make sure it has the forward slash
# For testing:
locust -f locustfile.py --host=http://127.0.0.1:5001/ --headless -u 1 -r 1 --run-time 5s --expect-workers=1
# For more realistic testing:
locust -f locustfile.py --host=http://127.0.0.1:5001/ --headless -u 100 -r 10 --run-time 30s --expect-workers=2
# Logging to file
locust -f locustfile.py --host=http://127.0.0.1:5001/ --headless -u 1 -r 1 --run-time 5s --expect-workers=1 --logfile="./locust.log"