SPB Git

Repository Public

perf.sh 3.3 KB 43 lines shellscript

1#!/usr/bin/env bash2# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: Scripts/perf.sh4# Description: Performance measurements: launch-to-window time, idle memory, and an Instruments (xctrace Time Profiler) recording of a long streamed answer with CPU / memory sampling.5#6# Usage: Scripts/perf.sh launch [app]        cold launches of the given app (default: dist build), time until its window is on screen7#        Scripts/perf.sh stream              Debug build + DEVELOPMENT backend: records build/perf-stream.trace while a ~1 500-word answer streams8set -euo pipefail9cd "$(dirname "$0")/.."10window_up() { swift Scripts/window-shot.swift "SP Chat" /tmp/spchat-perf.png 2>/dev/null | grep -q "^saved"; }11case "${1:-launch}" in12  launch)13    APP="$(cd "$(dirname "${2:-/Applications/SP Chat.app}")" && pwd)/SP Chat.app"; BIN="SP Chat"; stop() { pkill -f "$APP/Contents/MacOS" 2>/dev/null || true; }; pid() { pgrep -f "$APP/Contents/MacOS" | head -1; }14    swiftc -O Scripts/window-shot.swift -o build/window-shot 2>/dev/null15    for run in 1 2 3 4 5; do16      stop; while [ -n "$(pid)" ]; do sleep 0.2; done; sleep 117      START=$(python3 -c 'import time; print(time.time())'); open -n "$APP"18      until build/window-shot "SP Chat" /tmp/spchat-perf.png 2>/dev/null | grep -q "^saved"; do sleep 0.05; done19      END=$(python3 -c 'import time; print(time.time())'); sleep 420      PID=$(pid); RSS=$(ps -o rss= -p "$PID" | tr -d ' ')21      FOOT=$(footprint "$PID" 2>/dev/null | sed -n 's/.*Footprint: \([0-9.]* [A-Z]*\).*/\1/p' | head -1)22      python3 -c "print(f'run $run: window after {($END-$START)*1000:.0f} ms · RSS {int($RSS)/1024:.0f} MB · footprint ${FOOT:-n/a}')"23    done24    stop ;;25  stream)26    eval "$(Scripts/dev-session.sh)"27    APP="${2:-$PWD/build/DerivedData/Build/Products/Debug/SP Chat.app}"; pkill -f "$APP/Contents/MacOS" 2>/dev/null || true; sleep 1; rm -rf build/perf-stream.trace28    PROMPT="Écris un exposé de 1500 mots sur l'histoire des phares, avec des titres, une liste, un tableau de cinq lignes et un bloc de code Python de dix lignes."29    open -n "$APP" --env SPCHAT_SERVER="$SPCHAT_SERVER" --env SPCHAT_SESSION_COOKIE="$SPCHAT_SESSION_COOKIE" --args -SPChatInMemory -SPChatAutoSend "$PROMPT"30    sleep 2; PID=$(pgrep -f "$APP/Contents/MacOS/SP Chat" | head -1); echo "pid $PID — Instruments attached, sampling CPU and memory every second"31    xcrun xctrace record --template 'Time Profiler' --time-limit 50s --output build/perf-stream.trace --attach "$PID" >/tmp/xctrace.log 2>&1 &32    for i in $(seq 1 46); do LC_ALL=C ps -o %cpu=,rss= -p "$PID" 2>/dev/null | LC_ALL=C awk -v t=$i '{printf "t=%02ds cpu=%5.1f%% rss=%4.0f MB\n", t, $1, $2/1024}'; sleep 1; done | tee build/perf-stream.txt | awk 'NR%5==0'33    wait || true34    /usr/bin/python3 - <<'PY'35import re36rows = [re.search(r"cpu=\s*([\d.]+)%\s+rss=\s*(\d+)", l) for l in open("build/perf-stream.txt")]37rows = [(float(m.group(1)), int(m.group(2))) for m in rows if m]38cpu = [c for c, _ in rows]39print(f"CPU moyen {sum(cpu) / max(len(cpu), 1):.1f} % · pic {max(cpu, default=0):.1f} % · mémoire max {max((r for _, r in rows), default=0)} MB (un cœur = 100 %, {len(rows)} échantillons)")40PY41    ls -d build/perf-stream.trace >/dev/null 2>&1 && echo "Instruments trace: build/perf-stream.trace (open with Instruments)" ;;42esac43
Files main
Menu

Theme