Skip to content

Commit

Permalink
Merge pull request #6666 from hotosm/fastapi-refactor
Browse files Browse the repository at this point in the history
Fastapi refactor
  • Loading branch information
prabinoid authored Dec 24, 2024
2 parents bf44760 + 682bb7a commit 942a81e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 22 deletions.
22 changes: 0 additions & 22 deletions locust/locustfile.py

This file was deleted.

3 changes: 3 additions & 0 deletions scripts/locust/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM python:3.11-slim
WORKDIR /app/locust
RUN pip install locust
15 changes: 15 additions & 0 deletions scripts/locust/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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-fastapi.naxa.com.np"
Empty file.
74 changes: 74 additions & 0 deletions scripts/locust/locustfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 ProjectList(TaskSet):
@task
def get_project(self):
self.client.get("/api/v2/projects/")

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", "get_contributions").lower()
print(task_name, "The task name....")
self.tasks = [task_mapping.get(task_name, GetContributions)]


'''
/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
'''

0 comments on commit 942a81e

Please sign in to comment.