diff --git a/scripts/load_test/.env.example b/scripts/load_test/.env.example new file mode 100644 index 0000000000..50335a1d9e --- /dev/null +++ b/scripts/load_test/.env.example @@ -0,0 +1,7 @@ +API_KEY= +HIGH_PRIORITY_EMAIL_TEMPLATE_ID= +MEDIUM_PRIORITY_EMAIL_TEMPLATE_ID= +LOW_PRIORITY_EMAIL_TEMPLATE_ID= +HIGH_PRIORITY_SMS_TEMPLATE_ID= +MEDIUM_PRIORITY_SMS_TEMPLATE_ID= +LOW_PRIORITY_SMS_TEMPLATE_ID= diff --git a/scripts/load_test/README.md b/scripts/load_test/README.md new file mode 100644 index 0000000000..a44d36e2ef --- /dev/null +++ b/scripts/load_test/README.md @@ -0,0 +1,41 @@ +# Soak test + +## Goals + +The goal of this code is to do a realistic load test of api while we make significant application or infrastructure changes. + +## How to configure + +Run the setup.sh to install the python pre-requisites or run in the repo devcontainer. + +Default configuration is in the `locust.conf` file. + +The python file `load_test.py` requires environment variables as listed in `.env.example`. The templates should have no variables. + +__See Last Pass note "Load Test Variables" in Shared-New-Notify-Staging folder__ + + +## How to run + +There are two ways to run Locust, with the UI or headless. + +### With the UI + +Locally you can run the email soak test with: + +```shell +locust -f ./load_test.py +``` + +Follow the localhost address that the console will display to get to the UI. It will ask you how many total users and spawned users you want configured. Once setup, you can manually start the tests via the UI and follow the summary data and charts visually. + +### Headless, via the command line + +You can pass the necessary parameters to the command line to run in the headless mode. For example: + +```shell +locust -f ./load_test.py --headless +``` + +The defaults in `locust.conf` may be overridden by command line options + diff --git a/scripts/load_test/load_test.py b/scripts/load_test/load_test.py new file mode 100644 index 0000000000..6f1219f5d9 --- /dev/null +++ b/scripts/load_test/load_test.py @@ -0,0 +1,90 @@ +import csv +import os +from datetime import datetime +from io import StringIO +from typing import Iterator, List + +from dotenv import load_dotenv +from locust import HttpUser, constant_pacing, task + +load_dotenv() + + +def rows_to_csv(rows: List[List[str]]): + output = StringIO() + writer = csv.writer(output) + writer.writerows(rows) + return output.getvalue() + + +def job_lines(data: str, number_of_lines: int) -> Iterator[List[str]]: + return map(lambda n: [data], range(0, number_of_lines)) + + +class NotifyApiUser(HttpUser): + wait_time = constant_pacing(1) # do something every second + + def __init__(self, *args, **kwargs): + super(NotifyApiUser, self).__init__(*args, **kwargs) + + self.headers = {"Authorization": f"apikey-v1 {os.getenv('API_KEY')}"} + self.email_address = "success@simulator.amazonses.com" + self.phone_number = "16135550123" # INTERNAL_TEST_NUMBER, does not actually send SMS + self.high_priority_email_template = os.getenv("HIGH_PRIORITY_EMAIL_TEMPLATE_ID") + self.medium_priority_email_template = os.getenv("MEDIUM_PRIORITY_EMAIL_TEMPLATE_ID") + self.low_priority_email_template = os.getenv("LOW_PRIORITY_EMAIL_TEMPLATE_ID") + self.high_priority_sms_template = os.getenv("HIGH_PRIORITY_SMS_TEMPLATE_ID") + self.medium_priority_sms_template = os.getenv("MEDIUM_PRIORITY_SMS_TEMPLATE_ID") + self.low_priority_sms_template = os.getenv("LOW_PRIORITY_SMS_TEMPLATE_ID") + + def send_bulk_email(self, template: str, count: int): + json = { + "name": f"bulk emails {datetime.utcnow().isoformat()}", + "template_id": template, + "csv": rows_to_csv([["email address"], *job_lines(self.email_address, count)]) + } + self.client.post("/v2/notifications/bulk", json=json, headers=self.headers, timeout=60) + + def send_bulk_sms(self, template: str, count: int): + json = { + "name": f"bulk sms {datetime.utcnow().isoformat()}", + "template_id": template, + "csv": rows_to_csv([["phone_number"], *job_lines(self.phone_number, count)]) + } + self.client.post("/v2/notifications/bulk", json=json, headers=self.headers, timeout=60) + + # SMS Tasks + + @task(120) # about every 5 seconds + def send_high_priority_sms(self): + json = {"phone_number": self.phone_number, "template_id": self.high_priority_sms_template} + self.client.post("/v2/notifications/sms", json=json, headers=self.headers) + + @task(2) # about every 5 minutes + def send_medium_priority_sms(self): + self.send_bulk_sms(self.medium_priority_sms_template, 199) + + @task(1) # about every 10 minutes + def send_low_priority_sms(self): + self.send_bulk_sms(self.low_priority_sms_template, 1000) + + # Email Tasks + + @task(120) # about every 5 seconds + def send_high_priority_email(self): + json = {"email_address": self.email_address, "template_id": self.high_priority_email_template} + self.client.post("/v2/notifications/email", json=json, headers=self.headers) + + @task(2) # about every 5 minutes + def send_medium_priority_email(self): + self.send_bulk_email(self.medium_priority_email_template, 199) + + @task(1) # about every 10 minutes + def send_low_priority_emails(self): + self.send_bulk_email(self.low_priority_email_template, 10000) + + # Do nothing task + + @task(600 - 120 - 2 - 1 - 120 - 2 - 1) + def do_nothing(self): + pass diff --git a/scripts/load_test/locust.conf b/scripts/load_test/locust.conf new file mode 100644 index 0000000000..d88ebcd001 --- /dev/null +++ b/scripts/load_test/locust.conf @@ -0,0 +1,3 @@ +users=1 +stop-timeout=10 +host=https://api.staging.notification.cdssandbox.xyz diff --git a/scripts/load_test/setup.sh b/scripts/load_test/setup.sh new file mode 100755 index 0000000000..191b418eda --- /dev/null +++ b/scripts/load_test/setup.sh @@ -0,0 +1,2 @@ +#!/bin/bash +pip install locust python-dotenv