Skip to content

Commit

Permalink
Merge pull request #39 from wednesday-solutions/feat/load-testing
Browse files Browse the repository at this point in the history
feat - load testing added
  • Loading branch information
himanshu-wedensday authored Mar 20, 2024
2 parents f1ded38 + 1ba7693 commit 9788299
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 7 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This repository provides a template for creating and deploying a FastAPI project

- [Features](#features)
- [Getting started](#getting-started)
- [Advanced Usage](#advanced-usage)

### Features

Expand All @@ -28,6 +29,8 @@ This repository provides a template for creating and deploying a FastAPI project
- Application monitoring using Signoz
- Feature flagging added - User can enabled/disabled
- Database Monitoring using percona
- Loadtests using locust

### Getting Started

#### Requirements:
Expand Down Expand Up @@ -144,7 +147,9 @@ async def external_service_endpoint():
raise HTTPException(status_code=503, detail="Service temporarily unavailable")
```

# Using Signoz Monitoring Tool
### Advanced Usage

#### Using Signoz Monitoring Tool

To utilize Signoz for monitoring your applications, follow these steps:

Expand Down Expand Up @@ -179,7 +184,7 @@ To utilize Signoz for monitoring your applications, follow these steps:
OTEL_EXPORTER_OTLP_PROTOCOL=
```

## Logging with Signoz
#### Logging with Signoz

To enable logging with Signoz, follow these steps:

Expand All @@ -196,7 +201,8 @@ To enable logging with Signoz, follow these steps:
- Follow the instructions provided to configure log sending to Signoz.

By following these steps, you can effectively set up application monitoring and logging using Signoz for your Python FastAPI applications.
### Database Monitoring Using Percona

#### Database Monitoring Using Percona

To monitor your database using Percona, follow these steps:

Expand Down Expand Up @@ -230,8 +236,15 @@ To monitor your database using Percona, follow these steps:

By following these steps, you'll successfully configure Percona to monitor your MySQL database.
### Dashboard Links
#### Dashboard Links
- Percona: https://localhost:443
- flower: http://localhost:5556
- server: http://localhost:8000
- locust: http://localhost:8089
- server-healthcheck: http://localhost:8000/home
#### Useful scripts
- Tests - scripts/run_tests.sh
- Linting & Formatting - scripts/lint_and_format.sh
- Load tests - scripts/load_tests.sh (Change [locust.conf](https://docs.locust.io/en/stable/configuration.html) accordinly)
2 changes: 1 addition & 1 deletion app/middlewares/rate_limiter_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from app.config.redis_config import get_redis_pool

MAX_REQUESTS = 10
MAX_REQUESTS = 10000
TIME_WINDOW = 60


Expand Down
1 change: 0 additions & 1 deletion app/routes/users/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from app.daos.users import get_user as get_user_dao
from app.daos.users import list_users as list_users_dao
from app.daos.users import login as signin
from app.middlewares.request_id_injection import request_id_contextvar
from app.schemas.users.users_request import CreateUser
from app.schemas.users.users_request import Login
from app.schemas.users.users_response import UserOutResponse
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
Empty file added loadtests/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions loadtests/home.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import annotations

from locust import HttpUser
from locust import task


class Home(HttpUser):
@task
def home_endpoint(self):
self.client.get("/api/home/")
8 changes: 8 additions & 0 deletions locust.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
locustfile = ./loadtests
headless = true
master = false
expect-workers = 5
host = http://0.0.0.0:8000
users = 100
spawn-rate = 10
run-time = 10m
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ iniconfig==2.0.0
itsdangerous
joblib==1.2.0
langchain
locust>=2.0
lxml==4.9.2
lz4==4.3.2
Markdown==3.4.3
Expand Down
3 changes: 3 additions & 0 deletions scripts/load_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "Running load tests"
echo
locust

1 comment on commit 9788299

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
app
   app.py372824%12–67
app/config
   __init__.py3167%6
   base.py37295%53–54
   celery_config.py17170%1–27
   celery_utils.py21210%1–30
   redis_config.py5260%8–9
app/constants
   jwt_utils.py161131%11–23
app/daos
   home.py10550%11–18
   users.py706211%12–133
app/middlewares
   cache_middleware.py51510%1–67
   rate_limiter_middleware.py25250%1–35
   request_id_injection.py17170%1–25
app/models
   __init__.py330%1–5
   users.py27270%1–38
app/routes
   __init__.py12120%1–15
app/routes/cache_router
   __init__.py330%1–5
   cache_samples.py12120%1–18
app/routes/celery_router
   __init__.py330%1–5
   celery_samples.py12120%1–17
app/routes/home
   __init__.py330%1–5
   home.py33330%1–45
app/routes/users
   __init__.py330%1–5
   users.py37370%1–56
app/schemas/users
   users_request.py42420%1–73
   users_response.py10100%1–14
app/sessions
   db.py53530%1–82
app/tests
   test_basic.py201525%10–34
   test_daos_home.py231057%15–22, 28–37, 41
   test_daos_users.py1109514%19–208
app/utils
   exception_handler.py19190%1–36
   redis_utils.py440%1–7
   slack_notification_utils.py14140%1–32
   user_utils.py25250%1–36
app/wrappers
   cache_wrappers.py26260%1–32
TOTAL82070314% 

Tests Skipped Failures Errors Time
2 0 💤 0 ❌ 2 🔥 0.798s ⏱️

Please sign in to comment.