Skip to content

Commit

Permalink
added distributive load tests and docker support
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu-wedensday committed Mar 20, 2024
1 parent cf8122e commit 24ad9f5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Config:


class FlagFeatureSettings(BaseSettings):
CACHE_ENABLED: bool
CACHE_ENABLED: bool = False
SENTRY_ENABLED: bool = False
SLACK_ENABLED: bool = False

Expand Down
17 changes: 16 additions & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,22 @@ services:
- /:/host:ro
networks:
- fast-api-network

locust-master:
image: locustio/locust
ports:
- "8089:8089" # Expose Locust's web interface on port 8089
volumes:
- ./loadtests:/mnt/locust # Mount the loadtests directory into the container
command: -f /mnt/locust --master -H http://app:"${SERVER_PORT}" # Start Locust in master mode, pointing to your FastAPI application
networks:
- fast-api-network
locust-worker:
image: locustio/locust
volumes:
- ./loadtests:/mnt/locust # Mount the loadtests directory into the container
command: -f /mnt/locust --worker --master-host locust-master
networks:
- fast-api-network
networks:
fast-api-network:
driver: bridge
Expand Down
5 changes: 5 additions & 0 deletions loadtests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from __future__ import annotations

from loadtests.locustfile import LocustRunner

__all__ = ["LocustRunner"]
11 changes: 9 additions & 2 deletions loadtests/home.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from __future__ import annotations

from locust import HttpUser
from locust import constant
from locust import task
from locust import TaskSet


class Home(HttpUser):
class Home(TaskSet):
wait_time = constant(1)

@task
def home_endpoint(self):
self.client.get("/api/home/")

@task
def stop(self):
self.interrupt()
8 changes: 8 additions & 0 deletions loadtests/locustfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from home import Home
from locust import HttpUser


class LocustRunner(HttpUser):
tasks = [Home]
9 changes: 9 additions & 0 deletions locust.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
locustfile = ./loadtests
headless = false
master = false
expect-workers = 5
host = http://0.0.0.0:8000
users = 10
spawn-rate = 10
run-time = 3m
tags = [Critical, Normal]
2 changes: 1 addition & 1 deletion scripts/load_tests.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
echo "Running load tests"
echo
locust -f loadtests --headless --users 10 --spawn-rate 1 -t 2m -H http://0.0.0.0:8000
locust

0 comments on commit 24ad9f5

Please sign in to comment.