From 68df0e65a90db26c7d4d9d0eb38c5c730bb8ba8a Mon Sep 17 00:00:00 2001 From: Ahmed Sadman Date: Sun, 27 Oct 2019 00:29:54 +0600 Subject: [PATCH] added load testing --- requirements.txt | 13 ++++++++ tests/loadtest.py | 74 +++++++++++++++++++++++++++++++++++++++++ tests/loadtest_read.py | 42 +++++++++++++++++++++++ tests/loadtest_write.py | 47 ++++++++++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 tests/loadtest.py create mode 100644 tests/loadtest_read.py create mode 100644 tests/loadtest_write.py diff --git a/requirements.txt b/requirements.txt index 4407fd2..be5383d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,9 @@ appdirs==1.4.3 astroid==2.2.5 attrs==19.1.0 black==19.3b0 +certifi==2019.9.11 +cffi==1.13.1 +chardet==3.0.4 Click==7.0 colorama==0.4.1 Flask==1.1.1 @@ -11,28 +14,38 @@ Flask-JWT-Extended==3.22.0 Flask-Migrate==2.5.2 Flask-RESTful==0.3.7 Flask-SQLAlchemy==2.4.0 +gevent==1.4.0 +geventhttpclient-wheels==1.3.1.dev2 +greenlet==0.4.15 +idna==2.8 isort==4.3.21 itsdangerous==1.1.0 Jinja2==2.10.1 lazy-object-proxy==1.4.2 +locustio==0.12.1 Mako==1.1.0 MarkupSafe==1.1.1 marshmallow==3.0.0 mccabe==0.6.1 +msgpack-python==0.5.6 pep8==1.7.1 psycopg2==2.8.3 pycodestyle==2.5.0 +pycparser==2.19 PyJWT==1.7.1 python-dateutil==2.8.0 python-dotenv==0.10.3 python-editor==1.0.4 python-http-client==3.1.0 pytz==2019.2 +pyzmq==18.1.0 +requests==2.22.0 sendgrid==6.0.5 six==1.12.0 SQLAlchemy==1.3.6 toml==0.10.0 typed-ast==1.4.0 +urllib3==1.25.6 Werkzeug==0.15.5 wrapt==1.11.2 uwsgi \ No newline at end of file diff --git a/tests/loadtest.py b/tests/loadtest.py new file mode 100644 index 0000000..84cbace --- /dev/null +++ b/tests/loadtest.py @@ -0,0 +1,74 @@ +"""Tests server load by applying read/write operations""" + +from locust import HttpLocust, TaskSet, task +import random +import string + + +class UserBehavior(TaskSet): + EVENT_IDS = [1, 2, 3] + PAYMENT_STATUS = ["pending", "waiting", "ok"] + + def _random_string(self, n): + return "".join(random.choices(string.ascii_uppercase, k=n)) + + def generate_data(self): + return { + "participants": [ + { + "name": self._random_string(10), + "email": "{}@gmail.com".format(self._random_string(6)), + "tshirt_size": "l", + "institute": "IUT", + "contact_no": "01715002073", + }, + { + "name": self._random_string(10), + "email": "{}@gmail.com".format(self._random_string(6)), + "tshirt_size": "l", + "institute": "IUT", + "contact_no": "01715002073", + }, + ], + "team_name": self._random_string(5), + } + + @task + def index(self): + response = self.client.get("/") + + @task + def list_events(self): + response = self.client.get("/events") + + @task + def find_team_by_event(self): + event_id = random.choice(self.EVENT_IDS) + response = self.client.get("/team/find?event_id={}".format(event_id)) + + @task + def find_team_by_payment(self): + status = random.choice(self.PAYMENT_STATUS) + response = self.client.get( + "/team/find?payment_status={}".format(status) + ) + + @task + def find_participant_by_event(self): + event_id = random.choice(self.EVENT_IDS) + response = self.client.get( + "/participant/find?event_id={}".format(event_id) + ) + + @task + def event_register(self): + response = self.client.post( + "/event/register/1", json=self.generate_data() + ) + print(response) + + +class WebsiteUser(HttpLocust): + task_set = UserBehavior + min_wait = 3000 + max_wait = 7000 diff --git a/tests/loadtest_read.py b/tests/loadtest_read.py new file mode 100644 index 0000000..db1969e --- /dev/null +++ b/tests/loadtest_read.py @@ -0,0 +1,42 @@ +"""Tests server load by applying various read operations only""" + +from locust import HttpLocust, TaskSet, task +from random import choice + + +class UserBehavior(TaskSet): + EVENT_IDS = [1, 2, 3] + PAYMENT_STATUS = ["pending", "waiting", "ok"] + + @task(1) + def index(self): + response = self.client.get("/") + + @task(1) + def list_events(self): + response = self.client.get("/events") + + @task(2) + def find_team_by_event(self): + event_id = choice(self.EVENT_IDS) + response = self.client.get("/team/find?event_id={}".format(event_id)) + + @task(2) + def find_team_by_payment(self): + status = choice(self.PAYMENT_STATUS) + response = self.client.get( + "/team/find?payment_status={}".format(status) + ) + + @task(2) + def find_participant_by_event(self): + event_id = choice(self.EVENT_IDS) + response = self.client.get( + "/participant/find?event_id={}".format(event_id) + ) + + +class WebsiteUser(HttpLocust): + task_set = UserBehavior + min_wait = 2000 + max_wait = 5000 diff --git a/tests/loadtest_write.py b/tests/loadtest_write.py new file mode 100644 index 0000000..659cf5f --- /dev/null +++ b/tests/loadtest_write.py @@ -0,0 +1,47 @@ +"""Tests server load by applying various write operations only""" + +from locust import HttpLocust, TaskSet, task +import string +import random + + +class UserBehavior(TaskSet): + EVENT_IDS = [1, 2, 3] + PAYMENT_STATUS = ["pending", "waiting", "ok"] + + def _random_string(self, n): + return "".join(random.choices(string.ascii_uppercase, k=n)) + + def generate_data(self): + return { + "participants": [ + { + "name": self._random_string(10), + "email": "{}@gmail.com".format(self._random_string(6)), + "tshirt_size": "l", + "institute": "IUT", + "contact_no": "01715002073", + }, + { + "name": self._random_string(10), + "email": "{}@gmail.com".format(self._random_string(6)), + "tshirt_size": "l", + "institute": "IUT", + "contact_no": "01715002073", + }, + ], + "team_name": self._random_string(5), + } + + @task + def event_register(self): + response = self.client.post( + "/event/register/1", json=self.generate_data() + ) + print(response) + + +class WebsiteUser(HttpLocust): + task_set = UserBehavior + min_wait = 2000 + max_wait = 5000