You've already forked request-coalescing-py
mirror of
https://github.com/hexolan/request-coalescing-py.git
synced 2026-03-26 10:11:16 +00:00
initial commit
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
24
tests/conftest.py
Normal file
24
tests/conftest.py
Normal 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
25
tests/test_coalesced.py
Normal 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
25
tests/test_standard.py
Normal 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())
|
||||
Reference in New Issue
Block a user