ci: add startup memory budget check

This commit is contained in:
Luna Reed 2026-02-18 02:05:27 +08:00
parent ba47892bcf
commit 58133ca4bb
3 changed files with 59 additions and 2 deletions

View file

@ -56,3 +56,18 @@ jobs:
- name: Run go test - name: Run go test
run: go test ./... run: go test ./...
memory-budget:
runs-on: ubuntu-latest
needs: fmt-check
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Check startup memory budget
run: make memory-check

View file

@ -1,4 +1,4 @@
.PHONY: all build install uninstall clean help test .PHONY: all build install uninstall clean help test memory-check
# Build variables # Build variables
BINARY_NAME=picoclaw BINARY_NAME=picoclaw
@ -126,10 +126,14 @@ clean:
vet: vet:
@$(GO) vet ./... @$(GO) vet ./...
## fmt: Format Go code ## test: Run unit tests
test: test:
@$(GO) test ./... @$(GO) test ./...
## memory-check: Validate startup memory stays under budget (default 20MB RSS)
memory-check:
@bash ./scripts/memory_budget_check.sh
## fmt: Format Go code ## fmt: Format Go code
fmt: fmt:
@$(GO) fmt ./... @$(GO) fmt ./...

38
scripts/memory_budget_check.sh Executable file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BINARY_PATH="${PICOCLAW_BINARY_PATH:-${ROOT_DIR}/build/picoclaw}"
BUDGET_KB="${PICOCLAW_MEMORY_BUDGET_KB:-20480}"
if [[ ! -x "${BINARY_PATH}" ]]; then
echo "[memory-check] binary not found at ${BINARY_PATH}; building..."
make -C "${ROOT_DIR}" build >/dev/null
fi
tmp_time_out="$(mktemp)"
trap 'rm -f "${tmp_time_out}"' EXIT
if [[ "$(uname -s)" == "Darwin" ]]; then
/usr/bin/time -l "${BINARY_PATH}" version >/dev/null 2>"${tmp_time_out}"
rss_bytes="$(awk '/maximum resident set size/{print $1}' "${tmp_time_out}" | tail -n1)"
rss_kb="$((rss_bytes / 1024))"
else
/usr/bin/time -v "${BINARY_PATH}" version >/dev/null 2>"${tmp_time_out}"
rss_kb="$(awk -F: '/Maximum resident set size/{gsub(/^[ \t]+/, "", $2); print $2}' "${tmp_time_out}" | tail -n1)"
fi
if [[ -z "${rss_kb}" ]]; then
echo "[memory-check] failed to parse peak RSS from /usr/bin/time output"
cat "${tmp_time_out}"
exit 1
fi
echo "[memory-check] peak RSS: ${rss_kb} KiB (budget: ${BUDGET_KB} KiB)"
if (( rss_kb > BUDGET_KB )); then
echo "[memory-check] FAILED: peak RSS exceeds budget"
exit 1
fi
echo "[memory-check] PASS"