From 4478a0dea492d6c9141ddef68cc8ddd9f17e843b Mon Sep 17 00:00:00 2001 From: Matthias Theuermann Date: Fri, 12 Jan 2024 21:05:51 +0100 Subject: [PATCH] fix: added load generator --- .../kubernetes-deploy.sh | 18 ++++++- .../kubernetes/node-controller.yaml | 2 +- dapr-distributed-calendar/locust/ingress.yaml | 14 ++++++ dapr-distributed-calendar/locust/main.py | 49 +++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 dapr-distributed-calendar/locust/ingress.yaml create mode 100644 dapr-distributed-calendar/locust/main.py diff --git a/dapr-distributed-calendar/kubernetes-deploy.sh b/dapr-distributed-calendar/kubernetes-deploy.sh index a46b666..ae37ede 100755 --- a/dapr-distributed-calendar/kubernetes-deploy.sh +++ b/dapr-distributed-calendar/kubernetes-deploy.sh @@ -74,5 +74,21 @@ helm install redis bitnami/redis --namespace 12-factor-app --wait # deploy the 12-factor-app kubectl apply -f kubernetes/. +# setup locust for loadgeneration OPTIONAL +kubectl create configmap my-loadtest-locustfile --from-file locust/main.py -n 12-factor-app +helm repo add deliveryhero https://charts.deliveryhero.io/ +helm repo update +helm install locust deliveryhero/locust \ + --set loadtest.name=my-loadtest \ + --set loadtest.locust_locustfile_configmap=my-loadtest-locustfile \ + --set loadtest.locust_host=http://controller.12-factor-app:3000 \ + --set master.environment.LOCUST_RUN_TIME=1m \ + --set loadtest.environment.LOCUST_AUTOSTART="true" \ + --namespace 12-factor-app \ + --wait +kubectl apply -f locust/ingress.yaml + # get redis password (for manual interactions with the redis cli) OPTIONAL -kubectl get secret redis -n 12-factor-app -o jsonpath='{.data.redis-password}' | base64 --decode \ No newline at end of file +redis_pwd=$(kubectl get secret redis -n 12-factor-app -o jsonpath='{.data.redis-password}' | base64 --decode) +echo The redis password is $redis_pwd +echo To authenticate use: redis-cli -a $redis_pwd \ No newline at end of file diff --git a/dapr-distributed-calendar/kubernetes/node-controller.yaml b/dapr-distributed-calendar/kubernetes/node-controller.yaml index 7ef7756..4dce9a3 100644 --- a/dapr-distributed-calendar/kubernetes/node-controller.yaml +++ b/dapr-distributed-calendar/kubernetes/node-controller.yaml @@ -2,7 +2,7 @@ kind: Service apiVersion: v1 metadata: name: controller - # namespace: 12-factor-app + namespace: 12-factor-app labels: app: controller spec: diff --git a/dapr-distributed-calendar/locust/ingress.yaml b/dapr-distributed-calendar/locust/ingress.yaml new file mode 100644 index 0000000..81c3fe9 --- /dev/null +++ b/dapr-distributed-calendar/locust/ingress.yaml @@ -0,0 +1,14 @@ +apiVersion: traefik.containo.us/v1alpha1 +kind: IngressRoute +metadata: + name: locust-ingress + namespace: 12-factor-app +spec: + entryPoints: + - web + routes: + - match: Host(`locust.--01..`) && PathPrefix(`/`) + kind: Rule + services: + - name: locust + port: 8089 \ No newline at end of file diff --git a/dapr-distributed-calendar/locust/main.py b/dapr-distributed-calendar/locust/main.py new file mode 100644 index 0000000..40be6a6 --- /dev/null +++ b/dapr-distributed-calendar/locust/main.py @@ -0,0 +1,49 @@ +from locust import HttpUser, task, between + +default_headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'} + +class EventsUser(HttpUser): + wait_time = between(5, 10) # Adjust the wait time between tasks as needed + + @task + def event_lifecycle(self): + for event_id in range(100, 105): + # Create an event + headers = { + 'Content-Type': 'application/json', + } + + data = { + "data": { + "name": f"Event {event_id}", + "date": "TBD", + "id": str(event_id) + } + } + + # Send the POST request to create an event + response = self.client.post('/newevent', json=data, headers=headers) + + # Check if the request was successful + if response.status_code == 200: + print(f"Event {event_id} created successfully") + else: + print(f"Failed to create event {event_id}. Status code: {response.status_code}") + + # Get the event + response = self.client.get('/event/{event_id}') + + # Check if the request was successful + if response.status_code == 200: + print(f"Event {event_id} retrieved successfully") + else: + print(f"Failed to retrieve event {event_id}. Status code: {response.status_code}") + + # Delete the event + response = self.client.delete('/event/{event_id}') + + # Check if the request was successful + if response.status_code == 200: + print(f"Event {event_id} deleted successfully") + else: + print(f"Failed to delete event {event_id}. Status code: {response.status_code}")