Files
OSIT-AE-API-FastAPI/tests/e2e/test_e2e_redis_extensive.py

108 lines
3.8 KiB
Python

import requests
import json
import time
import random
# --- Configuration ---
BASE_URL = "https://dev-api.oneskyit.com"
AGENT_API_KEY = "PMM4n50teUCaOMMTN8qOJA"
def get_headers(account_id=None):
headers = {
"X-Aether-API-Key": AGENT_API_KEY,
"Content-Type": "application/json"
}
if account_id:
headers["x-account-id"] = account_id
else:
headers["x-no-account-id"] = "bypass"
return headers
def print_result(label, success, message=""):
"""Standardized output helper."""
status = "✅ PASS" if success else "❌ FAIL"
print(f"[{status}] {label} {message}")
def test_list_serialization_caching():
"""
Stress tests 'get_id_random' (Int -> String) by fetching a large batch of records.
Every record in the response requires multiple ID resolutions.
"""
print("\n--- Testing Bulk List Serialization (Int -> String Caching) ---")
url = f"{BASE_URL}/v3/crud/hosted_file/"
# Pass 1: Cold Cache (Populate)
print(" Running Pass 1 (Cold Cache / SQL heavy)...")
start = time.time()
resp1 = requests.get(url, headers=get_headers(), params={"limit": 500})
dur1 = time.time() - start
print_result(f"Pass 1: Complete ({dur1:.2f}s)", resp1.status_code == 200)
# Pass 2: Hot Cache (Should be faster)
print(" Running Pass 2 (Hot Cache / Redis only)...")
start = time.time()
resp2 = requests.get(url, headers=get_headers(), params={"limit": 500})
dur2 = time.time() - start
speed_up = ((dur1 - dur2) / dur1) * 100 if dur1 > 0 else 0
print_result(f"Pass 2: Complete ({dur2:.2f}s) - {speed_up:.1f}% faster", resp2.status_code == 200)
# Verify ID Vision in results
if resp2.status_code == 200:
data = resp2.json().get('data', [])
if data:
item = data[0]
# Account ID can be None for global records
vision_ok = isinstance(item.get('id'), str)
print_result("Vision Compliance: Random IDs only", vision_ok)
def test_individual_id_resolution_caching():
"""
Stress tests 'redis_lookup_id_random' (String -> Int) by performing many individual lookups.
"""
print("\n--- Testing Individual ID Resolution (String -> Int Caching) ---")
# First, get a batch of valid random IDs
url = f"{BASE_URL}/v3/crud/hosted_file/"
resp = requests.get(url, headers=get_headers(), params={"limit": 100})
if resp.status_code != 200:
print_result("Setup: Failed to fetch test IDs", False)
return
test_ids = [item['id'] for item in resp.json().get('data', [])]
random.shuffle(test_ids)
print(f" Performing {len(test_ids)} individual lookups...")
# Pass 1: Cold/Mixed
start = time.time()
success_count = 0
for rid in test_ids:
r = requests.get(f"{url}{rid}", headers=get_headers())
if r.status_code == 200: success_count += 1
dur1 = time.time() - start
print_result(f"Pass 1: Complete ({dur1:.2f}s)", success_count == len(test_ids))
# Pass 2: Hot Cache
start = time.time()
success_count = 0
for rid in test_ids:
r = requests.get(f"{url}{rid}", headers=get_headers())
if r.status_code == 200: success_count += 1
dur2 = time.time() - start
speed_up = ((dur1 - dur2) / dur1) * 100 if dur1 > 0 else 0
print_result(f"Pass 2: Complete ({dur2:.2f}s) - {speed_up:.1f}% faster", success_count == len(test_ids))
if __name__ == "__main__":
print(f"=== Aether Redis Caching Extensive Stress Test ===")
print(f"Target: {BASE_URL}")
overall_start = time.time()
try:
test_list_serialization_caching()
test_individual_id_resolution_caching()
except Exception as e:
print(f"💥 Suite Error: {e}")
print(f"\nExtensive Suite completed in {time.time() - overall_start:.2f}s")