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/fts/run.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 / fts / run.sh` 206 lines ยท 13756 bytes Raw Blame History 1 #!/bin/sh 2 # tests/fts/run.sh : differential tests of om/uni_fts.om, om/flog.om, om/fts.om, om/like.om 3 # against SQLite 3.45.0 (sqlite3.c of libsqlite3-sys 0.28.0, built with the -D flags of its 4 # build.rs and gcc -O3 like the cc crate inside cortex-rs) and glibc libm, plus negative controls. 5 # 6 # usage: sh tests/fts/run.sh DB DB: a copy of a cortex-rs database (opened read only) 7 # env: OMC (pinned compiler), SQLITE_SRC (dir with sqlite3.c/sqlite3.h), WORK (scratch dir), 8 # FTS_EVIDENCE (log dir, default evidence/fts; check.sh points it into $CHECK_DIR) 9 # exit 0 only when every differential is bit-identical AND every negative control is detected. 10 set -u 11 HERE=$(cd "$(dirname "$0")" && pwd) 12 ROOT=$(cd "$HERE/../.." && pwd) 13 DB=${1:?usage: run.sh DB} 14 OMC=$(sh "$ROOT/tools/omc.sh") || exit 2 15 SQLITE_SRC=${SQLITE_SRC:-$HOME/.cargo/registry/src/index.crates.io-`1949cf8c6b5b557f`/libsqlite3-sys-0.28.0/sqlite3} 16 WORK=${WORK:-$ROOT/build/fts-work} 17 EV=${FTS_EVIDENCE:-$ROOT/evidence/fts} 18 N="nice -n 19 ionice -c3" 19 mkdir -p "$WORK" "$EV" 20 LOG="$EV/run.log" 21 : > "$LOG" 22 say() { printf '%s\n' "$*" | tee -a "$LOG"; } 23 fail=0 24 check() { # name, expect(0=same,1=differ), file_a, file_b 25 if cmp -s "$3" "$4"; then r=same; else r=differ; fi 26 if { [ "$2" = 0 ] && [ $r = same ]; } || { [ "$2" = 1 ] && [ $r = differ ]; }; then 27 say "PASS $1 ($r)" 28 else 29 say "FAIL $1 ($r)"; fail=1 30 fi 31 } 32 say "date $(date -u +%FT%TZ) host $(uname -m)" 33 say "omc $(sha256sum "$OMC" | cut -c1-64)" 34 say "sqlite3.c $(sha256sum "$SQLITE_SRC/sqlite3.c" | cut -c1-64)" 35 say "libm $(sha256sum "$(readlink -f /lib/aarch64-linux-gnu/libm.so.6)" | cut -c1-64)" 36 say "db $(sha256sum "$DB" | cut -c1-64)" 37 say "gcc $(gcc --version | head -1)" 38 for f in uni_fts flog fts like; do say "om/$f.om $(sha256sum "$ROOT/om/$f.om" | cut -c1-64) fns=$(grep -c '^fn ' "$ROOT/om/$f.om")"; done 39 40 # ---- oracle ---- 41 FLAGS="-DSQLITE_CORE -DSQLITE_DEFAULT_FOREIGN_KEYS=1 -DSQLITE_ENABLE_API_ARMOR -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_LOAD_EXTENSION=1 -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_STAT2 -DSQLITE_ENABLE_STAT4 -DSQLITE_SOUNDEX -DSQLITE_THREADSAFE=1 -DSQLITE_USE_URI -DHAVE_USLEEP=1 -D_POSIX_THREAD_SAFE_FUNCTIONS -DHAVE_ISNAN -DHAVE_LOCALTIME_R" 42 if [ ! -x "$WORK/oracle" ] || [ "$HERE/oracle.c" -nt "$WORK/oracle" ]; then 43 [ -f "$WORK/sqlite3.o" ] || $N gcc -O3 -fPIC -ffunction-sections -fdata-sections $FLAGS -I"$SQLITE_SRC" -c "$SQLITE_SRC/sqlite3.c" -o "$WORK/sqlite3.o" || exit 2 44 $N gcc -O2 -I"$SQLITE_SRC" -c "$HERE/oracle.c" -o "$WORK/oracle.o" && gcc -o "$WORK/oracle" "$WORK/oracle.o" "$WORK/sqlite3.o" -lm -lpthread -ldl || exit 2 45 fi 46 O="$WORK/oracle" 47 $O plan "$DB" | tee -a "$LOG" 48 49 # ---- build harnesses (library files concatenated in build order + harness) ---- 50 build() { # name, extra files... 51 name=$1; shift 52 cat "$@" > "$WORK/$name.om" 53 $N "$OMC" < "$WORK/$name.om" > "$WORK/$name" 2> "$WORK/$name.err"; rc=$? 54 if [ $rc -ne 0 ] || [ -s "$WORK/$name.err" ]; then say "FAIL compile $name rc=$rc $(cat "$WORK/$name.err")"; exit 3; fi 55 chmod +x "$WORK/$name" 56 } 57 LIB="$ROOT/om/uni_fts.om $ROOT/om/flog.om $ROOT/om/fts.om $ROOT/om/like.om $HERE/tlib.om $HERE/tdump.om" 58 build t_flog $LIB "$HERE/t_flog.om" 59 build t_tok $LIB "$HERE/t_tok.om" 60 build t_query $LIB "$HERE/t_query.om" 61 build t_like $LIB "$HERE/t_like.om" 62 say "compiled: t_flog t_tok t_query t_like" 63 64 # ---- inputs ---- 65 $O dump "$DB" "$WORK/dump.bin" 66 $O tok "$DB" "$WORK/tok.txt" 67 python3 "$HERE/gen_tokf.py" "$WORK/tokf.bin" >> "$LOG" 68 $O tokf "$WORK/tokf.bin" "$WORK/tokf.txt" 69 $O log "$WORK/log.bin" `1000000` 3000 4200 2>> "$LOG" 70 $O fma "$WORK/fma.bin" `2000000` 71 python3 "$HERE/gen_queries.py" "$DB" "$WORK/q1.bin" 2400 `20261026` >> "$LOG" 72 python3 "$HERE/gen_queries.py" "$DB" "$WORK/q2.bin" 3000 99 >> "$LOG" 73 python3 "$HERE/gen_like.py" "$DB" "$WORK/like.bin" 600 >> "$LOG" 74 rm -f "$WORK/mut.db"; $O mutate "$DB" "$WORK/mut.db" "$WORK/ops.txt" 75 say "ops $(grep -c '^D' "$WORK/ops.txt") deletes $(grep -c '^P' "$WORK/ops.txt") re-inserts" 76 77 # ---- differentials ---- 78 if $N "$WORK/t_flog" l "$WORK/log.bin" > "$WORK/flog.txt"; then say "PASS flog vs glibc log: $(cat "$WORK/flog.txt")"; else say "FAIL flog vs glibc log: $(head -3 "$WORK/flog.txt")"; fail=1; fi 79 if $N "$WORK/t_flog" f "$WORK/fma.bin" > "$WORK/fma.txt"; then say "PASS flog_fma vs FMADD: $(cat "$WORK/fma.txt")"; else say "FAIL flog_fma vs FMADD: $(head -3 "$WORK/fma.txt")"; fail=1; fi 80 $N "$WORK/t_tok" "$WORK/dump.bin" > "$WORK/tok_om.txt"; check "tokenizer, all entities_fts rows" 0 "$WORK/tok_om.txt" "$WORK/tok.txt" 81 $N "$WORK/t_tok" "$WORK/tokf.bin" f > "$WORK/tokf_om.txt"; check "tokenizer, every code point + invalid UTF-8" 0 "$WORK/tokf_om.txt" "$WORK/tokf.txt" 82 $O query "$DB" "$WORK/q1.bin" "$WORK/q1_or.txt" 83 $N "$WORK/t_query" "$WORK/dump.bin" "$WORK/q1.bin" > "$WORK/q1_om.txt" 2> "$WORK/q1.time" 84 check "search corpus 1 (2400 queries x 5 spaces x 4 limits)" 0 "$WORK/q1_om.txt" "$WORK/q1_or.txt" 85 say "timing q1: $(cat "$WORK/q1.time")" 86 $O query "$DB" "$WORK/q2.bin" "$WORK/q2_or.txt" 87 $N "$WORK/t_query" "$WORK/dump.bin" "$WORK/q2.bin" > "$WORK/q2_om.txt" 2> "$WORK/q2.time" 88 check "search corpus 2 (3000 queries x 5 spaces x 4 limits)" 0 "$WORK/q2_om.txt" "$WORK/q2_or.txt" 89 say "timing q2: $(cat "$WORK/q2.time")" 90 $O query "$WORK/mut.db" "$WORK/q1.bin" "$WORK/qm_or.txt" 91 $N "$WORK/t_query" "$WORK/dump.bin" "$WORK/q1.bin" "$WORK/ops.txt" > "$WORK/qm_om.txt" 2> "$WORK/qm.time" 92 check "search after delete/re-insert + token-pool compaction" 0 "$WORK/qm_om.txt" "$WORK/qm_or.txt" 93 say "timing after delete/re-insert: $(cat "$WORK/qm.time")" 94 # the same searches with the plan of fts_search forced (1 scan of the space's rows, 2 term lists, 95 # 3 every row as SQLite runs it, 4 chosen but never the term-count loop that OR-of-terms queries 96 # take), before and after the delete/re-insert ops: each must give the oracle's answers, and the 97 # incremental index must equal a rebuild (else an icheck= line differs) 98 for rt in 1 2 3 4; do 99 $N "$WORK/t_query" "$WORK/dump.bin" "$WORK/q1.bin" - $rt > "$WORK/q1_r$rt.txt" 2> "$WORK/q1_r$rt.time" 100 check "search corpus 1, plan $rt forced" 0 "$WORK/q1_r$rt.txt" "$WORK/q1_or.txt" 101 $N "$WORK/t_query" "$WORK/dump.bin" "$WORK/q1.bin" "$WORK/ops.txt" $rt > "$WORK/qm_r$rt.txt" 2> "$WORK/qm_r$rt.time" 102 check "search after delete/re-insert, plan $rt forced" 0 "$WORK/qm_r$rt.txt" "$WORK/qm_or.txt" 103 done 104 $O like "$DB" "$WORK/like.bin" "$WORK/like_or.txt" 105 $N "$WORK/t_like" "$WORK/dump.bin" "$WORK/like.bin" > "$WORK/like_om.txt" 106 check "LIKE (600 patterns x every entity)" 0 "$WORK/like_om.txt" "$WORK/like_or.txt" 107 # latency: the 200 most frequent title words as single-term queries (x 5 spaces x 4 limits) 108 python3 - "$DB" "$WORK/lat.bin" <<'EOF' 109 import collections, re, sqlite3, struct, sys 110 con = sqlite3.connect('file:%s?mode=ro' % sys.argv[1], uri=True) 111 c = collections.Counter(w.lower() for (t,) in con.execute('SELECT canonical_name FROM entities_fts') for w in re.findall(r'[A-Za-z]{3,}', t)) 112 open(sys.argv[2], 'wb').write(b''.join(struct.pack(' "$WORK/lat_om.txt" 2> "$WORK/lat.time" 116 check "single-term latency corpus (200 frequent words)" 0 "$WORK/lat_om.txt" "$WORK/lat_or.txt" 117 say "timing single-term: $(cat "$WORK/lat.time")" 118 python3 - "$WORK" >> "$LOG" <<'EOF' 119 import sys, collections 120 w = sys.argv[1] 121 for name in ('q1', 'q2'): 122 L = open('%s/%s_or.txt' % (w, name), 'rb').read().split(b'\n')[:-1] 123 st = collections.Counter(l.split()[3] for l in L) 124 nz = sum(1 for l in L if not l.split()[4].endswith(b'=0')) 125 rows = sum(int(l.split()[4][2:]) for l in L) 126 ties = sum(1 for l in L if len(set(x.split(b':')[1] for x in l.split()[5:])) < len(l.split()[5:])) 127 print('%s lines=%d ok=%d err=%d nonempty=%d rows=%d lines_with_tied_bm25=%d' % (name, len(L), st[b'ok'], st[b'err'], nz, rows, ties)) 128 L = open('%s/like_or.txt' % w).read().split('\n')[:-1] 129 print('like patterns=%d err=%d matches=%d' % (len(L), sum(1 for l in L if ' err ' in l), sum(int(l.split()[2][2:]) for l in L))) 130 EOF 131 132 # ---- negative controls: each mutant must make its differential FAIL ---- 133 mutant() { # name, file, python replace expression old new, harness, run command... 134 name=$1; file=$2; old=$3; new=$4; harness=$5; shift 5 135 mkdir -p "$WORK/mut"; cp "$ROOT/om/uni_fts.om" "$ROOT/om/flog.om" "$ROOT/om/fts.om" "$ROOT/om/like.om" "$WORK/mut/" 136 python3 - "$WORK/mut/$file" "$old" "$new" <<'EOF' 137 import sys 138 p, old, new = sys.argv[1:4] 139 s = open(p).read() 140 if old.startswith('@qutx:'): 141 # change one number of the unicode table text: @qutx:INDEX, new = replacement value 142 i = int(old[6:]) 143 a = s.index('text qutx "') + len('text qutx "'); b = s.index('"', a) 144 v = s[a:b].split(' ') 145 assert v[i] != new, 'mutation is a no-op' 146 v[i] = new 147 s = s[:a] + ' '.join(v) + s[b:] 148 else: 149 assert s.count(old) >= 1, 'mutation anchor not found: ' + old 150 s = s.replace(old, new, 1) 151 open(p, 'w').write(s) 152 EOF 153 cat "$WORK/mut/uni_fts.om" "$WORK/mut/flog.om" "$WORK/mut/fts.om" "$WORK/mut/like.om" "$HERE/tlib.om" "$HERE/tdump.om" "$HERE/$harness.om" > "$WORK/mut/h.om" 154 $N "$OMC" < "$WORK/mut/h.om" > "$WORK/mut/h" 2> "$WORK/mut/h.err" || { say "FAIL negative control $name: mutant did not compile"; fail=1; return; } 155 chmod +x "$WORK/mut/h" 156 "$@" 157 } 158 # 1. bm25 k1 = 1.3 instead of 1.2 (K uses k1, the numerator uses k1+1) 159 mutant "bm25 k1=1.3" fts.om "kk = mul add kk 0.25 bitcast f64 ulong `4608083138725491507`" "kk = mul add kk 0.25 1.3" t_query \ 160 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' > '$WORK/mut/q.txt' 2>/dev/null" 161 check "negative control bm25 k1=1.3 detected" 1 "$WORK/mut/q.txt" "$WORK/q1_or.txt" 162 # 2. bm25 without the fused last term (plain multiply-add) 163 mutant "bm25 unfused tail" fts.om "s = flog_fma t get qidf i s" "s = add s mul t get qidf i" t_query \ 164 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' > '$WORK/mut/q.txt' 2>/dev/null" 165 check "negative control unfused bm25 tail detected" 1 "$WORK/mut/q.txt" "$WORK/q1_or.txt" 166 # 3. one tokenizer table entry: aiOff[1] (index QUAO+1 = 4037) 2 -> 3, a case-fold offset 167 mutant "aiOff entry" uni_fts.om "@qutx:4037" "3" t_tok \ 168 sh -c "$N '$WORK/mut/h' '$WORK/tokf.bin' f > '$WORK/mut/t.txt' 2>/dev/null" 169 check "negative control tokenizer table entry detected" 1 "$WORK/mut/t.txt" "$WORK/tokf.txt" 170 # 4. log table: ln2lo replaced by 0 171 mutant "log ln2lo" flog.om "d = flog_fma flog_t 1 kd d" "d = flog_fma 0.0 kd d" t_flog \ 172 sh -c "$N '$WORK/mut/h' l '$WORK/log.bin' > '$WORK/mut/l.txt'; echo rc=\$? >> '$WORK/mut/l.txt'" 173 if grep -q 'mismatches=0' "$WORK/mut/l.txt"; then say "FAIL negative control flog ln2lo not detected"; fail=1; else say "PASS negative control flog ln2lo detected ($(head -1 "$WORK/mut/l.txt"))"; fi 174 # 5. LIKE without ASCII case folding 175 mutant "LIKE case" like.om "if not and and eq fts_lower c fts_lower c2 lt c 128 lt c2 128" "if true" t_like \ 176 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/like.bin' > '$WORK/mut/k.txt' 2>/dev/null" 177 check "negative control LIKE case folding detected" 1 "$WORK/mut/k.txt" "$WORK/like_or.txt" 178 # 6. tie order: unstable insertion (ties before earlier rows) 179 mutant "tie order" fts.om "if le get qrb sub p 1 b 180 break" "if lt get qrb sub p 1 b 181 break" t_query \ 182 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' > '$WORK/mut/q.txt' 2>/dev/null" 183 check "negative control unstable tie order detected" 1 "$WORK/mut/q.txt" "$WORK/q1_or.txt" 184 # 7-10. the incremental index: each skipped update must change an answer or the icheck line 185 mutant "qdf not counted" fts.om "put qdf t uint add ulong get qdf t 1" "put qdf t uint ulong get qdf t" t_query \ 186 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' '$WORK/ops.txt' > '$WORK/mut/q.txt' 2>/dev/null" 187 check "negative control index: qdf not counted on put detected" 1 "$WORK/mut/q.txt" "$WORK/qm_or.txt" 188 mutant "term node not linked" fts.om "put qlh t uint add k 1" "put qlv 1 get qlv 1" t_query \ 189 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' '$WORK/ops.txt' > '$WORK/mut/q.txt' 2>/dev/null" 190 check "negative control index: term node not linked detected" 1 "$WORK/mut/q.txt" "$WORK/qm_or.txt" 191 mutant "group count not kept" fts.om "put qgc g uint add ulong get qgc g 1" "put qgc g get qgc g" t_query \ 192 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' '$WORK/ops.txt' > '$WORK/mut/q.txt' 2>/dev/null" 193 check "negative control index: group count not kept detected" 1 "$WORK/mut/q.txt" "$WORK/qm_or.txt" 194 mutant "generation not bumped" fts.om "put qgen doc uint g" "put qgen doc get qgen doc" t_query \ 195 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' '$WORK/ops.txt' > '$WORK/mut/q.txt' 2>/dev/null" 196 check "negative control index: generation not bumped on delete detected" 1 "$WORK/mut/q.txt" "$WORK/qm_or.txt" 197 # 11-12. the term-count loop counting each term once per row, and the LIKE byte search without case folding 198 mutant "term counted once" fts.om "put qotc k add v 1" "put qotc k 1" t_query \ 199 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/q1.bin' > '$WORK/mut/q.txt' 2>/dev/null" 200 check "negative control term-count loop counting once per row detected" 1 "$WORK/mut/q.txt" "$WORK/q1_or.txt" 201 mutant "LIKE literal case" like.om "b = add b 32" "b = add b 0" t_like \ 202 sh -c "$N '$WORK/mut/h' '$WORK/dump.bin' '$WORK/like.bin' > '$WORK/mut/k.txt' 2>/dev/null" 203 check "negative control LIKE byte search without case folding detected" 1 "$WORK/mut/k.txt" "$WORK/like_or.txt" 204 205 if [ $fail -eq 0 ]; then say "RESULT PASS"; else say "RESULT FAIL"; fi 206 exit $fail