BuddyMath / tests /archive_scripts /run_100_stress.py
dotandru's picture
Fix: Clean production deployment with sse-starlette
9d29c62
Raw
History Blame Contribute Delete
3.34 kB
import asyncio
import cv2
import numpy as np
import time
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def create_mock_image(text: str) -> bytes:
img = np.zeros((200, 600, 3), dtype=np.uint8)
cv2.putText(img, text, (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
success, encoded_image = cv2.imencode('.jpg', img)
if not success:
raise ValueError("Failed to encode image")
return encoded_image.tobytes()
smadar_img_bytes = create_mock_image("4x + 5y = 120, y = x + 6")
circle_img_bytes = create_mock_image("(x-3)**2 + (y-4)**2 = 25")
def run_stress_test():
print("๐Ÿš€ Starting 100-Request OpenCV Stress Test Matrix...")
print("Expected Triggers: Quota Limits (429), Logic Errors, Valid Passes\n")
stats = {
"success": 0,
"logic_error_handled": 0,
"quota_exceeded": 0,
"uncaught_exceptions": 0
}
start_time = time.time()
for i in range(1, 101):
print(f"--- Request {i}/100 ---")
is_smadar = (i % 2 != 0)
img_bytes = smadar_img_bytes if is_smadar else circle_img_bytes
grade = "ื–'" if is_smadar else "ื™'"
files = {'file': ('test.jpg', img_bytes, 'image/jpeg')}
data = {
'user': f'stress_user_{i % 5}', # Distribute across 5 mock users to eventual hit quotas
'grade': grade,
'student_gender': 'M',
'mode': 'solve'
}
try:
resp = client.post("/solve_stream", data=data, files=files)
if resp.status_code == 429:
print(" ๐Ÿ›ก๏ธ Quota Exceeded handled correctly (429)")
stats["quota_exceeded"] += 1
elif resp.status_code == 200:
res_json = resp.json()
if res_json.get("logic_error"):
print(" ๐Ÿ›ก๏ธ Logic Error Handled (Firewall/Validator tripped safely)")
stats["logic_error_handled"] += 1
else:
print(" โœ… Valid Response Generated")
stats["success"] += 1
elif resp.status_code == 500:
print(f" โŒ CRITICAL: 500 Internal Server Error returned: {resp.text}")
stats["uncaught_exceptions"] += 1
else:
print(f" โš ๏ธ Unexpected Status Code: {resp.status_code}")
stats["uncaught_exceptions"] += 1
except Exception as e:
print(f" โŒ Exception Burst: {e}")
stats["uncaught_exceptions"] += 1
print("\n" + "="*40)
print("๐Ÿ“Š FINAL STRESS TEST RESULTS ๐Ÿ“Š")
print(f"Total Requests: 100")
print(f"Successful Validations: {stats['success']}")
print(f"Safely Handled Logic Errors: {stats['logic_error_handled']}")
print(f"Safely Handled Quotas (429): {stats['quota_exceeded']}")
print(f"Unmanaged Exceptions/500s: {stats['uncaught_exceptions']}")
print("="*40)
print(f"Elapsed Time: {time.time() - start_time:.2f}s")
if stats["uncaught_exceptions"] == 0:
print("โœ… STRESS TEST PASSED - 100% STABILITY PROVEN")
else:
print("โŒ STRESS TEST FAILED - UNCAUGHT EXCEPTIONS DETECTED")
if __name__ == "__main__":
run_stress_test()