interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

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.
17set -u
18SRC=${1:?usage: load.sh SRC.db}
19R=$(cd "$(dirname "$0")/../.." && pwd)
20W=${BRAIN_LOAD_WORK:-$R/build/brain-load}
21EV=${BRAIN_EVIDENCE:-$R/build/brain}
22BIN=${CORTEX_BIN:-$R/build/cortex}
23IMP=${CORTEX_IMPORT:-$R/build/cortex-import}
24PORT=${BRAIN_LOAD_PORT:-18234}
25ALARM=${BRAIN_ALARM:-50}
26TOK=paritytok
27SPID=
28fail() { echo "brain load: $*" >&2; stop; exit 2; }
29stop() {
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}
38start() {
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}
48ms() { date +%s%N; }
49[ -f "$SRC" ] || fail "no source database $SRC"
50[ -x "$BIN" ] && [ -x "$IMP" ] || fail "build first (sh build.sh)"
51if curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health"; then fail "port $PORT is in use"; fi
52OMC=$(sh "$R/tools/omc.sh") || fail "pinned omc unavailable"
53rm -rf "$W"
54mkdir -p "$W" "$EV" || fail "cannot make $W"
55# short directory for the private socket (sockaddr_un holds at most 108 bytes, a kernel boundary)
56SOCKD=$(mktemp -d /tmp/cxbrain.XXXXXX) || fail "cannot make a socket directory"
57trap '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"
60if [ $? -ne 0 ] || [ -s "$W/pdrive.err" ]; then fail "pdrive.om did not compile"; fi
61chmod +x "$W/pdrive"
62D=$W/pdrive
63sqlite3 "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")"
65rm -f "$W/i.db"
66start
67B=/api/cortex/brain
68awk -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"
72awk -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"
75t0=$(ms)
76"$D" "$W/writes.tsv" "$PORT" "$W/writes.out" "$EV/lat-write.ns" || fail "write phase failed"
77t1=$(ms)
78"$D" "$W/reads.tsv" "$PORT" "$W/reads.out" "$EV/lat-read.ns" || fail "read phase failed"
79t2=$(ms)
80nw=$(grep -a -c '^HTTP/1.1 201' "$W/writes.out")
81nr=$(grep -a -c '^HTTP/1.1 200' "$W/reads.out")
82ns=$(grep -a -c '"supported":true' "$W/reads.out")
83nsup=$(grep -a -o '"id":"bl-[0-9]*8","space"' "$W/reads.out" | wc -l)
84printf "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"
86grep -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
88printf "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"
90alarm1=$(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"
99fails=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)); }
103fm=$(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)"
106if ! 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))
108fi
109case "$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)) ;;
112esac
113# crash: writers and readers together, kill -9 mid-stream, restart, read back what was acknowledged
114for 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"
118done
119CP=
120for 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 $!"
125done
126sleep 2
127kill -9 "$SPID"
128SPID=
129# shellcheck disable=SC2086
130wait $CP
131for 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"
135done > "$W/classes.txt"
136na=$(grep -c ' A$' "$W/classes.txt")
137nerr=$(grep -c ' E$' "$W/classes.txt")
138nz=$(grep -c ' Z$' "$W/classes.txt")
139no=$(grep -c ' O$' "$W/classes.txt")
140nall=$(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"
142for 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
145done
146start
147awk -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"
149got=$(grep -a -c '^HTTP/1.1 200' "$W/gets.out")
150body=$(grep -a -c '"body":"durable payload [0-9]*"' "$W/gets.out")
151stop
152rm -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"
159cat "$EV/wall.txt" "$EV/crash.txt"
160[ "$nall" -eq 4000 ] || { echo "FAIL crash: $nall answers do not account for 4000 writes"; fails=$((fails + 1)); }
161if [ "$na" -gt 0 ] && [ "$got" = "$na" ] && [ "$body" = "$na" ]; then echo "PASS crash: $na acknowledged writes all read back"
162else echo "FAIL crash: acknowledged writes lost ($got of $na read back, $body with body)"; fails=$((fails + 1)); fi
163if [ $fails -eq 0 ]; then echo "brain load: ALL PASS"; exit 0; fi
164echo "brain load: $fails failure(s)"; exit 1