22
authorSam Mirazi <sasan345@gmail.com>
Mon, 2 Jun 2025 05:26:24 +0000 (22:26 -0700)
committerSam Mirazi <sasan345@gmail.com>
Mon, 2 Jun 2025 05:26:24 +0000 (22:26 -0700)
benchmark/run_benchmark.py
run_benchmark_NO_RESTRICTIONS.py

index 8f49393bd9d4177a5605d18e5a61a297ad311d6d..3aea1a78fae718be009624c1093b5e27fa3add6a 100644 (file)
@@ -59,18 +59,30 @@ async def fetch_url_async(client, url):
 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
 
index 5660507750b4ebbc59914e5cc63fa68b1c7e3996..f2caf196f2dbd16b3692b56d946183a0f6546708 100644 (file)
@@ -12,7 +12,7 @@ import os
 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
 
 # ------------------------------------------------------------------------
@@ -121,20 +121,23 @@ def run_benchmark_script(framework_arg):
                     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: