added: profiling toolchain (py-spy, pyinstrument, pytest-benchmark, memray, snakeviz)

New `profile` optional-deps group, opt-in Pyinstrument ASGI middleware
gated by DECNET_PROFILE_REQUESTS, bench marker + tests/perf/ micro-benchmarks
for repository hot paths, and scripts/profile/ helpers for py-spy/cProfile/memray.
This commit is contained in:
2026-04-17 13:13:00 -04:00
parent c1d8102253
commit 319c1dbb61
10 changed files with 278 additions and 4 deletions

17
scripts/profile/cprofile-cli.sh Executable file
View File

@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# Run a `decnet` subcommand under cProfile and write a .prof file for snakeviz.
# Usage: scripts/profile/cprofile-cli.sh services
# scripts/profile/cprofile-cli.sh status
set -euo pipefail
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <decnet-subcommand> [args...]" >&2
exit 1
fi
OUT="${OUT:-profiles/cprofile-$(date +%s).prof}"
mkdir -p "$(dirname "$OUT")"
python -m cProfile -o "${OUT}" -m decnet.cli "$@"
echo "Wrote ${OUT}"
echo "View with: snakeviz ${OUT}"

15
scripts/profile/memray-api.sh Executable file
View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
# Run the DECNET API under memray to capture an allocation profile.
# Stop with Ctrl-C; then render with `memray flamegraph <bin>`.
set -euo pipefail
HOST="${DECNET_API_HOST:-127.0.0.1}"
PORT="${DECNET_API_PORT:-8000}"
OUT="${OUT:-profiles/memray-$(date +%s).bin}"
mkdir -p "$(dirname "$OUT")"
echo "Starting uvicorn under memray -> ${OUT}"
python -m memray run -o "${OUT}" -m uvicorn decnet.web.api:app \
--host "${HOST}" --port "${PORT}" --log-level warning
echo "Render with: memray flamegraph ${OUT}"

18
scripts/profile/pyspy-attach.sh Executable file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# Attach py-spy to the running DECNET uvicorn worker(s) and record a flamegraph.
# Requires sudo on Linux because of kernel.yama.ptrace_scope=1 by default.
set -euo pipefail
DURATION="${DURATION:-30}"
OUT="${OUT:-profiles/pyspy-$(date +%s).svg}"
mkdir -p "$(dirname "$OUT")"
PID="$(pgrep -f 'uvicorn decnet.web.api' | head -n 1 || true)"
if [[ -z "${PID}" ]]; then
echo "No uvicorn worker found. Start the API first (e.g. 'decnet deploy ...')." >&2
exit 1
fi
echo "Attaching py-spy to PID ${PID} for ${DURATION}s -> ${OUT}"
sudo py-spy record -o "${OUT}" -p "${PID}" -d "${DURATION}" --subprocesses
echo "Wrote ${OUT}"