Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/cortex/blob/main/tests/brain/load.sh # cortex aien-dev/cortex Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `cortex / tests / brain / load.sh` 164 lines ยท 9799 bytes Raw Blame History 1 #!/bin/sh 2 # tests/brain/load.sh : Company Brain latency, capacity alarm and kill -9 durability, on the load 3 # fixture of tests/load/run.sh (a cortex-rs snapshot imported into a fresh log). 4 # sh tests/brain/load.sh SRC.db 5 # 1. write-after latency: 2000 sequential POST /api/cortex/brain/write (one HTTP/1.1 connection each, 6 # tests/parity/pdrive.om); every 10th entry supersedes the one before it; 7 # 2. read-before latency: 500 sequential POST /api/cortex/brain/read over those entries; 8 # 3. capacity alarm: POST /api/cortex/brain/usage with alarmPercent $BRAIN_ALARM (default 50) must 9 # answer an empty "alarms" list; otherwise the gate fails and names the store capacity that passed 10 # the threshold (nothing is refused by the server; this is the alarm, not a cap); 11 # 4. crash: 4 writer clients x 1000 Brain writes and 4 read-before clients run together, the server is 12 # killed with kill -9 after 2 s and restarted on the same log; every write answered 201 must then be 13 # returned by /api/cortex/brain/get with its body (the tests/load/run.sh crash pattern). 14 # Writes $BRAIN_EVIDENCE (default build/brain): lat-write.ns, lat-read.ns (ns per request, one per 15 # line), usage.json, crash.txt, wall.txt. Statistics (p50/p99/mean) are computed afterwards through 16 # Wolfram and recorded in evidence/brain/stats.txt, as for evidence/load. Port 18234. 17 set -u 18 SRC=${1:?usage: load.sh SRC.db} 19 R=$(cd "$(dirname "$0")/../.." && pwd) 20 W=${BRAIN_LOAD_WORK:-$R/build/brain-load} 21 EV=${BRAIN_EVIDENCE:-$R/build/brain} 22 BIN=${CORTEX_BIN:-$R/build/cortex} 23 IMP=${CORTEX_IMPORT:-$R/build/cortex-import} 24 PORT=${BRAIN_LOAD_PORT:-18234} 25 ALARM=${BRAIN_ALARM:-50} 26 TOK=paritytok 27 SPID= 28 fail() { echo "brain load: $*" >&2; stop; exit 2; } 29 stop() { 30 [ -n "$SPID" ] || return 0 31 kill -- "-$SPID" 2>/dev/null || kill "$SPID" 2>/dev/null 32 SPID= 33 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do 34 curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health" || return 0 35 sleep 0.5 36 done 37 } 38 start() { 39 CORTEX_TOKEN=$TOK setsid nohup "$BIN" --port "$PORT" --log "$W/cx.log" --sock "$SOCKD/s" --encoder-url http://127.0.0.1:9 \ 40 >> "$W/server.out" 2>&1 < /dev/null & 41 SPID=$! 42 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30; do 43 curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health" && return 0 44 sleep 1 45 done 46 fail "server did not come up" 47 } 48 ms() { date +%s%N; } 49 [ -f "$SRC" ] || fail "no source database $SRC" 50 [ -x "$BIN" ] && [ -x "$IMP" ] || fail "build first (sh build.sh)" 51 if curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health"; then fail "port $PORT is in use"; fi 52 OMC=$(sh "$R/tools/omc.sh") || fail "pinned omc unavailable" 53 rm -rf "$W" 54 mkdir -p "$W" "$EV" || fail "cannot make $W" 55 # short directory for the private socket (sockaddr_un holds at most 108 bytes, a kernel boundary) 56 SOCKD=$(mktemp -d /tmp/cxbrain.XXXXXX) || fail "cannot make a socket directory" 57 trap 'rm -rf "$SOCKD"' EXIT 58 { echo om2; cat "$R/tests/parity/pdrive.om"; } > "$W/pdrive.om" 59 "$OMC" < "$W/pdrive.om" > "$W/pdrive" 2> "$W/pdrive.err" 60 if [ $? -ne 0 ] || [ -s "$W/pdrive.err" ]; then fail "pdrive.om did not compile"; fi 61 chmod +x "$W/pdrive" 62 D=$W/pdrive 63 sqlite3 "file:$SRC?mode=ro" ".backup '$W/i.db'" || fail "copy failed" 64 "$IMP" "$W/i.db" "$W/cx.log" > "$W/import.out" 2>&1 || fail "import failed: $(cat "$W/import.out")" 65 rm -f "$W/i.db" 66 start 67 B=/api/cortex/brain 68 awk -v b="$B" 'BEGIN { split("decision rule case change", K, " "); split("pricing staffing tender audit logistics", T, " ") 69 split("north south east west central", G, " ") 70 for (i = 0; i < 2000; i++) { s = ""; if (i % 10 == 9) s = sprintf(",\"supersedes\":\"bl-%05d\"", i - 1) 71 printf "brain.write\tPOST\t%s/write\taj\t{\"space\":\"brain-load\",\"id\":\"bl-%05d\",\"kind\":\"%s\",\"owner\":\"load\",\"date\":\"2026-%02d-%02d\",\"body\":\"%s note %d for the %s region, client %d, rate %d percent\"%s}\n", b, 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/writes.tsv" 72 awk -v b="$B" 'BEGIN { split("pricing staffing tender audit logistics", T, " "); split("north south east west central", G, " ") 73 for (i = 0; i < 500; i++) 74 printf "brain.read\tPOST\t%s/read\taj\t{\"space\":\"brain-load\",\"task\":\"how do we handle %s for a client in the %s region\",\"limit\":5}\n", b, T[i % 5 + 1], G[(i * 3) % 5 + 1] }' > "$W/reads.tsv" 75 t0=$(ms) 76 "$D" "$W/writes.tsv" "$PORT" "$W/writes.out" "$EV/lat-write.ns" || fail "write phase failed" 77 t1=$(ms) 78 "$D" "$W/reads.tsv" "$PORT" "$W/reads.out" "$EV/lat-read.ns" || fail "read phase failed" 79 t2=$(ms) 80 nw=$(grep -a -c '^HTTP/1.1 201' "$W/writes.out") 81 nr=$(grep -a -c '^HTTP/1.1 200' "$W/reads.out") 82 ns=$(grep -a -c '"supported":true' "$W/reads.out") 83 nsup=$(grep -a -o '"id":"bl-[0-9]*8","space"' "$W/reads.out" | wc -l) 84 printf "brain.usage\tPOST\t%s/usage\taj\t{\"alarmPercent\":%s}\n" "$B" "$ALARM" > "$W/usage.tsv" 85 "$D" "$W/usage.tsv" "$PORT" "$W/usage.out" || fail "usage request failed" 86 grep -a '^{' "$W/usage.out" > "$EV/usage.json" 87 # negative control: at 1 percent the content pool of this fixture (about 20 percent full) must alarm 88 printf "brain.usage\tPOST\t%s/usage\taj\t{\"alarmPercent\":1}\n" "$B" > "$W/usage1.tsv" 89 "$D" "$W/usage1.tsv" "$PORT" "$W/usage1.out" || fail "usage request failed" 90 alarm1=$(grep -a -o '"alarms":\[[^]]*\]' "$W/usage1.out") 91 { 92 echo "brain load $(date -u +%Y-%m-%dT%H:%M:%SZ) binary sha256 $(sha256sum "$BIN" | cut -c1-64)" 93 echo "fixture: $(sqlite3 "file:$SRC?mode=ro" 'select count(*) from entities') entities in the source snapshot, imported with cortex-import" 94 echo "write-after: 2000 sequential requests, $nw answered 201, wall_ns=$((t1 - t0))" 95 echo "read-before: 500 sequential requests (limit 5), $nr answered 200, $ns supported, wall_ns=$((t2 - t1))" 96 echo "superseded entries (ids ending in 8 that a later entry supersedes) seen in read-before entries: $nsup" 97 echo "usage (alarmPercent $ALARM): $(cat "$EV/usage.json")" 98 } > "$EV/wall.txt" 99 fails=0 100 [ "$nw" = 2000 ] || { echo "FAIL write-after: $nw of 2000 answered 201"; fails=$((fails + 1)); } 101 [ "$nr" = 500 ] || { echo "FAIL read-before: $nr of 500 answered 200"; fails=$((fails + 1)); } 102 [ "$nsup" = 0 ] || { echo "FAIL read-before returned $nsup superseded entries"; fails=$((fails + 1)); } 103 fm=$(grep -a -o '"ftsIndexMismatches":[a-z0-9]*' "$EV/usage.json") 104 [ "$fm" = '"ftsIndexMismatches":0' ] || { echo "FAIL incremental FTS index differs from a rebuild ($fm)"; fails=$((fails + 1)); } 105 [ "$fm" = '"ftsIndexMismatches":0' ] && echo "PASS incremental FTS index equals a rebuild after the snapshot and 2000 writes ($fm)" 106 if ! grep -q '"alarms":\[\]' "$EV/usage.json"; then 107 echo "FAIL capacity alarm at $ALARM percent: $(grep -o '"alarms":\[[^]]*\]' "$EV/usage.json") (see $EV/usage.json)"; fails=$((fails + 1)) 108 fi 109 case "$alarm1" in 110 *contentBytes*) echo "PASS alarm control: at 1 percent the server raises $alarm1" ;; 111 *) echo "FAIL alarm control: at 1 percent no contentBytes alarm ($alarm1)"; fails=$((fails + 1)) ;; 112 esac 113 # crash: writers and readers together, kill -9 mid-stream, restart, read back what was acknowledged 114 for k in 0 1 2 3; do 115 awk -v k=$k -v b="$B" 'BEGIN { for (j = 0; j < 1000; j++) { i = k + 4 * j 116 printf "brain.crash\tPOST\t%s/write\taj\t{\"space\":\"brain-crash\",\"id\":\"ck-%05d\",\"kind\":\"case\",\"owner\":\"crash\",\"date\":\"2026-11-11\",\"body\":\"durable payload %05d\"}\n", b, i, i } }' > "$W/cw$k.tsv" 117 head -250 "$W/reads.tsv" | sed 's/"brain-load"/"brain-crash"/' > "$W/cr$k.tsv" 118 done 119 CP= 120 for k in 0 1 2 3; do 121 "$D" "$W/cw$k.tsv" "$PORT" "$W/cw$k.out" & 122 CP="$CP $!" 123 "$D" "$W/cr$k.tsv" "$PORT" "$W/cr$k.out" & 124 CP="$CP $!" 125 done 126 sleep 2 127 kill -9 "$SPID" 128 SPID= 129 # shellcheck disable=SC2086 130 wait $CP 131 for k in 0 1 2 3; do 132 awk -v k=$k '/^#[0-9]+ ERR$/ { print k + 4 * substr($1, 2), "E"; next } 133 /^#[0-9]+ 0$/ { print k + 4 * substr($1, 2), "Z"; next } 134 /^#[0-9]+ [0-9]+$/ { ix = substr($1, 2); getline; print k + 4 * ix, ($2 == "201" ? "A" : "O") }' "$W/cw$k.out" 135 done > "$W/classes.txt" 136 na=$(grep -c ' A$' "$W/classes.txt") 137 nerr=$(grep -c ' E$' "$W/classes.txt") 138 nz=$(grep -c ' Z$' "$W/classes.txt") 139 no=$(grep -c ' O$' "$W/classes.txt") 140 nall=$(wc -l < "$W/classes.txt") 141 [ $((nerr + nz + no)) -gt 0 ] || fail "kill -9 landed after the streams ended (every write acknowledged); nothing was tested" 142 for i in 1 2 3 4 5 6 7 8 9 10; do 143 curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health" || break 144 sleep 0.5 145 done 146 start 147 awk -v b="$B" '$2 == "A" { printf "brain.get\tPOST\t%s/get\taj\t{\"space\":\"brain-crash\",\"id\":\"ck-%05d\"}\n", b, $1 }' "$W/classes.txt" > "$W/gets.tsv" 148 "$D" "$W/gets.tsv" "$PORT" "$W/gets.out" || fail "read-back failed" 149 got=$(grep -a -c '^HTTP/1.1 200' "$W/gets.out") 150 body=$(grep -a -c '"body":"durable payload [0-9]*"' "$W/gets.out") 151 stop 152 rm -rf "$SOCKD" 153 { 154 echo "brain crash test $(date -u +%Y-%m-%dT%H:%M:%SZ): 4 writer clients x 1000 Brain writes + 4 read-before clients, kill -9 after 2 s" 155 echo "binary sha256 $(sha256sum "$BIN" | cut -c1-64)" 156 echo "acknowledged (201) before kill: $na; transport error: $nerr; empty answer: $nz; other answer: $no; total $nall" 157 echo "read back after restart: $got answered 200, $body carry their body" 158 } > "$EV/crash.txt" 159 cat "$EV/wall.txt" "$EV/crash.txt" 160 [ "$nall" -eq 4000 ] || { echo "FAIL crash: $nall answers do not account for 4000 writes"; fails=$((fails + 1)); } 161 if [ "$na" -gt 0 ] && [ "$got" = "$na" ] && [ "$body" = "$na" ]; then echo "PASS crash: $na acknowledged writes all read back" 162 else echo "FAIL crash: acknowledged writes lost ($got of $na read back, $body with body)"; fails=$((fails + 1)); fi 163 if [ $fails -eq 0 ]; then echo "brain load: ALL PASS"; exit 0; fi 164 echo "brain load: $fails failure(s)"; exit 1