#!/bin/bash set -euo pipefail cd "$(dirname "$0")/.." source Scripts/remote-sync.sh if [[ $# -lt 1 || $# -gt 3 ]]; then printf 'Usage: %s [bench|bench-record|bench-ab|smoothness|idle|soak] [BASE=ref]\n' "$0" >&2 exit 2 fi host=$1 task=${2:-bench} remote_validate_host "$host" case "$task" in bench|bench-record|bench-ab|smoothness|idle|soak) ;; *) printf 'Unknown bench task: %s\n' "$task" >&2; exit 2 ;; esac if [[ $# -eq 3 && ( $task != bench-ab || $3 != BASE=* ) ]]; then printf 'BASE=ref is only accepted for bench-ab.\n' >&2 exit 2 fi wait_value=${BENCH_WAIT:-15m} if [[ ! $wait_value =~ ^([0-9]+)(s|m)?$ ]]; then printf 'BENCH_WAIT must be an integer number of seconds or minutes (for example 15m).\n' >&2 exit 2 fi wait_seconds=$((10#${BASH_REMATCH[1]})) [[ ${BASH_REMATCH[2]:-} != m ]] || wait_seconds=$((wait_seconds * 60)) base_revision= if [[ $task == bench-ab ]]; then base=${3:-BASE=${BASE:-main}} base_revision=$(git merge-base HEAD "${base#BASE=}") fi remote_dir=$(remote_sync "$host") if [[ $task == bench-ab ]]; then bundle=$(mktemp "${TMPDIR:-/tmp}/parcel-bench-history.XXXXXX") trap 'rm -f "$bundle"' EXIT git bundle create "$bundle" HEAD rsync -az "$bundle" "$host:parcel-ci/$remote_dir/measurement-history.bundle" fi stamp=$(date -u +%Y%m%dT%H%M%SZ) destination="$PWD/build/remote/$host/$stamp" mkdir -p "$destination" # All inserted values are validated tokens; the script is sent verbatim, not shell-expanded. status=0 # shellcheck disable=SC2029 ssh "$host" "bash -s -- $remote_dir $task $wait_seconds ${base_revision:-none}" <<'REMOTE' || status=$? set -euo pipefail cd "$HOME/parcel-ci/$1" task=$2 wait_seconds=$3 base_revision=$4 mkdir -p build started=$SECONDS while true; do quiet_status=0 Scripts/quiet-check.sh --output build/bench-quiet.json || quiet_status=$? case "$quiet_status" in 0) break ;; 3) if (( SECONDS - started >= wait_seconds )); then printf 'Host did not become quiet before BENCH_WAIT expired.\n' >&2 exit 3 fi remaining=$((wait_seconds - (SECONDS - started))) delay=60 (( remaining >= delay )) || delay=$remaining sleep "$delay" ;; *) printf 'Quiet preflight failed with status %s; refusing measurement.\n' "$quiet_status" >&2; exit "$quiet_status" ;; esac done if [[ $task == bench-ab ]]; then git init --quiet git fetch --quiet measurement-history.bundle HEAD:refs/heads/measurement-head git symbolic-ref HEAD refs/heads/measurement-head git reset --mixed --quiet HEAD command=(Scripts/bench-ab.sh "$base_revision") else command=(make "$task") fi PARCEL_BENCH_QUIET="$PWD/build/bench-quiet.json" BENCH_STRICT_QUIET=1 \ caffeinate -i -s nice -n -5 "${command[@]}" REMOTE # Readings and failure artifacts are retrieved even if the task terminates unsuccessfully. # shellcheck disable=SC2029 artifacts=$(ssh "$host" "cd ~/parcel-ci/$remote_dir && for path in build/bench-results.json build/bench-quiet.json build/bench-ab build/smoothness build/idle build/soak build/traces; do if [ -e \"\$path\" ]; then printf '%s\\n' \"\$path\"; fi; done") while IFS= read -r artifact; do [[ -n $artifact ]] || continue case "$artifact" in build/bench-results.json|build/bench-quiet.json|build/bench-ab|build/smoothness|build/idle|build/soak|build/traces) ;; *) printf 'Unexpected artifact: %s\n' "$artifact" >&2; exit 2 ;; esac rsync -az "$host:parcel-ci/$remote_dir/$artifact" "$destination/" done <<< "$artifacts" if (( status != 0 )); then printf 'Remote task failed with status %s; artifacts: %s\n' "$status" "$destination" >&2 exit "$status" fi case "$task" in bench|bench-record) required=bench-results.json ;; bench-ab) required=bench-ab/summary.json ;; smoothness) required=smoothness ;; idle) required=idle ;; soak) required=soak ;; esac [[ -e $destination/$required ]] || { printf 'Required remote artifact missing: %s\n' "$required" >&2; exit 2; } if [[ $task == bench-record ]]; then # A fresh remote checkout can only produce this host's deliberate new baseline. # shellcheck disable=SC2029 baselines=$(ssh "$host" "cd ~/parcel-ci/$remote_dir && find Benchmarks/baselines -type f -name '*.json' -newer build/bench-quiet.json -print") [[ -n $baselines ]] || { printf 'No newly recorded remote baseline found.\n' >&2; exit 2; } while IFS= read -r baseline; do [[ $baseline =~ ^Benchmarks/baselines/[A-Za-z0-9,._-]+\.json$ ]] || { printf 'Unexpected baseline path.\n' >&2; exit 2; } rsync -az "$host:parcel-ci/$remote_dir/$baseline" "$baseline" printf 'Retrieved baseline for review (not staged): %s\n' "$baseline" done <<< "$baselines" fi printf 'Remote artifacts: %s\n' "$destination"