From 71cf7f52812130484ad3cbdbda6ffd94daace666 Mon Sep 17 00:00:00 2001 From: Niklas Fondberg Date: Tue, 3 Mar 2020 11:20:28 +0100 Subject: [PATCH] Add locust perf testing --- README.md | 25 ++++++++++++------ locustfile.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 locustfile.py diff --git a/README.md b/README.md index 7924e3c..5a322ee 100644 --- a/README.md +++ b/README.md @@ -4,21 +4,23 @@ Provides a REST API to Photowall database. ## Installation -Make sure that your system has Python version 3.4 installed. If you dont have it here is how you install it on Ubuntu 12.04: -``` -$ sudo add-apt-repository ppa:fkrull/deadsnakes -$ sudo apt-get update -$ sudo apt-get install python3.4 python3.4-dev build-essential libpq-dev -``` +Make sure that your system has Python version 3.6 installed. -Next its recommended that you create a [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/) of python 3.4 and install all packages in it. +Next its recommended that you create a [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/) of python 3.6 and install all packages in it. To do that execute the following commands in your project folder: ``` -$ virtualenv -p /usr/bin/python3.4 venv +$ virtualenv -p /usr/bin/python3.6 venv $ source venv/bin/activate $ pip install -r requirements.txt ``` +## Local testing with docker +Make sure you edit `docker-compose.yml` with correct environment variables. +This will launch the production image and listen on port 1881. +``` +$ docker-compose up --force-recreate --build -d +``` + ## Documentation Documentation is available [here](docs) @@ -55,3 +57,10 @@ The staging branch is automatically synced from master on new commits to master. To run unit tests: `$ python -m unittest` + +## Performance testing with Locust +Run locally +``` +$ pip install locust +$ USERNAME=user locust --host=http://localhost:1881 +``` diff --git a/locustfile.py b/locustfile.py new file mode 100644 index 0000000..4556226 --- /dev/null +++ b/locustfile.py @@ -0,0 +1,70 @@ +import random +import os +from locust import HttpLocust, TaskSet, task, between + + +""" +## Performance testing with Locust +Run locally +``` +$ pip install locust +$ USERNAME=user locust --host=http://localhost:1880 +``` +""" +username = os.environ.get("USERNAME") +password = "" + +products = { + "58339", + "46768", + "48067", + "60643", + "62101", +} + +markets = { + "SE", + "GB", + "NO", + "DE" +} + +product_query = { + "wallpaper?width=100&height=100", + "canvas?width=100&height=70", + "poster?width=70&height=50", + "framed-print?sku=GLOBAL-CFP-20x28" +} + + +def random_product(): + return random.sample(products, 1)[0] + +def random_market(): + return random.sample(markets, 1)[0] + +def random_product_query(): + return random.sample(product_query, 1)[0] + + +class QueryTaskSet(TaskSet): + def on_start(self): + self.client.auth = (username, password) + + @task(10) + def product_price(self): + """ get random product with details""" + self.client.get("/prices/{0}&territory={1}&product={2}".format(random_product_query(), random_market(),random_product())) + + @task(1) + def stop(self): + self.interrupt() + + +class UserBehaviour(TaskSet): + tasks = {QueryTaskSet: 10} + + +class Api2Locust(HttpLocust): + task_set = UserBehaviour + wait_time = between(0.100, 3)