diff --git a/kubernetes-manifests/loadgenerator.yaml b/kubernetes-manifests/loadgenerator.yaml new file mode 100644 index 0000000..7fa40a0 --- /dev/null +++ b/kubernetes-manifests/loadgenerator.yaml @@ -0,0 +1,27 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: loadgenerator +spec: + template: + metadata: + labels: + app: loadgenerator + spec: + initContainers: + - name: wait-frontend + image: alpine:3.6 + command: ['sh', '-c', 'until wget -qO- "http://${FRONTEND_ADDR}"; do echo "waiting for ${FRONTEND_ADDR}"; sleep 2; done;'] + containers: + - name: server + image: loadgenerator + env: + - name: FRONTEND_ADDR + value: "frontend:8080" + resources: + requests: + cpu: 100m + memory: 64Mi + limits: + cpu: 200m + memory: 128Mi diff --git a/skaffold.yaml b/skaffold.yaml index 45f2fa6..fb88de3 100644 --- a/skaffold.yaml +++ b/skaffold.yaml @@ -22,6 +22,8 @@ build: workspace: src/cartservice - imageName: frontend workspace: src/frontend + - imageName: loadgenerator + workspace: src/loadgenerator deploy: kubectl: manifests: diff --git a/src/loadgenerator/Dockerfile b/src/loadgenerator/Dockerfile new file mode 100644 index 0000000..69cf7b3 --- /dev/null +++ b/src/loadgenerator/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3 + +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY . . +ENTRYPOINT locust --host="http://${FRONTEND_ADDR}" --no-web -c=10 diff --git a/src/loadgenerator/locustfile.py b/src/loadgenerator/locustfile.py new file mode 100644 index 0000000..f56beba --- /dev/null +++ b/src/loadgenerator/locustfile.py @@ -0,0 +1,66 @@ +import random +from locust import HttpLocust, TaskSet + +products = [ + '0PUK6V6EV0', + '1YMWWN1N4O', + '2ZYFJ3GM2N', + '66VCHSJNUP', + '6E92ZMYYFZ', + '9SIQT8TOJO', + 'L9ECAV7KIM', + 'LS4PSXUNUM', + 'OLJCESPC7Z'] + +def index(l): + l.client.get("/") + +def setCurrency(l): + currencies = ['EUR', 'USD', 'JPY', 'CAD'] + l.client.post("/setCurrency", + {'currency_code': random.choice(currencies)}) + +def browseProduct(l): + l.client.get("/product/" + random.choice(products)) + +def viewCart(l): + l.client.get("/cart") + +def addToCart(l): + product = random.choice(products) + l.client.get("/product/" + product) + l.client.post("/cart", { + 'product_id': product, + 'quantity': random.choice([1,2,3,4,5,10])}) + +def checkout(l): + addToCart(l) + l.client.post("/cart/checkout", { + 'email': 'someone@example.com', + 'street_address': '1600 Amphitheatre Parkway', + 'zip_code': '94043', + 'city': 'Mountain View', + 'state': 'CA', + 'country': 'United States', + 'credit_card_number': '4432-8015-6152-0454', + 'credit_card_expiration_month': '1', + 'credit_card_expiration_year': '2019', + 'credit_card_cvv': '672', + }) + +class UserBehavior(TaskSet): + + def on_start(self): + index(self) + + tasks = {index: 1, + setCurrency: 2, + browseProduct: 10, + addToCart: 2, + viewCart: 3, + checkout: 1} + +class WebsiteUser(HttpLocust): + task_set = UserBehavior + min_wait = 1000 + max_wait = 2000 diff --git a/src/loadgenerator/requirements.txt b/src/loadgenerator/requirements.txt new file mode 100644 index 0000000..fa77c1c --- /dev/null +++ b/src/loadgenerator/requirements.txt @@ -0,0 +1,2 @@ +locustio==0.8.1 +pyzmq==17.0.0