strawberry/scripts/perf_kpi.sh
trvon 2d5e05e8db
Add resource governor, scan profiles and KPI harness
PSI/cgroup-aware governor with efficient/balanced/aggressive profiles,
background pacing and thread priorities; timing histograms and optional
Tracy zones; a capped Docker KPI harness with report/compare tooling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:16 -06:00

78 lines
3.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# Constrained-lane KPI for the Strawberry daemon (modeled on ../yams
# tests/integration/p2p_mesh "constrained lane"): run the real daemon with the
# real rule set inside a CPU/memory-capped privileged container, drive a fixed
# workload, and record CPU, memory and the daemon's own timings.
#
# scripts/perf_kpi.sh [label] # one run -> build/perf/<stamp>-<label>/
# scripts/perf_kpi_report.py <dir> # summarize / check thresholds
# scripts/perf_kpi_report.py --compare <dirA> <dirB>
#
# The container is capped (KPI_CPUS, default 2; KPI_MEMORY, default 2g) and the
# docker client runs under nice, so the KPI cannot starve the host. Image builds
# are capped too. Linux only (eBPF + fanotify need --privileged).
set -euo pipefail
LABEL="${1:-run}"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
IMAGE_TAG="${IMAGE_TAG:-strawberry-linux-test}"
KPI_CPUS="${KPI_CPUS:-2}"
KPI_MEMORY="${KPI_MEMORY:-2g}"
STAMP="$(date +%Y%m%d-%H%M%S)"
OUT="$ROOT/build/perf/$STAMP-$LABEL"
RULES_SRC="${KPI_RULES:-/var/lib/strawberry/feeds/yara-forge-core/current}"
mkdir -p "$OUT/rules" "$OUT/state"
cp "$ROOT/config/example_rules.yar" "$OUT/rules/"
if [ -d "$RULES_SRC" ]; then
find -L "$RULES_SRC" -maxdepth 1 -name '*.yar' -exec cp {} "$OUT/rules/" \;
else
echo "warning: $RULES_SRC not found; using the 5 starter rules only (not representative)" >&2
fi
echo "rules: $(cat "$OUT"/rules/*.yar | grep -cE '^(private |global )*rule ')" > "$OUT/meta.txt"
git -C "$ROOT" rev-parse --short HEAD >> "$OUT/meta.txt" 2>/dev/null || true
echo "label=$LABEL cpus=$KPI_CPUS memory=$KPI_MEMORY profile=${KPI_PROFILE:-default}" >> "$OUT/meta.txt"
if [ "${KPI_SKIP_BUILD:-0}" != "1" ]; then
nice -n 19 docker build --cpu-quota=200000 --cpu-period=100000 --memory=4g \
--target test -t "$IMAGE_TAG" "$ROOT" > "$OUT/docker_build.log" 2>&1 \
|| { tail -20 "$OUT/docker_build.log"; exit 1; }
fi
cat > "$OUT/strawberry.conf" <<EOF
db_path=/kpi/state/database.db
policy_path=/kpi/state/policy.json
feeds_dir=/kpi/state/feeds
yara_rules_path=/kpi/rules
require_rules=true
enable_bpf_monitoring=true
scan_existing_processes_on_start=true
control_socket_path=/kpi/state/control.sock
control_group=
opensnitch_integration=false
${KPI_EXTRA_CONFIG:-}
EOF
cp "$ROOT/scripts/perf_kpi_workload.sh" "$OUT/workload.sh"
NAME="strawberry-kpi-$STAMP"
# Host-side sampler: docker stats every 2 s while the container runs.
(
echo "t,cpu_pct,mem"
while ! docker inspect "$NAME" >/dev/null 2>&1; do sleep 0.5; done
while docker inspect -f '{{.State.Running}}' "$NAME" 2>/dev/null | grep -q true; do
line=$(docker stats --no-stream --format '{{.CPUPerc}},{{.MemUsage}}' "$NAME" 2>/dev/null || true)
[ -n "$line" ] && echo "$(date +%s.%N),${line%%%*},$(echo "${line#*,}" | cut -d/ -f1 | tr -d ' ')"
sleep 2
done
) > "$OUT/docker_stats.csv" &
SAMPLER=$!
nice -n 19 docker run --rm --name "$NAME" --privileged \
--cpus="$KPI_CPUS" --memory="$KPI_MEMORY" --memory-swap="$KPI_MEMORY" \
-v /sys/kernel/tracing:/sys/kernel/tracing -v /sys/kernel/debug:/sys/kernel/debug \
-v "$OUT":/kpi -e STARTUP_S -e IDLE_S -e STORM_S -e LARGE_S -e RELOAD_S \
"$IMAGE_TAG" bash /kpi/workload.sh > "$OUT/workload.log" 2>&1 || echo "workload exit=$?" >> "$OUT/workload.log"
wait "$SAMPLER" 2>/dev/null || true
/usr/bin/env python3 -I "$ROOT/scripts/perf_kpi_report.py" "$OUT"