testing/locust_file.py

71 lines
2.2 KiB
Python
Raw Permalink Normal View History

2018-12-09 22:23:42 +00:00
from locust import Locust, TaskSequence, task, HttpLocust, seq_task
import os
import json
import random
#HOST_NAME = os.environ['HOST']
class VikunjaTaskSet(TaskSequence):
2023-11-05 16:06:07 +00:00
project_ids = []
2018-12-09 22:23:42 +00:00
def on_start(self):
# Called everytime when a simulated user starts executing TaskSet class
self.headers = {
'accept': "application/json",
'content-type': "application/json",
'Authorization': ""
}
2019-01-17 13:41:56 +00:00
2018-12-09 22:23:42 +00:00
# Append your login or authentication keys to header
self.get_authentication_keys()
2023-11-05 16:06:07 +00:00
self.projects = {}
2018-12-09 22:23:42 +00:00
def get_authentication_keys(self):
2019-01-17 13:41:56 +00:00
response = self.client.post("/login", '{"username":"testuser' + str(random.randint(1, 100)) + '", "password":"1234"}', headers=self.headers)
2018-12-09 22:23:42 +00:00
data = json.loads(response.text)
self.headers["Authorization"] = 'Bearer ' + data["token"]
@seq_task(1)
2018-12-10 00:28:37 +00:00
@task(25)
2018-12-09 22:23:42 +00:00
def dashboard(self):
2018-12-09 23:36:00 +00:00
self.client.get("/tasks/all", headers=self.headers)
2023-11-05 16:06:07 +00:00
response = self.client.get("/projects", headers=self.headers)
self.projects = json.loads(response.text)
2018-12-09 22:23:42 +00:00
@seq_task(2)
2023-11-05 16:06:07 +00:00
@task(50)
def new_project(self):
response = self.client.put("/projects/",
data=json.dumps({"title": "project created by locust"}),
2018-12-09 22:23:42 +00:00
headers=self.headers)
data = json.loads(response.text)
2023-11-05 16:06:07 +00:00
self.project_ids.append(data["id"])
2018-12-09 22:23:42 +00:00
2018-12-09 23:36:00 +00:00
@seq_task(3)
@task(500)
2018-12-09 22:23:42 +00:00
def add_task(self):
2023-11-05 16:06:07 +00:00
self.client.put("/projects/" + str(random.choice(self.project_ids)) + "/tasks/",
2018-12-09 22:23:42 +00:00
data=json.dumps({"text": "task created by locust"}),
headers=self.headers)
2018-12-09 23:36:00 +00:00
@seq_task(4)
@task(2000)
2023-11-05 16:06:07 +00:00
def get_project(self):
self.client.get("/projects/" + str(random.choice(self.project_ids)),
2018-12-09 23:36:00 +00:00
headers=self.headers)
2018-12-09 22:23:42 +00:00
2023-11-05 16:07:48 +00:00
# More Test ideas:
# * Create tasks with reminders within the next minute -> async processing
# * Create tasks with overdue due dates
# * Deeply nested projects -> db structure
# * Add a lot of webhooks and make them fire
2018-12-09 22:23:42 +00:00
class VikunjaLocust(HttpLocust):
task_set = VikunjaTaskSet
min_wait = 100
2018-12-09 23:36:00 +00:00
max_wait = 1500