From 729cd6b47aed73135f86d1ef8e5280e4528f4332 Mon Sep 17 00:00:00 2001 From: mahesh-naxa Date: Tue, 17 Dec 2024 13:45:12 +0545 Subject: [PATCH 1/2] init: locustfile added --- scripts/locust/Dockerfile | 3 ++ scripts/locust/docker-compose.yml | 15 ++++++ scripts/locust/locust_requirements.txt | 0 scripts/locust/locustfile.py | 68 ++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 scripts/locust/Dockerfile create mode 100644 scripts/locust/docker-compose.yml create mode 100644 scripts/locust/locust_requirements.txt create mode 100644 scripts/locust/locustfile.py diff --git a/scripts/locust/Dockerfile b/scripts/locust/Dockerfile new file mode 100644 index 0000000000..1fe6880bc9 --- /dev/null +++ b/scripts/locust/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.11-slim +WORKDIR /app/locust +RUN pip install locust diff --git a/scripts/locust/docker-compose.yml b/scripts/locust/docker-compose.yml new file mode 100644 index 0000000000..51eaf47569 --- /dev/null +++ b/scripts/locust/docker-compose.yml @@ -0,0 +1,15 @@ +version: '3.8' + +services: + locust: + build: + context: . + dockerfile: Dockerfile + ports: + - "8089:8089" # Expose Locust web UI + volumes: + - ./:/app/locust + working_dir: /app/locust + entrypoint: locust -f /app/locust/locustfile.py + environment: + LOCUST_HOST: "https://tm.naxa.com.np" diff --git a/scripts/locust/locust_requirements.txt b/scripts/locust/locust_requirements.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/scripts/locust/locustfile.py b/scripts/locust/locustfile.py new file mode 100644 index 0000000000..9752a61b5b --- /dev/null +++ b/scripts/locust/locustfile.py @@ -0,0 +1,68 @@ +import os +from locust import HttpUser, TaskSet, task, between + +class ProjectAndComments(TaskSet): + @task + def get_project(self): + self.client.get("/api/v2/projects/114/") + + @task + def get_comments(self): + self.client.get("/api/v2/projects/114/comments/") + +class GetSimilarProjects(TaskSet): + @task + def get_similar_projects(self): + self.client.get("/api/v2/projects/queries/114/similar-projects/") + +class GetContributions(TaskSet): + @task + def get_contributions(self): + self.client.get("/api/v2/projects/114/contributions/") + +class GetContributionsByDay(TaskSet): + @task + def get_contributions_by_day(self): + self.client.get("/api/v2/projects/114/contributions/queries/day/") + +class GetStatistics(TaskSet): + @task + def get_statistics(self): + self.client.get("/api/v2/system/statistics/") + +class GetActionAny(TaskSet): + @task + def get_action_any(self): + self.client.get("/api/v2/projects/?action=any") + +# Mapping task names to classes +task_mapping = { + "project_and_comments": ProjectAndComments, + "similar_projects": GetSimilarProjects, + "contributions": GetContributions, + "contributions_by_day": GetContributionsByDay, + "statistics": GetStatistics, + "action_any": GetActionAny, +} + +# User class +class ApiBenchmarkUser(HttpUser): + wait_time = between(1, 2) + + # Dynamically select tasks based on environment variable or CLI parameter + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + task_name = os.getenv("TASK_SET", "project_and_comments").lower() + self.tasks = [task_mapping.get(task_name, ProjectAndComments)] + + +''' +/api/v2/projects/?action=any&omitMapResults=true +/api/v2/projects/114/ +/api/v2/projects/114/comments/ +/api/v2/projects/queries/114/similar-projects/ +/api/v2/projects/114/contributions/ +/api/v2/projects/114/contributions/queries/day/ +/api/v2/system/statistics/ +/api/v2/projects/?action=any +''' From 682bb7ab5e702c8fbbc3eabb64b71627d3531849 Mon Sep 17 00:00:00 2001 From: prabinoid <38830224+prabinoid@users.noreply.github.com> Date: Tue, 24 Dec 2024 13:39:52 +0545 Subject: [PATCH 2/2] Locust and compose files for performance bench marking and its setup and removed old locust --- locust/locustfile.py | 22 ---------------------- scripts/locust/docker-compose.yml | 2 +- scripts/locust/locustfile.py | 10 ++++++++-- 3 files changed, 9 insertions(+), 25 deletions(-) delete mode 100644 locust/locustfile.py diff --git a/locust/locustfile.py b/locust/locustfile.py deleted file mode 100644 index 4fe23d12c7..0000000000 --- a/locust/locustfile.py +++ /dev/null @@ -1,22 +0,0 @@ -from locust import HttpUser, between, task - -class Project(HttpUser): - wait_time = between(1, 5) - @task - def index(self): - self.client.get("/api/v2/projects/") - - # @task - # def projects(self): - # # headers = { - # # 'Authorization': f'Token TVRBeU5UQTBOVFkuWmFpU2dBLmNKaFRpbjYyX0NnbjBJUnIzNXhqZlEtUHRXQQ==' - # # } - # # for i in range(95, 100): - # # self.client.get("/projects/{}".format(i)) - # self.client.get("/api/v2/projects/") - -class Country(HttpUser): - wait_time = between(1, 5) - @task - def projects(self): - self.client.get("/api/v2/countries/") diff --git a/scripts/locust/docker-compose.yml b/scripts/locust/docker-compose.yml index 51eaf47569..b161c5f7b5 100644 --- a/scripts/locust/docker-compose.yml +++ b/scripts/locust/docker-compose.yml @@ -12,4 +12,4 @@ services: working_dir: /app/locust entrypoint: locust -f /app/locust/locustfile.py environment: - LOCUST_HOST: "https://tm.naxa.com.np" + LOCUST_HOST: "https://tm-fastapi.naxa.com.np" diff --git a/scripts/locust/locustfile.py b/scripts/locust/locustfile.py index 9752a61b5b..7b95be3f3f 100644 --- a/scripts/locust/locustfile.py +++ b/scripts/locust/locustfile.py @@ -10,6 +10,11 @@ def get_project(self): def get_comments(self): self.client.get("/api/v2/projects/114/comments/") +class ProjectList(TaskSet): + @task + def get_project(self): + self.client.get("/api/v2/projects/") + class GetSimilarProjects(TaskSet): @task def get_similar_projects(self): @@ -52,8 +57,9 @@ class ApiBenchmarkUser(HttpUser): # Dynamically select tasks based on environment variable or CLI parameter def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - task_name = os.getenv("TASK_SET", "project_and_comments").lower() - self.tasks = [task_mapping.get(task_name, ProjectAndComments)] + task_name = os.getenv("TASK_SET", "get_contributions").lower() + print(task_name, "The task name....") + self.tasks = [task_mapping.get(task_name, GetContributions)] '''