Skip to content

Commit

Permalink
Add locust benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-selo committed Sep 30, 2024
1 parent 15a01a2 commit 2ca90d6
Show file tree
Hide file tree
Showing 6 changed files with 681 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ jobs:
with:
poetry-version: "1.5.1"
- run: poetry install
- run: poetry run black --check test_observer tests migrations scripts tasks
- run: poetry run ruff test_observer tests migrations scripts tasks
- run: poetry run mypy --explicit-package-bases test_observer tests migrations scripts tasks
- run: poetry run black --check --extend-exclude charm .
- run: poetry run ruff --extend-exclude charm .
- run: poetry run mypy --exclude charm --explicit-package-bases .
- run: poetry run pytest
env:
TEST_DB_URL: postgresql+pg8000://postgres:password@localhost:5432/postgres
Expand Down
16 changes: 16 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,19 @@ DOCKER_BUILDKIT=1 docker build . -t your-choice-of-tag -f backend/Dockerfile.pro
```

GitHub built images are also made available in the authorisation requiring private registry at `ghcr.io/canonical/test_observer/api` (every main branch commit and tag following the pattern `v*.*.*` gets stored there).

## Loading a database dump

If you have a database dump created using pg_dump, you can load it your local database instance in k8s. To do that simply run the following:

```bash
kubectl exec -i services/test-observer-db -- pg_restore -U postgres -d postgres --clean --if-exists --no-owner < path/to/db.dump
```

## Benchmarks

We use [locust](https://locust.io) to benchmark our API. Our benchmarks live in `locustfile.py` and can be run independantly via:

```bash
locust
```
35 changes: 35 additions & 0 deletions backend/locustfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import random
import time

from locust import HttpUser, between, task


class APIUser(HttpUser):
wait_time = between(1, 5)

@task
def normal_usage(self):
artefacts = self._get_artefacts()
time.sleep(1)
for artefact in random.choices(artefacts, k=10):
test_executions = self._get_test_executions(artefact)
time.sleep(1)
for te in random.choices(test_executions, k=30):
self._get_test_results(te)
time.sleep(1)

def _get_artefacts(self) -> list[dict]:
return self.client.get("/v1/artefacts").json()

def _get_test_executions(self, artefact: dict) -> list[dict]:
json = self.client.get(
f"/v1/artefacts/{artefact['id']}/builds",
name="/v1/artefacts/<id>/builds",
).json()
return [te for build in json for te in build["test_executions"]]

def _get_test_results(self, test_execution: dict) -> list[dict]:
return self.client.get(
f"/v1/test-executions/{test_execution['id']}/test-results",
name="/v1/test-executions/<id>/test-results",
).json()
Loading

0 comments on commit 2ca90d6

Please sign in to comment.