interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / conc / run.sh

169 lines · 7547 bytes Raw Blame History
1#!/bin/sh
2# tests/conc/run.sh : concurrent clients and slow clients against the Omega server.
3# sh tests/conc/run.sh SRC.db
4# Needs the parity corpus, pdrive and pcmp (run tests/parity/run.sh first). Starts build/cortex on a
5# fresh import (port 18290, unreachable encoder). The read mix of the corpus is cut into 16 slices:
6# seq the 16 slices one after another, one client -> lat-seq.ns
7# c16 the 16 slices as 16 parallel clients -> lat-c16.ns
8# slow the 16 parallel clients while S slow clients (tests/conc/slow.om) hold connections open
9# by never finishing their request head -> lat-slow.ns, slow.txt
10# restart 4 slow clients hold handlers for 14 s, the state process is killed with kill -9, and a
11# new server on the same port must answer /health within 3 s (the handlers still finish
12# their clients, but the listener must be free; a build whose busy handlers keep it waits
13# for their 10 s header deadline) -> wall.txt
14# Pass: no transport error in any phase, every request answered in every phase, every slow
15# connection ended by the server (answered or closed) within SLOWMAX seconds, and every parallel
16# answer equal to the one-client answer to the same request under the parity masks (pcmp), and the
17# restart came up.
18# CONC_CONTROL_BIN=path runs the slow phase once more against that binary (negative control: a
19# one-connection server is expected to stall behind the slow clients); recorded, not gated.
20# Statistics are computed afterwards through Wolfram and recorded in evidence/conc/stats.txt.
21set -u
22SRC=${1:?usage: run.sh SRC.db}
23R=$(cd "$(dirname "$0")/../.." && pwd)
24PW=${PARITY_WORK:-$R/build/parity}
25W=${CONC_WORK:-$R/build/conc}
26EV=${CONC_EVIDENCE:-$R/evidence/conc}
27BIN=${CORTEX_BIN:-$R/build/cortex}
28IMP=${CORTEX_IMPORT:-$R/build/cortex-import}
29OMC=$(sh "$R/tools/omc.sh") || { echo "conc: pinned omc unavailable" >&2; exit 2; }
30S=${CONC_SLOW:-200}
31SLOWMAX=11
32SP=18290
33TOK=paritytok
34ENC=http://127.0.0.1:9
35SPID=
36stop() {
37 [ -n "$SPID" ] && kill "$SPID" 2>/dev/null
38 SPID=
39 sleep 1
40}
41fail() { echo "conc: $*" >&2; stop; exit 2; }
42waitup() {
43 for i in $(seq 1 30); do
44 curl -sf -o /dev/null --max-time 1 "http://127.0.0.1:$SP/health" && return 0
45 sleep 1
46 done
47 return 1
48}
49start() { # start BINARY
50 CORTEX_TOKEN=$TOK setsid nohup "$1" --port $SP --log "$W/cx.log" --sock "$SOCKD/s" --encoder-url $ENC \
51 >> "$W/server.out" 2>&1 < /dev/null &
52 SPID=$!
53 waitup || fail "server did not come up"
54}
55ms() { date +%s%N; }
56D=$PW/pdrive
57[ -x "$D" ] && [ -x "$PW/pcmp" ] && [ -f "$PW/corpus.tsv" ] || fail "run tests/parity/run.sh first"
58curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$SP/health" && fail "port $SP is in use"
59rm -rf "$W"
60mkdir -p "$W" "$EV" || exit 2
61# private socket in a short directory (sockaddr_un path: 108 bytes, a kernel boundary)
62SOCKD=$(mktemp -d /tmp/cxsock.XXXXXX) || exit 2
63trap 'rm -rf "$SOCKD"' EXIT
64{ echo om2; cat "$R/tests/conc/slow.om"; } > "$W/slow.om"
65"$OMC" < "$W/slow.om" > "$W/slow" 2> "$W/slow.err"
66if [ $? -ne 0 ] || [ -s "$W/slow.err" ]; then fail "slow.om did not compile"; fi
67chmod +x "$W/slow"
68awk -F'\t' '$1 ~ /^(search|recall|get|traverse)\./' "$PW/corpus.tsv" > "$W/mix.tsv"
69N=$(wc -l < "$W/mix.tsv")
70[ "$N" -ge 2000 ] || fail "read mix has only $N requests"
71for k in $(seq 0 15); do awk -v k=$k 'NR % 16 == k' "$W/mix.tsv" > "$W/mix$k.tsv"; done
72sqlite3 "file:$SRC?mode=ro" ".backup '$W/i.db'" || fail "copy failed"
73"$IMP" "$W/i.db" "$W/cx.log" > "$W/import.out" 2>&1 || fail "import failed"
74start "$BIN"
75: > "$EV/wall.txt"
76par() { # par PHASE: 16 parallel clients; concatenated latencies in EV/lat-PHASE.ns
77 CP=
78 for k in $(seq 0 15); do
79 "$D" "$W/mix$k.tsv" $SP "$W/$1-c$k.out" "$W/lat-$1-c$k.ns" &
80 CP="$CP $!"
81 done
82 # shellcheck disable=SC2086
83 wait $CP
84 for k in $(seq 0 15); do cat "$W/lat-$1-c$k.ns"; done > "$EV/lat-$1.ns"
85}
86errs() { cat "$@" | grep -c '^#[0-9]* ERR$'; }
87SINCE=$(date -u +%Y-%m-%dT%H:%M:%S)
88t0=$(ms)
89for k in $(seq 0 15); do
90 "$D" "$W/mix$k.tsv" $SP "$W/seq-c$k.out" "$W/lat-seq-c$k.ns" || fail "sequential run failed"
91done
92t1=$(ms)
93for k in $(seq 0 15); do cat "$W/lat-seq-c$k.ns"; done > "$EV/lat-seq.ns"
94par c16
95t2=$(ms)
96"$W/slow" $SP "$S" 14 "$W/slow.txt" &
97SL=$!
98sleep 1
99t3=$(ms)
100par slow
101t4=$(ms)
102wait $SL
103src=$?
104cp "$W/slow.txt" "$EV/slow.txt"
105e1=$(errs "$W"/seq-c*.out); e2=$(errs "$W"/c16-c*.out); e3=$(errs "$W"/slow-c*.out)
106{
107 echo "conc $(date -u +%Y-%m-%dT%H:%M:%SZ) binary sha256 $(sha256sum "$BIN" | cut -c1-64)"
108 echo "requests per phase: $N"
109 echo "seq 1 client wall_ns=$((t1 - t0)) transport_errors=$e1"
110 echo "c16 16 clients wall_ns=$((t2 - t1)) transport_errors=$e2"
111 echo "slow 16 clients wall_ns=$((t4 - t3)) transport_errors=$e3 with $S slow clients (slow rc=$src)"
112} >> "$EV/wall.txt"
113# every parallel answer must equal the one-client answer to the same request (parity masks)
114pc=0
115for ph in c16 slow; do
116 for k in $(seq 0 15); do
117 "$PW/pcmp" "$W/mix$k.tsv" "$W/seq-c$k.out" "$W/$ph-c$k.out" "$W/pcmp-$ph-$k.txt" "$SINCE" > /dev/null 2>&1 || pc=1
118 done
119 grep -h '^TOTAL' "$W"/pcmp-$ph-*.txt | awk -v ph=$ph '{ for (i = 2; i <= NF; i++) { split($i, a, "="); t[a[1]] += a[2] } }
120 END { printf "pcmp %s vs one client: records=%d pass=%d fail=%d masked-values=%d\n", ph, t["records"], t["pass"], t["fail"], t["masked-values"] }' >> "$EV/wall.txt"
121done
122stop
123if [ -n "${CONC_CONTROL_BIN:-}" ]; then
124 start "$CONC_CONTROL_BIN"
125 "$W/slow" $SP "$S" 14 "$W/ctl-slow.txt" &
126 SL=$!
127 sleep 1
128 t5=$(ms)
129 par ctl
130 t6=$(ms)
131 wait $SL
132 stop
133 echo "control sha256 $(sha256sum "$CONC_CONTROL_BIN" | cut -c1-64): 16 clients with $S slow clients wall_ns=$((t6 - t5)) transport_errors=$(errs "$W"/ctl-c*.out) latencies=$(wc -l < "$EV/lat-ctl.ns") of $N" >> "$EV/wall.txt"
134 cp "$W/ctl-slow.txt" "$EV/ctl-slow.txt"
135fi
136# restart while slow clients hold handlers (kill -9 of the state process only)
137start "$BIN"
138"$W/slow" $SP 4 14 "$W/rs-slow.txt" &
139RSL=$!
140sleep 1
141OLD=$SPID
142kill -9 "$OLD"
143SPID=
144sleep 1
145t7=$(ms)
146CORTEX_TOKEN=$TOK setsid nohup "$BIN" --port $SP --log "$W/cx.log" --sock "$SOCKD/s2" --encoder-url $ENC \
147 >> "$W/server.out" 2>&1 < /dev/null &
148SPID=$!
149rsup=0
150for i in $(seq 1 100); do
151 curl -sf -o /dev/null --max-time 0.2 "http://127.0.0.1:$SP/health" && { rsup=1; break; }
152 [ $(( ($(ms) - t7) / 1000000 )) -lt 3000 ] || break
153 sleep 0.1
154done
155t8=$(ms)
156wait $RSL
157stop
158kill -9 -- "-$OLD" 2>/dev/null
159echo "restart after kill -9 with 4 slow clients in flight: up=$rsup after $(( (t8 - t7) / 1000000 )) ms" >> "$EV/wall.txt"
160late=$(awk -v m=$SLOWMAX '$2 > m * 1000000000 || $3 == "open"' "$EV/slow.txt" | wc -l)
161ns=$(wc -l < "$EV/slow.txt")
162echo "slow connections: $ns of $S opened, $late not ended by the server within $SLOWMAX s; endings: $(cut -d' ' -f3- "$EV/slow.txt" | sort | uniq -c | tr -s ' ' | tr '\n' ';')" >> "$EV/wall.txt"
163cat "$EV/wall.txt"
164[ "$e1" = 0 ] && [ "$e2" = 0 ] && [ "$e3" = 0 ] || { echo "conc: transport errors" >&2; exit 1; }
165[ "$(wc -l < "$EV/lat-c16.ns")" = "$N" ] && [ "$(wc -l < "$EV/lat-slow.ns")" = "$N" ] || { echo "conc: missing answers" >&2; exit 1; }
166[ "$src" = 0 ] && [ "$ns" = "$S" ] && [ "$late" = 0 ] || { echo "conc: slow clients not handled" >&2; exit 1; }
167[ "$pc" = 0 ] || { echo "conc: parallel answers differ from one-client answers (see $W/pcmp-*.txt)" >&2; exit 1; }
168[ "$rsup" = 1 ] || { echo "conc: a new server could not take the port while killed handlers held slow clients" >&2; exit 1; }
169exit 0