initial commit

This commit is contained in:
2023-10-03 22:13:42 +01:00
commit 79ba18e15f
17 changed files with 1110 additions and 0 deletions

0
tests/__init__.py Normal file
View File

24
tests/conftest.py Normal file
View File

@@ -0,0 +1,24 @@
import pytest
from fastapi import FastAPI
from httpx import AsyncClient
from asgi_lifespan import LifespanManager
from request_coalescing_py.main import app
from request_coalescing_py.models import Item
@pytest.fixture
def anyio_backend() -> str:
return "asyncio"
@pytest.fixture(scope="function")
async def test_app() -> FastAPI:
async with LifespanManager(app) as manager:
yield manager.app
@pytest.fixture(scope="function")
async def client(test_app) -> AsyncClient:
async with AsyncClient(app=test_app, base_url="http://test") as client:
yield client

25
tests/test_coalesced.py Normal file
View File

@@ -0,0 +1,25 @@
import asyncio
from datetime import datetime
import pytest
@pytest.mark.anyio
async def test_coalesced_requests(client) -> None:
print("Making 5x100 concurrent requests (500 total)...")
async def make_requests():
for _ in range(100):
response = await client.get("/coalesced/1")
assert response.status_code == 200
# Make the requests concurrently and wait until they complete
start_time = datetime.now()
tasks = [asyncio.create_task(make_requests()) for _ in range(5)]
for task in tasks:
await task
print("Coalesced Requests: Took {delta}ms".format(delta=(datetime.now() - start_time).microseconds / 1000))
# View the metrics
metrics = await client.get("/metrics")
print("Coalesced Metrics:", metrics.json())

25
tests/test_standard.py Normal file
View File

@@ -0,0 +1,25 @@
import asyncio
from datetime import datetime
import pytest
@pytest.mark.anyio
async def test_standard_requests(client) -> None:
print("Making 5x100 concurrent requests (500 total)...")
async def make_requests():
for _ in range(100):
response = await client.get("/standard/1")
assert response.status_code == 200
# Make the requests concurrently and wait until they complete
start_time = datetime.now()
tasks = [asyncio.create_task(make_requests()) for _ in range(5)]
for task in tasks:
await task
print("Standard Requests: Took {delta}ms".format(delta=(datetime.now() - start_time).microseconds / 1000))
# View the metrics
metrics = await client.get("/metrics")
print("Standard Metrics:", metrics.json())