Update code in endpoints/api/load-test.post.py
This commit is contained in:
parent
99be160112
commit
00ca519156
40
endpoints/api/load-test.post.py
Normal file
40
endpoints/api/load-test.post.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from core.database import fake_users_db
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/api/load-test")
|
||||||
|
async def load_test_handler(
|
||||||
|
delay_ms: int = 0,
|
||||||
|
error_rate: float = 0.0,
|
||||||
|
response_size: int = 1
|
||||||
|
):
|
||||||
|
"""Load test endpoint for performance testing"""
|
||||||
|
|
||||||
|
# Simulate random errors based on error_rate
|
||||||
|
if random.random() < error_rate:
|
||||||
|
raise HTTPException(status_code=500, detail="Random error for load testing")
|
||||||
|
|
||||||
|
# Simulate processing delay
|
||||||
|
if delay_ms > 0:
|
||||||
|
time.sleep(delay_ms / 1000)
|
||||||
|
|
||||||
|
# Generate response data of specified size
|
||||||
|
test_data = [{
|
||||||
|
"index": i,
|
||||||
|
"value": f"test_value_{i}",
|
||||||
|
"timestamp": time.time()
|
||||||
|
} for i in range(response_size)]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"message": "Load test completed successfully",
|
||||||
|
"data": test_data,
|
||||||
|
"metadata": {
|
||||||
|
"delay_ms": delay_ms,
|
||||||
|
"error_rate": error_rate,
|
||||||
|
"response_size": response_size,
|
||||||
|
"timestamp": time.time()
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user