async def run_fastapi_benchmark_async(num_requests):
print(f"Starting FastAPI benchmark: {num_requests} requests to {FASTAPI_URL}...")
start_time = time.perf_counter()
+ results_list = [] # To store results for final count
- tasks = []
async with httpx.AsyncClient() as client:
- for i in range(num_requests):
- tasks.append(fetch_url_async(client, FASTAPI_URL))
- # Print status for each request initiated
- print(f"REQ_STATUS:FASTAPI_TASK_LAUNCHED_{i+1}", flush=True)
- results = await asyncio.gather(*tasks)
+ tasks = [fetch_url_async(client, FASTAPI_URL) for _ in range(num_requests)]
+ # Print a message indicating all tasks are launched and now awaiting completion.
+ # This might be redundant if the parent script already says "launched X/X tasks"
+ # but can be useful for the benchmark script's own log.
+ # print(f"FASTAPI_INFO: All {num_requests} tasks launched, awaiting completion...", flush=True)
+
+ completed_count = 0
+ for i, task_future in enumerate(asyncio.as_completed(tasks)):
+ try:
+ result = await task_future
+ results_list.append(result)
+ except Exception as e:
+ # print(f"FASTAPI_TASK_ERROR: Task {i+1} failed with {e}", flush=True) # Optional: log task errors
+ results_list.append(None) # Mark as failed
+ finally:
+ completed_count += 1
+ print(f"REQ_STATUS:FASTAPI_TASK_COMPLETED_{completed_count}", flush=True)
end_time = time.perf_counter()
total_time = end_time - start_time
- successful_requests = sum(1 for r in results if r == 200)
+ successful_requests = sum(1 for r in results_list if r == 200)
print(f"FastAPI benchmark: {successful_requests}/{num_requests} successful requests in {total_time:.2f} seconds.")
return total_time
FLASK_SERVER_URL = "http://127.0.0.1:3000/"
FASTAPI_SERVER_URL = "http://127.0.0.1:8000/"
BENCHMARK_SCRIPT_PATH = "benchmark/run_benchmark.py" # This script sends requests, delays are in apps
-NUM_REQUESTS_EXPECTED = 10000
+NUM_REQUESTS_EXPECTED = 1000
PYTHON_EXE = sys.executable
# ------------------------------------------------------------------------
if not line:
continue
- if line.startswith("REQ_STATUS:FASTAPI_TASK_LAUNCHED_"):
- # Extract count if needed, or just increment
- # current_task_num_str = line.split('_')[-1]
- # requests_done_count = int(current_task_num_str)
- requests_done_count += 1 # Increment for each launched task
- # Using carriage return to update the line in place
- print(f"\rFastAPI progress: Launched {requests_done_count}/{NUM_REQUESTS_EXPECTED} tasks...", end="", flush=True)
+ if line.startswith("REQ_STATUS:FASTAPI_TASK_COMPLETED_"):
+ # Extract count from the message, e.g., FASTAPI_TASK_COMPLETED_123
+ try:
+ current_task_num_str = line.split('_')[-1]
+ requests_done_count = int(current_task_num_str)
+ except (IndexError, ValueError):
+ # Fallback if parsing fails, though it shouldn't with the new format
+ requests_done_count += 1
+ print(f"\rFastAPI progress: Completed {requests_done_count}/{NUM_REQUESTS_EXPECTED} tasks...", end="", flush=True)
progress_line_printed = True
elif line.startswith("[DIAG-BRB-FASTAPI]"): # Placeholder for potential future diagnostics
if progress_line_printed:
print("\r" + " " * 80 + "\r", end="", flush=True)
print(line, flush=True)
if progress_line_printed:
- print(f"\rFastAPI progress: Launched {requests_done_count}/{NUM_REQUESTS_EXPECTED} tasks...", end="", flush=True)
+ # Reprint the progress line after diagnostic output
+ print(f"\rFastAPI progress: Completed {requests_done_count}/{NUM_REQUESTS_EXPECTED} tasks...", end="", flush=True)
elif "FastAPI benchmark:" in line: # Updated to match FastAPI summary line
final_summary_line = line
if progress_line_printed: