synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / scripts / board-bench.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Swarm board bench (issue #83). The rule is evidence/swarm-board/RULE.md (fixed before the |
| 3 | # run). Measures post -> availability at the addressed agent, board vs our own pre-board |
| 4 | # flow (agent-mail + crumb whisper, busy-poll peek + sniff + read), at 1/4/8/32 writers. |
| 5 | # |
| 6 | # usage: scripts/board-bench.sh [out-dir] |
| 7 | # SYNAPSE_COORD_BIN release synapse-coord binary (default: $CARGO_TARGET_DIR or ./target, release) |
| 8 | # CRUMB_BIN pinned crumb.c binary (required) |
| 9 | # AGENT_MAIL agent-mail script (default ~/.local/bin/agent-mail) |
| 10 | # BENCH_SCRATCH parent of the scratch directory (must be on the disk under test) |
| 11 | # |
| 12 | # Never touches the real ~/shared/agents: every pre-board command runs with HOME set to a |
| 13 | # scratch directory. No Python; bash and awk only. |
| 14 | set -euo pipefail |
| 15 | HERE=$(cd "$(dirname "$0")/.." && pwd) |
| 16 | OUT=${1:-$HERE/evidence/swarm-board} |
| 17 | BIN=${SYNAPSE_COORD_BIN:-${CARGO_TARGET_DIR:-$HERE/target}/release/synapse-coord} |
| 18 | CRUMB=${CRUMB_BIN:?set CRUMB_BIN to the pinned crumb binary} |
| 19 | MAIL=${AGENT_MAIL:-$HOME/.local/bin/agent-mail} |
| 20 | TOTAL=${BENCH_TOTAL:-128} # the record uses 128 (RULE.md); smaller only for smoke runs |
| 21 | WRITERS=(1 4 8 32) |
| 22 | [ -x "$BIN" ] || { echo "no synapse-coord at $BIN"; exit 3; } |
| 23 | [ -x "$CRUMB" ] || { echo "no crumb at $CRUMB"; exit 3; } |
| 24 | [ -x "$MAIL" ] || { echo "no agent-mail at $MAIL"; exit 3; } |
| 25 | S=$(mktemp -d "${BENCH_SCRATCH:-$HERE}/.board-bench.XXXXXX") |
| 26 | trap 'rm -rf "$S"' EXIT |
| 27 | mkdir -p "$OUT/raw" "$S/bin" |
| 28 | ln -s "$CRUMB" "$S/bin/crumb" |
| 29 | |
| 30 | energy_file="" |
| 31 | for d in /sys/class/hwmon/hwmon*; do |
| 32 | if [ "$(cat "$d/name" 2>/dev/null)" = aien_spbm ] && [ "$(cat "$d/energy1_label" 2>/dev/null)" = pkg ]; then |
| 33 | energy_file=$d/energy1_input |
| 34 | fi |
| 35 | done |
| 36 | energy() { if [ -n "$energy_file" ]; then cat "$energy_file"; else echo NOT_METERED; fi; } |
| 37 | |
| 38 | # $EPOCHREALTIME "sec.usec" -> ns since BASE (string arithmetic: no float rounding) |
| 39 | BASE=$(date +%s) |
| 40 | to_ns='function ns(t, a){ split(t, a, "."); return (a[1]-BASE)*1000000000 + substr(a[2] "000000", 1, 6)*1000 } |
| 41 | function nsr(s, n){ n=length(s); return (substr(s,1,n-9)-BASE)*1000000000 + substr(s,n-8)+0 }' |
| 42 | |
| 43 | # stats <file of latencies in us> -> "n p50 p95 max" |
| 44 | stats() { |
| 45 | sort -n "$1" | awk '{v[NR]=$1} END { |
| 46 | if (NR==0) { print 0, "NA", "NA", "NA"; exit } |
| 47 | i50=int((NR*50+99)/100); i95=int((NR*95+99)/100); |
| 48 | printf "%d %.0f %.0f %.0f\n", NR, v[i50], v[i95], v[NR] }' |
| 49 | } |
| 50 | |
| 51 | # join <send files glob dir> <recv file> <out latencies> ; prints "delivered dups" |
| 52 | join_lat() { |
| 53 | local sends=$1 recv=$2 lat=$3 |
| 54 | cat "$sends"/send.* > "$S/send.all" |
| 55 | awk -v BASE="$BASE" "$to_ns"' |
| 56 | FNR==NR { t0[$1]=ns($2); next } |
| 57 | { k=$1; if (k in seen) { dup++; next } seen[k]=1 |
| 58 | if (!(k in t0)) next |
| 59 | t1 = ($3=="ns") ? nsr($2) : ns($2) |
| 60 | printf "%.0f\n", (t1 - t0[k]) / 1000; got++ } |
| 61 | END { print got+0, dup+0 > "/dev/stderr" }' "$S/send.all" "$recv" > "$lat" 2> "$S/joinres" |
| 62 | cat "$S/joinres" |
| 63 | } |
| 64 | |
| 65 | { |
| 66 | echo "host: $(uname -srm)" |
| 67 | echo "fs: $(df -T "$S" | tail -1 | awk '{print $1, $2}')" |
| 68 | echo "binary: $BIN ($(sha256sum "$BIN" | cut -c1-16))" |
| 69 | echo "crumb: $CRUMB ($(sha256sum "$CRUMB" | cut -c1-16))" |
| 70 | echo "agent-mail: $MAIL ($(sha256sum "$MAIL" | cut -c1-16))" |
| 71 | echo "commit: $(git -C "$HERE" rev-parse HEAD)" |
| 72 | } > "$OUT/raw/env.txt" |
| 73 | |
| 74 | # fsync floor on the same filesystem |
| 75 | dd_line=$(dd if=/dev/zero of="$S/dd.bin" bs=512 count=500 oflag=dsync 2>&1 | tail -1) |
| 76 | dd_s=$(echo "$dd_line" | awk '{for (i=1;i<=NF;i++) if ($i=="s," || $i=="s") print $(i-1)}' | head -1) |
| 77 | rm -f "$S/dd.bin" |
| 78 | echo "dd 500 x 512 B oflag=dsync: $dd_line" > "$OUT/raw/fsync.txt" |
| 79 | |
| 80 | e_idle0=$(energy); sleep 5; e_idle1=$(energy) |
| 81 | echo "idle_5s_uJ $e_idle0 $e_idle1" > "$OUT/raw/energy-idle.txt" |
| 82 | |
| 83 | wait_for() { local i=0; while ! eval "$1"; do i=$((i+1)); [ $i -gt 500 ] && return 1; sleep 0.01; done; } |
| 84 | |
| 85 | board_run() { |
| 86 | local W=$1 per=$((TOTAL / $1)) root=$S/board-$1 d=$S/b$1 |
| 87 | mkdir -p "$d" |
| 88 | local B=("$BIN" board --board-root "$root") |
| 89 | "${B[@]}" init > /dev/null |
| 90 | "${B[@]}" --session sub-s register sub > /dev/null |
| 91 | for ((w = 0; w < W; w++)); do "${B[@]}" --session "ws$w" register "w$w" > /dev/null; done |
| 92 | "${B[@]}" --session sub-s watch --follow "$TOTAL" --stamp --timeout-ms 60000 > "$d/recv.json" & |
| 93 | local wpid=$! |
| 94 | wait_for "ls '$root'/wake/*.sock > /dev/null 2>&1" |
| 95 | local e0 e1 t_start t_end |
| 96 | e0=$(energy); t_start=$EPOCHREALTIME |
| 97 | local pids=() |
| 98 | for ((w = 0; w < W; w++)); do |
| 99 | ( |
| 100 | for ((n = 0; n < per; n++)); do |
| 101 | t0=$EPOCHREALTIME |
| 102 | "${B[@]}" --session "ws$w" post --thread bench --topic "bench/w$W" --to sub --text "w$w-$n" > /dev/null |
| 103 | echo "w$w-$n $t0" >> "$d/send.$w" |
| 104 | done |
| 105 | ) & |
| 106 | pids+=($!) |
| 107 | done |
| 108 | wait "${pids[@]}" |
| 109 | wait "$wpid" || true |
| 110 | t_end=$EPOCHREALTIME; e1=$(energy) |
| 111 | sed -n 's/.*"text":"\([^"]*\)".*"recv_unix_ns":\([0-9]*\).*/\1 \2 ns/p' "$d/recv.json" > "$d/recv" |
| 112 | read -r got dup < <(join_lat "$d" "$d/recv" "$d/lat") |
| 113 | cp "$d/lat" "$OUT/raw/board-w$W.lat_us" |
| 114 | "${B[@]}" check > /dev/null && chk=clean || chk=PROBLEMS |
| 115 | echo "board $W $got $dup $(stats "$d/lat") $t_start $t_end $e0 $e1 $chk" |
| 116 | } |
| 117 | |
| 118 | old_run() { |
| 119 | local W=$1 per=$((TOTAL / $1)) d=$S/o$1 H=$S/home-$1 |
| 120 | mkdir -p "$d" "$H/shared/agents" |
| 121 | local ENVV=(env HOME="$H" PATH="$S/bin:$PATH" CRUMB_COORD_ROOT="$S/coord-$1") |
| 122 | : > "$d/recv" |
| 123 | ( |
| 124 | # agent-mail read exits 1 when it found mail (its last test is `[ $found = 0 ]`), so |
| 125 | # the receiver runs without errexit and takes every exit status as-is. |
| 126 | set +e |
| 127 | got=0 |
| 128 | deadline=$(( $(date +%s) + 60 )) |
| 129 | while [ "$got" -lt "$TOTAL" ] && [ "$(date +%s)" -lt "$deadline" ]; do |
| 130 | n=$("${ENVV[@]}" "$MAIL" peek sub) |
| 131 | "${ENVV[@]}" crumb sniff sub "$H/shared/agents" > /dev/null 2>&1 |
| 132 | if [ "${n:-0}" -gt 0 ]; then |
| 133 | out=$("${ENVV[@]}" "$MAIL" read sub) |
| 134 | t1=$EPOCHREALTIME |
| 135 | while read -r k; do |
| 136 | echo "$k $t1 us" >> "$d/recv" |
| 137 | got=$((got + 1)) |
| 138 | done < <(printf '%s\n' "$out" | grep -E '^w[0-9]+-[0-9]+$') |
| 139 | fi |
| 140 | done |
| 141 | ) & |
| 142 | local rpid=$! |
| 143 | local e0 e1 t_start t_end |
| 144 | e0=$(energy); t_start=$EPOCHREALTIME |
| 145 | local pids=() |
| 146 | for ((w = 0; w < W; w++)); do |
| 147 | ( |
| 148 | for ((n = 0; n < per; n++)); do |
| 149 | t0=$EPOCHREALTIME |
| 150 | "${ENVV[@]}" "$MAIL" send "w$w" sub "w$w-$n" > /dev/null |
| 151 | echo "w$w-$n $t0" >> "$d/send.$w" |
| 152 | done |
| 153 | ) & |
| 154 | pids+=($!) |
| 155 | done |
| 156 | wait "${pids[@]}" |
| 157 | wait "$rpid" || true |
| 158 | t_end=$EPOCHREALTIME; e1=$(energy) |
| 159 | read -r got dup < <(join_lat "$d" "$d/recv" "$d/lat") |
| 160 | cp "$d/lat" "$OUT/raw/preboard-w$W.lat_us" |
| 161 | echo "preboard $W $got $dup $(stats "$d/lat") $t_start $t_end $e0 $e1 -" |
| 162 | } |
| 163 | |
| 164 | { |
| 165 | echo "# flow writers delivered dups n p50_us p95_us max_us t_start t_end energy0_uJ energy1_uJ check" |
| 166 | for W in "${WRITERS[@]}"; do |
| 167 | board_run "$W" |
| 168 | old_run "$W" |
| 169 | done |
| 170 | } | tee "$OUT/raw/runs.txt" |
| 171 | echo "fsync: $dd_line" |
| 172 | echo "raw results in $OUT/raw" |