cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / brain / scale.sh
| 1 | #!/bin/sh |
| 2 | # tests/brain/scale.sh : read-before latency against the number of Brain entries in a space, for two |
| 3 | # server binaries on copies of one log (a measurement, not a check.sh step: it writes 111,000 entries). |
| 4 | # BEFORE=path/to/cortex AFTER=path/to/cortex sh tests/brain/scale.sh |
| 5 | # 1. AFTER is started on an empty log and receives 1,000 / 10,000 / 100,000 Brain entries in the |
| 6 | # spaces s1k / s10k / s100k (every 10th entry supersedes the one before it), one HTTP/1.1 |
| 7 | # connection per request (tests/parity/pdrive.om); the server is stopped and the log kept. |
| 8 | # 2. For each binary (BEFORE, then AFTER) on its own copy of that log: one warm-up read per space |
| 9 | # (not recorded; the first read after a start builds the supersede index), then 200 sequential |
| 10 | # read-before requests per space (limit 5), latencies in ns to $SCALE_EVIDENCE/lat-<arm>-<space>.ns |
| 11 | # (default build/brain-scale-ev). Every answer must be 200 with "supported":true. |
| 12 | # Statistics are computed afterwards (evidence/brain/stats.txt quotes the query). Port 18235. |
| 13 | set -u |
| 14 | R=$(cd "$(dirname "$0")/../.." && pwd) |
| 15 | W=${SCALE_WORK:-$R/build/brain-scale} |
| 16 | EV=${SCALE_EVIDENCE:-$R/build/brain-scale-ev} |
| 17 | PORT=${SCALE_PORT:-18235} |
| 18 | : "${BEFORE:?set BEFORE to the server binary to compare against}" |
| 19 | AFTER=${AFTER:-$R/build/cortex} |
| 20 | TOK=paritytok |
| 21 | SPID= |
| 22 | fail() { echo "brain scale: $*" >&2; stop; exit 2; } |
| 23 | stop() { |
| 24 | [ -n "$SPID" ] || return 0 |
| 25 | kill -- "-$SPID" 2>/dev/null || kill "$SPID" 2>/dev/null |
| 26 | SPID= |
| 27 | for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do |
| 28 | curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health" || return 0 |
| 29 | sleep 0.5 |
| 30 | done |
| 31 | } |
| 32 | start() { # start BINARY LOG |
| 33 | CORTEX_TOKEN=$TOK setsid nohup "$1" --port "$PORT" --log "$2" --sock "$SOCKD/s" --encoder-url http://127.0.0.1:9 \ |
| 34 | >> "$W/server.out" 2>&1 < /dev/null & |
| 35 | SPID=$! |
| 36 | for i in $(seq 1 120); do |
| 37 | curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health" && return 0 |
| 38 | sleep 1 |
| 39 | done |
| 40 | fail "server did not come up" |
| 41 | } |
| 42 | [ -x "$BEFORE" ] && [ -x "$AFTER" ] || fail "BEFORE and AFTER must be executables" |
| 43 | if curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health"; then fail "port $PORT is in use"; fi |
| 44 | OMC=$(sh "$R/tools/omc.sh") || fail "pinned omc unavailable" |
| 45 | rm -rf "$W" "$EV" |
| 46 | mkdir -p "$W" "$EV" || fail "cannot make $W" |
| 47 | SOCKD=$(mktemp -d /tmp/cxscale.XXXXXX) || fail "cannot make a socket directory" |
| 48 | trap 'rm -rf "$SOCKD"' EXIT |
| 49 | { echo om2; cat "$R/tests/parity/pdrive.om"; } > "$W/pdrive.om" |
| 50 | "$OMC" < "$W/pdrive.om" > "$W/pdrive" 2> "$W/pdrive.err" |
| 51 | if [ $? -ne 0 ] || [ -s "$W/pdrive.err" ]; then fail "pdrive.om did not compile"; fi |
| 52 | chmod +x "$W/pdrive" |
| 53 | D=$W/pdrive |
| 54 | B=/api/cortex/brain |
| 55 | for sz in 1000:s1k 10000:s10k 100000:s100k; do |
| 56 | n=${sz%%:*} sp=${sz#*:} |
| 57 | awk -v b="$B" -v n="$n" -v sp="$sp" 'BEGIN { split("decision rule case change", K, " ") |
| 58 | split("pricing staffing tender audit logistics", T, " "); split("north south east west central", G, " ") |
| 59 | for (i = 0; i < n; i++) { s = ""; if (i % 10 == 9) s = sprintf(",\"supersedes\":\"e%06d\"", i - 1) |
| 60 | printf "brain.write\tPOST\t%s/write\taj\t{\"space\":\"%s\",\"id\":\"e%06d\",\"kind\":\"%s\",\"owner\":\"scale\",\"date\":\"2026-%02d-%02d\",\"body\":\"%s note %d for the %s region, client %d, rate %d percent\"%s}\n", b, sp, i, K[i % 4 + 1], i % 12 + 1, i % 28 + 1, T[i % 5 + 1], i, G[i % 5 + 1], i % 97, i % 31, s } }' > "$W/w-$sp.tsv" |
| 61 | awk -v b="$B" -v sp="$sp" 'BEGIN { split("pricing staffing tender audit logistics", T, " "); split("north south east west central", G, " ") |
| 62 | for (i = 0; i < 200; i++) |
| 63 | printf "brain.read\tPOST\t%s/read\taj\t{\"space\":\"%s\",\"task\":\"how do we handle %s for a client in the %s region\",\"limit\":5}\n", b, sp, T[i % 5 + 1], G[(i * 3) % 5 + 1] }' > "$W/r-$sp.tsv" |
| 64 | head -n 1 "$W/r-$sp.tsv" > "$W/warm-$sp.tsv" |
| 65 | done |
| 66 | start "$AFTER" "$W/cx.log" |
| 67 | # pdrive holds a corpus in one 16 MiB global (the largest pool the pinned omc compiles), so the |
| 68 | # 100,000 writes go in pieces of 20,000 lines |
| 69 | for sp in s1k s10k s100k; do |
| 70 | rm -f "$W/w-$sp.part."* |
| 71 | split -l 20000 -d -a 3 "$W/w-$sp.tsv" "$W/w-$sp.part." |
| 72 | : > "$W/w-$sp.out" |
| 73 | for p in "$W/w-$sp.part."*; do |
| 74 | "$D" "$p" "$PORT" "$p.out" || fail "writing $sp ($p) failed" |
| 75 | cat "$p.out" >> "$W/w-$sp.out" |
| 76 | done |
| 77 | nw=$(grep -a -c '^HTTP/1.1 201' "$W/w-$sp.out"); want=$(wc -l < "$W/w-$sp.tsv") |
| 78 | [ "$nw" = "$want" ] || fail "$sp: $nw of $want writes answered 201" |
| 79 | done |
| 80 | stop |
| 81 | fails=0 |
| 82 | for arm in before after; do |
| 83 | if [ $arm = before ]; then bin=$BEFORE; else bin=$AFTER; fi |
| 84 | cp "$W/cx.log" "$W/cx-$arm.log" |
| 85 | start "$bin" "$W/cx-$arm.log" |
| 86 | for sp in s1k s10k s100k; do |
| 87 | "$D" "$W/warm-$sp.tsv" "$PORT" "$W/warm-$arm-$sp.out" || fail "warm-up $arm $sp failed" |
| 88 | "$D" "$W/r-$sp.tsv" "$PORT" "$W/r-$arm-$sp.out" "$EV/lat-$arm-$sp.ns" || fail "reads $arm $sp failed" |
| 89 | ok=$(grep -a -c '"supported":true' "$W/r-$arm-$sp.out") |
| 90 | echo "$arm $sp: 200 reads, $ok supported" |
| 91 | [ "$ok" = 200 ] || fails=$((fails + 1)) |
| 92 | done |
| 93 | stop |
| 94 | done |
| 95 | { |
| 96 | echo "brain scale $(date -u +%Y-%m-%dT%H:%M:%SZ) host $(uname -sm) load $(cut -d' ' -f1-3 /proc/loadavg)" |
| 97 | echo "before sha256 $(sha256sum "$BEFORE" | cut -c1-64)" |
| 98 | echo "after sha256 $(sha256sum "$AFTER" | cut -c1-64)" |
| 99 | echo "log $(wc -c < "$W/cx.log") bytes, spaces s1k s10k s100k with 1000 / 10000 / 100000 Brain entries" |
| 100 | } > "$EV/run.txt" |
| 101 | cat "$EV/run.txt" |
| 102 | if [ $fails -eq 0 ]; then echo "brain scale: ALL PASS"; exit 0; fi |
| 103 | echo "brain scale: $fails failure(s)"; exit 1 |