Skip to content

Commit 4469299

Browse files
committed
Add server profiling
1 parent 0ddaa13 commit 4469299

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

.gitignore

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@
33
*.egg-info
44

55
dist/
6+
build/
67
venv/
78
/site/
89
/.cache/
910
.pytest_cache/
1011
.coverage
1112

1213
.idea/
14+
.fleet
15+
.vscode
16+
.aider*
17+
.local/
18+
.DS_Store
19+
.env
20+
.envrc
21+
uv.lock
1322

1423
/runner/cmd/shim/shim
1524
/runner/cmd/runner/runner
1625

1726
/src/dstack/_internal/server/statics
1827

19-
build/
20-
.DS_Store
21-
.fleet
22-
.env
23-
.envrc
24-
.vscode
25-
.aider*
26-
uv.lock
27-
.local/
28+
profiling_results.html

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ dev = [
9191
"ruff==0.11.6", # should match .pre-commit-config.yaml
9292
"testcontainers>=4.9.2",
9393
"pytest-xdist>=3.6.1",
94+
"pyinstrument>=5.0.0",
9495
]
9596

9697
[project.optional-dependencies]

src/dstack/_internal/server/app.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,23 @@ async def log_request(request: Request, call_next):
245245
)
246246
return response
247247

248+
if settings.SERVER_PROFILING_ENABLED:
249+
from pyinstrument import Profiler
250+
251+
@app.middleware("http")
252+
async def profile_request(request: Request, call_next):
253+
profiling = request.query_params.get("profile", False)
254+
if profiling:
255+
profiler = Profiler()
256+
profiler.start()
257+
respone = await call_next(request)
258+
profiler.stop()
259+
with open("profiling_results.html", "w+") as f:
260+
f.write(profiler.output_html())
261+
return respone
262+
else:
263+
return await call_next(request)
264+
248265
# this middleware must be defined after the log_request middleware
249266
@app.middleware("http")
250267
async def log_http_metrics(request: Request, call_next):

src/dstack/_internal/server/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109

110110
SQL_ECHO_ENABLED = os.getenv("DSTACK_SQL_ECHO_ENABLED") is not None
111111

112+
SERVER_PROFILING_ENABLED = os.getenv("DSTACK_SERVER_PROFILING_ENABLED") is not None
113+
112114
UPDATE_DEFAULT_PROJECT = os.getenv("DSTACK_UPDATE_DEFAULT_PROJECT") is not None
113115
DO_NOT_UPDATE_DEFAULT_PROJECT = os.getenv("DSTACK_DO_NOT_UPDATE_DEFAULT_PROJECT") is not None
114116
SKIP_GATEWAY_UPDATE = os.getenv("DSTACK_SKIP_GATEWAY_UPDATE", None) is not None

0 commit comments

Comments
 (0)