cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / conc / run.sh
| 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. |
| 21 | set -u |
| 22 | SRC=${1:?usage: run.sh SRC.db} |
| 23 | R=$(cd "$(dirname "$0")/../.." && pwd) |
| 24 | PW=${PARITY_WORK:-$R/build/parity} |
| 25 | W=${CONC_WORK:-$R/build/conc} |
| 26 | EV=${CONC_EVIDENCE:-$R/evidence/conc} |
| 27 | BIN=${CORTEX_BIN:-$R/build/cortex} |
| 28 | IMP=${CORTEX_IMPORT:-$R/build/cortex-import} |
| 29 | OMC=$(sh "$R/tools/omc.sh") || { echo "conc: pinned omc unavailable" >&2; exit 2; } |
| 30 | S=${CONC_SLOW:-200} |
| 31 | SLOWMAX=11 |
| 32 | SP=18290 |
| 33 | TOK=paritytok |
| 34 | ENC=http://127.0.0.1:9 |
| 35 | SPID= |
| 36 | stop() { |
| 37 | [ -n "$SPID" ] && kill "$SPID" 2>/dev/null |
| 38 | SPID= |
| 39 | sleep 1 |
| 40 | } |
| 41 | fail() { echo "conc: $*" >&2; stop; exit 2; } |
| 42 | waitup() { |
| 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 | } |
| 49 | start() { # 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 | } |
| 55 | ms() { date +%s%N; } |
| 56 | D=$PW/pdrive |
| 57 | [ -x "$D" ] && [ -x "$PW/pcmp" ] && [ -f "$PW/corpus.tsv" ] || fail "run tests/parity/run.sh first" |
| 58 | curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$SP/health" && fail "port $SP is in use" |
| 59 | rm -rf "$W" |
| 60 | mkdir -p "$W" "$EV" || exit 2 |
| 61 | # private socket in a short directory (sockaddr_un path: 108 bytes, a kernel boundary) |
| 62 | SOCKD=$(mktemp -d /tmp/cxsock.XXXXXX) || exit 2 |
| 63 | trap '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" |
| 66 | if [ $? -ne 0 ] || [ -s "$W/slow.err" ]; then fail "slow.om did not compile"; fi |
| 67 | chmod +x "$W/slow" |
| 68 | awk -F'\t' '$1 ~ /^(search|recall|get|traverse)\./' "$PW/corpus.tsv" > "$W/mix.tsv" |
| 69 | N=$(wc -l < "$W/mix.tsv") |
| 70 | [ "$N" -ge 2000 ] || fail "read mix has only $N requests" |
| 71 | for k in $(seq 0 15); do awk -v k=$k 'NR % 16 == k' "$W/mix.tsv" > "$W/mix$k.tsv"; done |
| 72 | sqlite3 "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" |
| 74 | start "$BIN" |
| 75 | : > "$EV/wall.txt" |
| 76 | par() { # 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 | } |
| 86 | errs() { cat "$@" | grep -c '^#[0-9]* ERR$'; } |
| 87 | SINCE=$(date -u +%Y-%m-%dT%H:%M:%S) |
| 88 | t0=$(ms) |
| 89 | for 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" |
| 91 | done |
| 92 | t1=$(ms) |
| 93 | for k in $(seq 0 15); do cat "$W/lat-seq-c$k.ns"; done > "$EV/lat-seq.ns" |
| 94 | par c16 |
| 95 | t2=$(ms) |
| 96 | "$W/slow" $SP "$S" 14 "$W/slow.txt" & |
| 97 | SL=$! |
| 98 | sleep 1 |
| 99 | t3=$(ms) |
| 100 | par slow |
| 101 | t4=$(ms) |
| 102 | wait $SL |
| 103 | src=$? |
| 104 | cp "$W/slow.txt" "$EV/slow.txt" |
| 105 | e1=$(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) |
| 114 | pc=0 |
| 115 | for 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" |
| 121 | done |
| 122 | stop |
| 123 | if [ -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" |
| 135 | fi |
| 136 | # restart while slow clients hold handlers (kill -9 of the state process only) |
| 137 | start "$BIN" |
| 138 | "$W/slow" $SP 4 14 "$W/rs-slow.txt" & |
| 139 | RSL=$! |
| 140 | sleep 1 |
| 141 | OLD=$SPID |
| 142 | kill -9 "$OLD" |
| 143 | SPID= |
| 144 | sleep 1 |
| 145 | t7=$(ms) |
| 146 | CORTEX_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 & |
| 148 | SPID=$! |
| 149 | rsup=0 |
| 150 | for 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 |
| 154 | done |
| 155 | t8=$(ms) |
| 156 | wait $RSL |
| 157 | stop |
| 158 | kill -9 -- "-$OLD" 2>/dev/null |
| 159 | echo "restart after kill -9 with 4 slow clients in flight: up=$rsup after $(( (t8 - t7) / 1000000 )) ms" >> "$EV/wall.txt" |
| 160 | late=$(awk -v m=$SLOWMAX '$2 > m * 1000000000 || $3 == "open"' "$EV/slow.txt" | wc -l) |
| 161 | ns=$(wc -l < "$EV/slow.txt") |
| 162 | echo "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" |
| 163 | cat "$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; } |
| 169 | exit 0 |