interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / brain / bench.sh

185 lines · 10481 bytes Raw Blame History
1#!/bin/sh
2# tests/brain/bench.sh : read-before and ranked-search latency of two Omega server binaries and the
3# cortex-rs oracle on one store (a measurement, not a check.sh step: it writes 111,000 entities).
4# BEFORE=path/to/cortex [AFTER=build/cortex] [BENCH_RUNS=3] sh tests/brain/bench.sh
5# 1. The store is written once through the oracle (CORTEX_ORACLE, default cortex-rs on PATH, on a
6# fresh database in the work directory, never the live one): 1,000 / 10,000 / 100,000 entities in
7# the spaces s1k / s10k / s100k, each a Company Brain entry (entityType brain.<kind>, validFrom,
8# metadata owner and, for every 10th, supersedes the one before), the texts of tests/brain/scale.sh.
9# It is kept in $BENCH_WORK/store.db and reused by later runs (delete it to rebuild).
10# 2. A sqlite3 .backup copy is imported with build/cortex-import into an Omega log; each Omega arm
11# runs on its own copy of that log, the oracle on its own copy of the database.
12# 3. BENCH_RUNS rounds; in each, the arms run one after the other (oracle, before, after), each
13# started fresh: one warm-up of each request kind per space (not recorded), then per space 200
14# read-before requests (Omega arms; the oracle has no Brain routes) and 200 GET searches with the
15# same task text (no phrase match: FTS, then the substring fallback over the space), then 200 GET
16# searches for a two-word phrase that matches a fifth of the space ("pricing note", ranked by
17# bm25), limit 5, one HTTP/1.1 connection per request (tests/parity/pdrive.om).
18# Latencies in ns go to $BENCH_EVIDENCE/r<round>/lat-<arm>-<read|search|rank>-<space>.ns.
19# Checks (exit 1 on any failure): every read answers 200 with "supported":true; the before and after
20# answers are byte-identical apart from elapsedMs and the date header; the oracle's and Omega's
21# searches return the same entity names in the same order. Ports 18241 (oracle) and 18242 (Omega).
22set -u
23R=$(cd "$(dirname "$0")/../.." && pwd)
24W=${BENCH_WORK:-$R/build/brain-bench}
25EV=${BENCH_EVIDENCE:-$R/build/brain-bench-ev}
26OP=${BENCH_ORACLE_PORT:-18241}
27SP=${BENCH_OMEGA_PORT:-18242}
28RUNS=${BENCH_RUNS:-3}
29: "${BEFORE:?set BEFORE to the server binary to compare against}"
30AFTER=${AFTER:-$R/build/cortex}
31IMP=${CORTEX_IMPORT:-$R/build/cortex-import}
32ORA=${CORTEX_ORACLE:-$(command -v cortex-rs)}
33TOK=paritytok
34ENC=http://127.0.0.1:9
35PID=
36fail() { echo "brain bench: $*" >&2; stop; exit 2; }
37up() {
38 for i in $(seq 1 240); do
39 curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$1/health" && return 0
40 sleep 0.5
41 done
42 return 1
43}
44stop() {
45 [ -n "$PID" ] || return 0
46 kill -- "-$PID" 2>/dev/null || kill "$PID" 2>/dev/null
47 PID=
48 for i in $(seq 1 20); do
49 curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$OP/health" ||
50 curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$SP/health" || return 0
51 sleep 0.5
52 done
53}
54startora() { # DB
55 CORTEX_TOKEN=$TOK setsid nohup "$ORA" --port "$OP" --db-path "$1" --encoder-url $ENC \
56 >> "$W/oracle.out" 2>&1 < /dev/null &
57 PID=$!
58 up "$OP" || fail "the oracle did not come up"
59}
60startomega() { # BIN LOG
61 CORTEX_TOKEN=$TOK setsid nohup "$1" --port "$SP" --log "$2" --sock "$SOCKD/s" --encoder-url $ENC \
62 >> "$W/omega.out" 2>&1 < /dev/null &
63 PID=$!
64 up "$SP" || fail "$1 did not come up"
65}
66[ -x "$BEFORE" ] && [ -x "$AFTER" ] && [ -x "$IMP" ] || fail "BEFORE, AFTER and build/cortex-import must be executables"
67[ -n "$ORA" ] && [ -x "$ORA" ] || fail "oracle cortex-rs not found (set CORTEX_ORACLE)"
68for port in $OP $SP; do
69 if curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$port/health"; then fail "port $port is in use"; fi
70done
71OMC=$(sh "$R/tools/omc.sh") || fail "pinned omc unavailable"
72mkdir -p "$W" || fail "cannot make $W"
73rm -rf "$EV" && mkdir -p "$EV" || fail "cannot make $EV"
74SOCKD=$(mktemp -d /tmp/cxbench.XXXXXX) || fail "cannot make a socket directory"
75trap 'rm -rf "$SOCKD"' EXIT
76{ echo om2; cat "$R/tests/parity/pdrive.om"; } > "$W/pdrive.om"
77"$OMC" < "$W/pdrive.om" > "$W/pdrive" 2> "$W/pdrive.err"
78if [ $? -ne 0 ] || [ -s "$W/pdrive.err" ]; then fail "pdrive.om did not compile"; fi
79chmod +x "$W/pdrive"
80D=$W/pdrive
81SPACES="s1k s10k s100k"
82for sz in 1000:s1k 10000:s10k 100000:s100k; do
83 n=${sz%%:*} sp=${sz#*:}
84 awk -v n="$n" -v sp="$sp" 'BEGIN { split("decision rule case change", K, " ")
85 split("pricing staffing tender audit logistics", T, " "); split("north south east west central", G, " ")
86 for (i = 0; i < n; i++) { s = ""; if (i % 10 == 9) s = sprintf(",\"supersedes\":\"e%06d\"", i - 1)
87 printf "write\tPOST\t/api/cortex/write\taj\t{\"kind\":\"entity\",\"value\":{\"space\":\"%s\",\"entityType\":\"brain.%s\",\"canonicalName\":\"e%06d\",\"content\":\"%s note %d for the %s region, client %d, rate %d percent\",\"metadata\":{\"owner\":\"scale\"%s},\"validFrom\":\"2026-%02d-%02d\",\"confidence\":1.0}}\n", sp, K[i % 4 + 1], i, T[i % 5 + 1], i, G[i % 5 + 1], i % 97, i % 31, s, i % 12 + 1, i % 28 + 1 } }' > "$W/w-$sp.tsv"
88 awk -v sp="$sp" 'BEGIN { split("pricing staffing tender audit logistics", T, " "); split("north south east west central", G, " ")
89 for (i = 0; i < 200; i++) {
90 t = sprintf("how do we handle %s for a client in the %s region", T[i % 5 + 1], G[(i * 3) % 5 + 1])
91 printf "brain.read\tPOST\t/api/cortex/brain/read\taj\t{\"space\":\"%s\",\"task\":\"%s\",\"limit\":5}\n", sp, t > "/dev/stderr"
92 gsub(/ /, "%20", t)
93 printf "search.get\tGET\t/api/cortex/search?q=%s&space=%s&limit=5\ta\t\n", t, sp } }' > "$W/s-$sp.tsv" 2> "$W/r-$sp.tsv"
94 awk -v sp="$sp" 'BEGIN { split("pricing staffing tender audit logistics", T, " ")
95 for (i = 0; i < 200; i++)
96 printf "rank.get\tGET\t/api/cortex/search?q=%s%%20note&space=%s&limit=5\ta\t\n", T[i % 5 + 1], sp }' > "$W/q-$sp.tsv"
97 head -n 1 "$W/r-$sp.tsv" > "$W/warm-$sp.tsv"
98 head -n 1 "$W/s-$sp.tsv" >> "$W/warm-$sp.tsv"
99 head -n 1 "$W/q-$sp.tsv" >> "$W/warm-$sp.tsv"
100 head -n 1 "$W/s-$sp.tsv" > "$W/warmo-$sp.tsv"
101 head -n 1 "$W/q-$sp.tsv" >> "$W/warmo-$sp.tsv"
102done
103# 1. the store, written through the oracle (pdrive holds a corpus in one 16 MiB global: pieces)
104if [ ! -s "$W/store.db" ]; then
105 rm -f "$W/new.db" "$W/new.db-wal" "$W/new.db-shm"
106 startora "$W/new.db"
107 for sp in $SPACES; do
108 rm -f "$W/w-$sp.part."*
109 split -l 20000 -d -a 3 "$W/w-$sp.tsv" "$W/w-$sp.part."
110 : > "$W/w-$sp.out"
111 for p in "$W/w-$sp.part."*; do
112 "$D" "$p" "$OP" "$p.out" || fail "writing $sp ($p) failed"
113 cat "$p.out" >> "$W/w-$sp.out"
114 done
115 nw=$(grep -a -c '^HTTP/1.1 201' "$W/w-$sp.out"); want=$(wc -l < "$W/w-$sp.tsv")
116 [ "$nw" = "$want" ] || fail "$sp: $nw of $want writes answered 201"
117 rm -f "$W/w-$sp.part."* "$W/w-$sp.out"
118 done
119 stop
120 sqlite3 "$W/new.db" ".backup '$W/store.db'" || fail "backup of the new store failed"
121 rm -f "$W/new.db" "$W/new.db-wal" "$W/new.db-shm"
122fi
123# 2. the Omega log
124rm -f "$W/i.db" "$W/cx.log"
125sqlite3 "file:$W/store.db?mode=ro" ".backup '$W/i.db'" || fail "copy failed"
126"$IMP" "$W/i.db" "$W/cx.log" > "$W/import.out" 2>&1 || fail "import failed: $(cat "$W/import.out")"
127rm -f "$W/i.db"
128bodies() { grep -a '^{' "$1" | sed 's/"elapsedMs":[0-9.e+-]*//'; }
129names() { grep -a '^{' "$1" | sed 's/"elapsedMs":[0-9.e+-]*//' | awk '{ s = ""; t = $0
130 while (match(t, /"canonicalName":"[^"]*"/)) { s = s " " substr(t, RSTART + 17, RLENGTH - 18); t = substr(t, RSTART + RLENGTH) }
131 print s }'; }
132fails=0
133# 3. rounds
134for r in $(seq 1 "$RUNS"); do
135 E=$EV/r$r
136 mkdir -p "$E"
137 for arm in oracle before after; do
138 case $arm in
139 oracle) rm -f "$W/o.db"; sqlite3 "file:$W/store.db?mode=ro" ".backup '$W/o.db'" || fail "copy failed"
140 startora "$W/o.db"; port=$OP ;;
141 before) cp "$W/cx.log" "$W/cx-$arm.log"; startomega "$BEFORE" "$W/cx-$arm.log"; port=$SP ;;
142 after) cp "$W/cx.log" "$W/cx-$arm.log"; startomega "$AFTER" "$W/cx-$arm.log"; port=$SP ;;
143 esac
144 for sp in $SPACES; do
145 if [ $arm = oracle ]; then
146 "$D" "$W/warmo-$sp.tsv" "$port" "$W/warm.out" || fail "warm-up $arm $sp failed"
147 else
148 "$D" "$W/warm-$sp.tsv" "$port" "$W/warm.out" || fail "warm-up $arm $sp failed"
149 "$D" "$W/r-$sp.tsv" "$port" "$W/r-$arm-$sp.out" "$E/lat-$arm-read-$sp.ns" || fail "reads $arm $sp failed"
150 ok=$(grep -a -c '"supported":true' "$W/r-$arm-$sp.out")
151 [ "$ok" = 200 ] || { echo "FAIL round $r $arm $sp: $ok of 200 reads supported"; fails=$((fails + 1)); }
152 fi
153 "$D" "$W/s-$sp.tsv" "$port" "$W/s-$arm-$sp.out" "$E/lat-$arm-search-$sp.ns" || fail "searches $arm $sp failed"
154 ok=$(grep -a -c '^HTTP/1.1 200' "$W/s-$arm-$sp.out")
155 [ "$ok" = 200 ] || { echo "FAIL round $r $arm $sp: $ok of 200 searches answered 200"; fails=$((fails + 1)); }
156 "$D" "$W/q-$sp.tsv" "$port" "$W/q-$arm-$sp.out" "$E/lat-$arm-rank-$sp.ns" || fail "ranked searches $arm $sp failed"
157 ok=$(grep -a -c '"canonicalName"' "$W/q-$arm-$sp.out")
158 [ "$ok" = 200 ] || { echo "FAIL round $r $arm $sp: $ok of 200 ranked searches returned entities"; fails=$((fails + 1)); }
159 done
160 stop
161 rm -f "$W/o.db" "$W/o.db-wal" "$W/o.db-shm" "$W/cx-$arm.log"
162 done
163 for sp in $SPACES; do
164 for k in r s q; do
165 bodies "$W/$k-before-$sp.out" > "$W/b.txt"; bodies "$W/$k-after-$sp.out" > "$W/a.txt"
166 if cmp -s "$W/b.txt" "$W/a.txt"; then echo "PASS round $r $sp $k: before and after answers identical"
167 else echo "FAIL round $r $sp $k: before and after answers differ"; fails=$((fails + 1)); fi
168 done
169 for k in s q; do
170 names "$W/$k-oracle-$sp.out" > "$W/no.txt"; names "$W/$k-after-$sp.out" > "$W/na.txt"
171 if cmp -s "$W/no.txt" "$W/na.txt"; then echo "PASS round $r $sp $k: oracle and Omega searches name the same entities in the same order"
172 else echo "FAIL round $r $sp $k: oracle and Omega searches differ"; fails=$((fails + 1)); fi
173 done
174 done
175done
176{
177 echo "brain bench $(date -u +%Y-%m-%dT%H:%M:%SZ) host $(uname -sm) load $(cut -d' ' -f1-3 /proc/loadavg) rounds $RUNS"
178 echo "before sha256 $(sha256sum "$BEFORE" | cut -c1-64)"
179 echo "after sha256 $(sha256sum "$AFTER" | cut -c1-64)"
180 echo "oracle sha256 $(sha256sum "$ORA" | cut -c1-64)"
181 echo "store $(sqlite3 "file:$W/store.db?mode=ro" 'select count(*) from entities') entities, sha256 $(sha256sum "$W/store.db" | cut -c1-64); log $(wc -c < "$W/cx.log") bytes"
182} > "$EV/run.txt"
183cat "$EV/run.txt"
184if [ $fails -eq 0 ]; then echo "brain bench: ALL PASS"; exit 0; fi
185echo "brain bench: $fails failure(s)"; exit 1