Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,43 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
-
from fastapi.staticfiles import StaticFiles
|
| 4 |
-
import
|
| 5 |
-
import json
|
| 6 |
-
|
| 7 |
-
app = FastAPI()
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
"
|
| 21 |
-
"--
|
| 22 |
-
"--
|
| 23 |
-
"--
|
| 24 |
-
"--
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
import asyncio
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
app.mount("/frontend", StaticFiles(directory="frontend"), name="frontend")
|
| 10 |
+
|
| 11 |
+
@app.get("/", response_class=HTMLResponse)
|
| 12 |
+
async def root():
|
| 13 |
+
with open("frontend/index.html", "r") as f:
|
| 14 |
+
return f.read()
|
| 15 |
+
|
| 16 |
+
@app.get("/api/benchmark")
|
| 17 |
+
async def run_benchmark(orders: int = 1000000, cancel: float = 0.20, market: float = 0.10):
|
| 18 |
+
cmd = [
|
| 19 |
+
"./build/benchmark",
|
| 20 |
+
"--orders", str(orders),
|
| 21 |
+
"--cancel-ratio", str(cancel),
|
| 22 |
+
"--market-ratio", str(market),
|
| 23 |
+
"--json",
|
| 24 |
+
"--quiet"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
# BUG 4 FIXED: Using non-blocking asyncio subprocess
|
| 29 |
+
process = await asyncio.create_subprocess_exec(
|
| 30 |
+
*cmd,
|
| 31 |
+
stdout=asyncio.subprocess.PIPE,
|
| 32 |
+
stderr=asyncio.subprocess.PIPE
|
| 33 |
+
)
|
| 34 |
+
stdout, stderr = await process.communicate()
|
| 35 |
+
|
| 36 |
+
if process.returncode != 0:
|
| 37 |
+
return {"error": f"C++ Binary Crashed (Code {process.returncode}). Stderr: {stderr.decode()}"}
|
| 38 |
+
|
| 39 |
+
return json.loads(stdout.decode())
|
| 40 |
+
except json.JSONDecodeError:
|
| 41 |
+
return {"error": f"Invalid JSON output from C++: {stdout.decode()}"}
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return {"error": f"Server execution error: {str(e)}"}
|