-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
60 lines (48 loc) · 1.5 KB
/
Copy pathserver.py
File metadata and controls
60 lines (48 loc) · 1.5 KB
1
2
3
4
5
6
7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from fastapi import FastAPI, HTTPException
from experiment import experiment
from pydantic import BaseModel
import asyncio
from concurrent.futures import ThreadPoolExecutor
import os
app = FastAPI()
executor = ThreadPoolExecutor(max_workers=4)
perf_folder = os.path.join('.', 'perf_data')
if not os.path.exists(os.path.join(perf_folder, "binary")):
os.makedirs(os.path.join(perf_folder, "binary"), exist_ok=True)
if not os.path.exists(os.path.join(perf_folder, "logs")):
os.makedirs(os.path.join(perf_folder, "logs"), exist_ok=True)
@app.get("/")
async def root():
return {"message": "Hello World"}
class ExperimentRequest(BaseModel):
scheduler: str
cpu: int
cpu_method: str
io: int
mem_load: int
duration: float
interval: float
@app.post("/experiment")
async def run_experiment(req: ExperimentRequest):
"""
Run an experiment and return the results.
Executes in a thread so the FastAPI server stays fast.
"""
try:
loop = asyncio.get_event_loop()
# Run experiment in background thread
result = await loop.run_in_executor(
executor,
lambda: experiment(
req.scheduler,
req.cpu,
req.cpu_method,
req.io,
req.mem_load,
req.duration,
req.interval
)
)
return {"status": "ok", "result": result}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))