From 1d5a8242d0f5784ff72102f5d7d549b3a6eb853e Mon Sep 17 00:00:00 2001 From: Stephen Astels Date: Tue, 24 Oct 2023 13:58:12 -0400 Subject: [PATCH 1/5] soak test with sms --- scripts/soak_test/.env.example | 1 + scripts/soak_test/soak_test_send_email.py | 30 --------------- .../soak_test/soak_test_send_notification.py | 38 +++++++++++++++++++ 3 files changed, 39 insertions(+), 30 deletions(-) delete mode 100644 scripts/soak_test/soak_test_send_email.py create mode 100644 scripts/soak_test/soak_test_send_notification.py diff --git a/scripts/soak_test/.env.example b/scripts/soak_test/.env.example index 0da06802cc..6e797ff095 100644 --- a/scripts/soak_test/.env.example +++ b/scripts/soak_test/.env.example @@ -1,2 +1,3 @@ API_KEY= EMAIL_TEMPLATE_ID= +SMS_TEMPLATE_ID= diff --git a/scripts/soak_test/soak_test_send_email.py b/scripts/soak_test/soak_test_send_email.py deleted file mode 100644 index f9cefd88d1..0000000000 --- a/scripts/soak_test/soak_test_send_email.py +++ /dev/null @@ -1,30 +0,0 @@ -import os - -from dotenv import load_dotenv -from locust import HttpUser, constant_pacing, events, task -from soak_utils import url_with_prefix - -load_dotenv() - - -@events.init_command_line_parser.add_listener -def _(parser): - parser.add_argument("--ref", type=str, default="test", help="reference") - - -class NotifyApiUser(HttpUser): - wait_time = constant_pacing(1) # each user makes one post per second - - def __init__(self, *args, **kwargs): - self.host = url_with_prefix(self.host, "api") - - super(NotifyApiUser, self).__init__(*args, **kwargs) - self.headers = {"Authorization": f"apikey-v1 {os.getenv('API_KEY')}"} - self.email_address = "success@simulator.amazonses.com" - self.email_template = os.getenv("EMAIL_TEMPLATE_ID") - - @task(1) - def send_email(self): - reference_id = self.environment.parsed_options.ref - json = {"email_address": self.email_address, "template_id": self.email_template, "reference": reference_id} - self.client.post("/v2/notifications/email", json=json, headers=self.headers) diff --git a/scripts/soak_test/soak_test_send_notification.py b/scripts/soak_test/soak_test_send_notification.py new file mode 100644 index 0000000000..be8f3b0ece --- /dev/null +++ b/scripts/soak_test/soak_test_send_notification.py @@ -0,0 +1,38 @@ +import os + +from dotenv import load_dotenv +from locust import HttpUser, constant_pacing, events, task +from soak_utils import url_with_prefix + +load_dotenv() + + +@events.init_command_line_parser.add_listener +def _(parser): + parser.add_argument("--ref", type=str, default="test", help="reference") + parser.add_argument("--type", type=str, default="email", help="email or sms") + + +class NotifyApiUser(HttpUser): + wait_time = constant_pacing(1) # each user makes one post per second + + def __init__(self, *args, **kwargs): + self.host = url_with_prefix(self.host, "api") + + super(NotifyApiUser, self).__init__(*args, **kwargs) + self.headers = {"Authorization": f"apikey-v1 {os.getenv('API_KEY')}"} + self.email_template = os.getenv("EMAIL_TEMPLATE_ID") + self.sms_template = os.getenv("SMS_TEMPLATE_ID") + self.email_address = "success@simulator.amazonses.com" + self.phone_number = "+16135550123" # INTERNAL_TEST_NUMBER + self.reference_id = self.environment.parsed_options.ref + self.type = self.environment.parsed_options.type + + @task(1) + def send_notification(self): + if self.type == "email": + json = {"email_address": self.email_address, "template_id": self.email_template, "reference": self.reference_id} + self.client.post("/v2/notifications/email", json=json, headers=self.headers) + else: + json = {"phone_number": self.phone_number, "template_id": self.sms_template, "reference": self.reference_id} + self.client.post("/v2/notifications/sms", json=json, headers=self.headers) \ No newline at end of file From ed971c8f43539a2e1933e2eb3c9a80321dbdf7c5 Mon Sep 17 00:00:00 2001 From: Stephen Astels Date: Tue, 24 Oct 2023 14:02:10 -0400 Subject: [PATCH 2/5] change README --- scripts/soak_test/README.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/scripts/soak_test/README.md b/scripts/soak_test/README.md index a9e2ecb50e..af3023cfb4 100644 --- a/scripts/soak_test/README.md +++ b/scripts/soak_test/README.md @@ -5,7 +5,7 @@ The goal of this code is to do a soak test of api while we make significant application or infrastructure changes. There are two soak tests here: -- `soak_test_send_email.py` will POST an email to api every second. +- `soak_test_send_notification.py` will POST an email or SMS to api every second. - `soak_test_all_servers.py` will do a GET to all our servers (admin, api, dd-api, api-k8s, documentation), on average hitting each server once a second ## How to configure @@ -14,19 +14,21 @@ Run the setup.sh to install the python pre-requisites or run in the repo devcont Default configuration is in the `locust.conf` file. Note that the `host` is the base address of the system you are testing, for example `https://staging.notification.cdssandbox.xyz` **not** `https://api.staging.notification.cdssandbox.xyz`. The "api" prefix will be added in the code. -The python file `soak_test_send_email.py` requires environment variables `API_KEY` and `EMAIL_TEMPLATE_ID`. The template should have no variables. +The python file `soak_test_send_notification.py` requires environment variables `API_KEY`, `EMAIL_TEMPLATE_ID`, and `SMS_TEMPLATE_ID` . The template should have no personalisation variables. ``` API_KEY=gcntfy-notAKey-f6c7cc49-b5b7-4e67-a8ff-24f34be34523-f6c7cc49-b5b7-4e67-a8ff-24f34be34523 EMAIL_TEMPLATE_ID=f6c7cc49-b5b7-4e67-a8ff-24f34be34523 +SMS_TEMPLATE_ID=f6c7cc49-b5b7-4e67-aeef-24f34be34523 ``` These can be in a `.env` file in the soak_test directory. __See Last Pass note "Soak Test Staging API Key and Template" in Shared-New-Notify-Staging folder__ -Note that the default configuration in `locust.conf` is to send one email per second. - -You can supply a `--ref` option to `soak_test_send_email.py` that will set the notification's `client_reference`. This is useful in testing that all POSTs were processed successfully. +Notes: +- The default configuration in `locust.conf` is to send one email per second. +- You can supply a `--ref` option to `soak_test_send_notification.py` that will set the notification's `client_reference`. This is useful in testing that all POSTs were processed successfully. +- You can also supply a `--type` option that will allow you to choose between `email` and `sms` (default is `email`) ## How to run @@ -37,7 +39,7 @@ There are two ways to run Locust, with the UI or headless. Locally you can run the email soak test with: ```shell -locust -f ./soak_test_send_email.py --ref=soak-2023-05-30-A +locust -f ./soak_test_send_notification.py --ref=soak-2023-05-30-A ``` 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. @@ -53,7 +55,7 @@ locust -f ./soak_test_all_servers.py You can pass the necessary parameters to the command line to run in the headless mode. For example: ```shell -locust -f ./soak_test_send_email.py --headless --ref=soak-2023-05-30-A +locust -f ./soak_test_send_notification.py --headless --ref=soak-2023-05-30-A ``` The defaults in `locust.conf` may be overridden by command line options @@ -64,9 +66,9 @@ The server soak test can be run with locust -f ./soak_test_all_servers.py --headless ``` -## Checking if all emails were sent +## Checking if all notifications were sent -To check whether all the POSTs from `soak_test_send_email.py` made it into the database, run the "Soak test" query on blazer. The query is already in staging, or you can run: +To check whether all the POSTs from `soak_test_send_notification.py` made it into the database, run the "Soak test" query on blazer. The query is already in staging, or you can run: ```sql WITH From 1920060b967de8f5c453be4e63281c563d4d7f2b Mon Sep 17 00:00:00 2001 From: Stephen Astels Date: Tue, 24 Oct 2023 14:20:10 -0400 Subject: [PATCH 3/5] use flag --- scripts/soak_test/soak_test_send_notification.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/scripts/soak_test/soak_test_send_notification.py b/scripts/soak_test/soak_test_send_notification.py index be8f3b0ece..7e23b3e3d2 100644 --- a/scripts/soak_test/soak_test_send_notification.py +++ b/scripts/soak_test/soak_test_send_notification.py @@ -10,8 +10,7 @@ @events.init_command_line_parser.add_listener def _(parser): parser.add_argument("--ref", type=str, default="test", help="reference") - parser.add_argument("--type", type=str, default="email", help="email or sms") - + parser.add_argument("--sms", action='store_true', help="send sms") class NotifyApiUser(HttpUser): wait_time = constant_pacing(1) # each user makes one post per second @@ -26,13 +25,13 @@ def __init__(self, *args, **kwargs): self.email_address = "success@simulator.amazonses.com" self.phone_number = "+16135550123" # INTERNAL_TEST_NUMBER self.reference_id = self.environment.parsed_options.ref - self.type = self.environment.parsed_options.type + self.send_sms = self.environment.parsed_options.sms @task(1) def send_notification(self): - if self.type == "email": + if self.send_sms: + json = {"phone_number": self.phone_number, "template_id": self.sms_template, "reference": self.reference_id} + self.client.post("/v2/notifications/sms", json=json, headers=self.headers) + else: json = {"email_address": self.email_address, "template_id": self.email_template, "reference": self.reference_id} self.client.post("/v2/notifications/email", json=json, headers=self.headers) - else: - json = {"phone_number": self.phone_number, "template_id": self.sms_template, "reference": self.reference_id} - self.client.post("/v2/notifications/sms", json=json, headers=self.headers) \ No newline at end of file From dfce7489b227af23e7710f3cd832bb078ddd4cfc Mon Sep 17 00:00:00 2001 From: Stephen Astels Date: Tue, 31 Oct 2023 15:09:52 -0400 Subject: [PATCH 4/5] tweak README --- scripts/soak_test/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/soak_test/README.md b/scripts/soak_test/README.md index af3023cfb4..04804dde05 100644 --- a/scripts/soak_test/README.md +++ b/scripts/soak_test/README.md @@ -28,7 +28,7 @@ __See Last Pass note "Soak Test Staging API Key and Template" in Shared-New-Noti Notes: - The default configuration in `locust.conf` is to send one email per second. - You can supply a `--ref` option to `soak_test_send_notification.py` that will set the notification's `client_reference`. This is useful in testing that all POSTs were processed successfully. -- You can also supply a `--type` option that will allow you to choose between `email` and `sms` (default is `email`) +- You can also supply a `--sms` option that will sens sms instead of email. ## How to run @@ -39,7 +39,7 @@ There are two ways to run Locust, with the UI or headless. Locally you can run the email soak test with: ```shell -locust -f ./soak_test_send_notification.py --ref=soak-2023-05-30-A +locust -f ./soak_test_send_notification.py [--ref=soak-2023-05-30-A] [--sms] ``` 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. @@ -55,7 +55,7 @@ locust -f ./soak_test_all_servers.py You can pass the necessary parameters to the command line to run in the headless mode. For example: ```shell -locust -f ./soak_test_send_notification.py --headless --ref=soak-2023-05-30-A +locust -f ./soak_test_send_notification.py --headless [--ref=soak-2023-05-30-A] [--sms] ``` The defaults in `locust.conf` may be overridden by command line options From 6972523ff78d88a204fdf575670353b6a927272e Mon Sep 17 00:00:00 2001 From: Stephen Astels Date: Tue, 31 Oct 2023 15:13:14 -0400 Subject: [PATCH 5/5] format --- scripts/soak_test/soak_test_send_notification.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/soak_test/soak_test_send_notification.py b/scripts/soak_test/soak_test_send_notification.py index 7e23b3e3d2..d3cab98765 100644 --- a/scripts/soak_test/soak_test_send_notification.py +++ b/scripts/soak_test/soak_test_send_notification.py @@ -12,6 +12,7 @@ def _(parser): parser.add_argument("--ref", type=str, default="test", help="reference") parser.add_argument("--sms", action='store_true', help="send sms") + class NotifyApiUser(HttpUser): wait_time = constant_pacing(1) # each user makes one post per second @@ -23,7 +24,7 @@ def __init__(self, *args, **kwargs): self.email_template = os.getenv("EMAIL_TEMPLATE_ID") self.sms_template = os.getenv("SMS_TEMPLATE_ID") self.email_address = "success@simulator.amazonses.com" - self.phone_number = "+16135550123" # INTERNAL_TEST_NUMBER + self.phone_number = "+16135550123" # INTERNAL_TEST_NUMBER self.reference_id = self.environment.parsed_options.ref self.send_sms = self.environment.parsed_options.sms