code wiki / _hdl_build / nx_cms_cache_exceed_gate.nx

nx_cms_cache_exceed_gate.nx source

↩ module page · 122 lines · 7094 B

1// nx_cms_cache_exceed_gate.nx -- W-RE-004 head-to-head EXCEED (caching-perf, PERF axis = the R4-P2 2// wrk-vs-nginx pattern, but with a SOVEREIGN deterministic metric: BODY BYTES TRANSFERRED, not wallclock). 3// Serves the SAME unchanged resource K times: 4// OURS = RFC 7232 conditional requests via nx_http_cache (ETag + If-None-Match -> 304, no body) 5// INCUMBENT = no conditional-request support -> 200 + full body every time (the WP-default uncached path 6// wp-super-cache exists to fix). 7// Metric: total body bytes sent for K repeats of an unchanged page (LOWER wins). Correctness GUARD (the 8// perf referee enforces it): OURS must 304 ONLY when unchanged and serve fresh 200 when the resource 9// CHANGES -- a perf win bought by serving stale content is rejected. Verdict by nx_cms_exceed. 10// license_tier: ORIGINAL 11import "nx_cms_exceed.nx" 12import "nx_http_cache.nx" 13import "nx_syscalls.nx" 14 15const KQ_REQUESTS: i64 = 100 16 17func kx_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func kx_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=48+(m%10);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 } 19func kx_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var k: i64=0; while s[k]!=(0 as u8){dst[o]=s[k];o=o+1;k=k+1} return o } 20func kx_catnum(dst: *u8, off: i64, v: i64) -> i64 { 21 var o: i64=off; let t: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; var k: i64=0 22 if m==0 {t[0]=48 as u8;k=1}; while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1} 23 var i: i64=0; while i<k {dst[o]=t[k-1-i]; o=o+1; i=i+1} return o 24} 25func kx_cpy(dst: *u8, src: *u8) -> i64 { var k: i64=0; while src[k]!=(0 as u8){dst[k]=src[k];k=k+1} return k } 26func kx_row(id: i64, ok: i64, what: *u8) -> i64 { 27 kx_w("KXROW " as *u8); kx_num(id); kx_w(" " as *u8) 28 if ok==1 { kx_w("PASS " as *u8) } else { kx_w("FAIL " as *u8) } 29 kx_w(what); kx_w("\n" as *u8) 30 return ok 31} 32 33// compute the ETag hex for a body into etag (returns etag length) 34func kx_etag(body: *u8, bn: i64, etag: *u8) -> i64 { 35 let off: *i64 = sys_mmap(8) as *i64 36 off[0] = 0 37 nx_http_etag_compute(body, bn, etag, off, 64) 38 return off[0] 39} 40 41func main() -> i64 { 42 var pass: i64 = 0 43 var rows: i64 = 0 44 45 // a rendered page (the resource) + its ETag 46 let body: *u8 = sys_mmap(4096) 47 let bn: i64 = kx_cpy(body, "<h1>Andelin West Law</h1><p>Estate planning, family law, business formation.</p>" as *u8) 48 let etag: *u8 = sys_mmap(64) 49 let etn: i64 = kx_etag(body, bn, etag) 50 51 // a CHANGED version of the page (content edited) + its new ETag 52 let body2: *u8 = sys_mmap(4096) 53 let bn2: i64 = kx_cpy(body2, "<h1>Andelin West Law</h1><p>NOW ALSO probate and trusts.</p>" as *u8) 54 let etag2: *u8 = sys_mmap(64) 55 let etn2: i64 = kx_etag(body2, bn2, etag2) 56 57 // ---- PERF measurement: serve the UNCHANGED page KQ_REQUESTS times ---- 58 var our_bytes: i64 = 0 59 var inc_bytes: i64 = 0 60 var r: i64 = 0 61 while r < KQ_REQUESTS { 62 // OURS: first request has no If-None-Match -> 200 + body; subsequent send the ETag -> 304, no body 63 if r == 0 { our_bytes = our_bytes + bn } 64 else { if nx_http_if_none_match_matches(etag, etn, etag, etn) == 1 { } else { our_bytes = our_bytes + bn } } 65 // INCUMBENT: no conditional support -> 200 + full body every time 66 inc_bytes = inc_bytes + bn 67 r = r + 1 68 } 69 70 let verdict: i64 = xcd_verdict_perf(our_bytes, inc_bytes) 71 72 // ---- CORRECTNESS GUARD: ours must not serve STALE (304 only when truly unchanged) ---- 73 var our_correct: i64 = 0 74 let ncorr: i64 = 3 75 // case 1: unchanged + matching ETag -> 304 (skip body) is CORRECT (and safe) 76 if nx_http_if_none_match_matches(etag, etn, etag, etn) == 1 { our_correct = our_correct + 1 } 77 // case 2: CHANGED resource + client's STALE ETag -> must NOT match -> serve fresh 200 (no stale) 78 if nx_http_if_none_match_matches(etag, etn, etag2, etn2) == 0 { our_correct = our_correct + 1 } 79 // case 3: first visit, no If-None-Match -> serve 200 (modelled: empty header never matches) 80 let empty: *u8 = sys_mmap(8); empty[0] = 0 as u8 81 if nx_http_if_none_match_matches(empty, 0, etag, etn) == 0 { our_correct = our_correct + 1 } 82 83 kx_w("HEAD-TO-HEAD caching-perf (body bytes for " as *u8); kx_num(KQ_REQUESTS) 84 kx_w(" repeats of an unchanged page): ours=" as *u8); kx_num(our_bytes) 85 kx_w(" incumbent=" as *u8); kx_num(inc_bytes) 86 kx_w(" correctness=" as *u8); kx_num(our_correct); kx_w("/" as *u8); kx_num(ncorr) 87 kx_w(" verdict=" as *u8); kx_w(xcd_vname(verdict)); kx_w("\n" as *u8) 88 89 var ok: i64 = 0; if verdict == XCD_AHEAD { ok = 1 } 90 rows=rows+1; pass=pass+kx_row(0, ok, "perf: ours transfers fewer body bytes (304 on repeat)" as *u8) 91 ok = 0; if our_bytes == bn { if inc_bytes == bn * KQ_REQUESTS { ok = 1 } } 92 rows=rows+1; pass=pass+kx_row(1, ok, "ours sends body ONCE; incumbent re-sends every request" as *u8) 93 ok = 0; if our_correct == ncorr { ok = 1 } 94 rows=rows+1; pass=pass+kx_row(2, ok, "correctness guard: 304 only when unchanged, fresh 200 when changed (no stale)" as *u8) 95 ok = 0; if xcd_referee_perf_ok(verdict, our_correct, ncorr) == 1 { ok = 1 } 96 rows=rows+1; pass=pass+kx_row(3, ok, "perf referee accepts (win did NOT sacrifice correctness)" as *u8) 97 ok = 0; if xcd_referee_perf_ok(XCD_AHEAD, ncorr - 1, ncorr) == 0 { ok = 1 } 98 rows=rows+1; pass=pass+kx_row(4, ok, "perf referee rejects a win bought by serving stale (anti-false-green)" as *u8) 99 ok = 0; if xcd_verdict_perf(500, 100) == XCD_BEHIND { if xcd_verdict_perf(100, 100) == XCD_PARITY { ok = 1 } } 100 rows=rows+1; pass=pass+kx_row(5, ok, "perf verdict reports BEHIND/PARITY honestly (cannot only-ever-win)" as *u8) 101 102 kx_w("CMS-CACHE-EXCEED-GATE rows=" as *u8); kx_num(rows); kx_w(" pass=" as *u8); kx_num(pass); kx_w("\n" as *u8) 103 104 if pass == rows { 105 if verdict == XCD_AHEAD { 106 if xcd_referee_perf_ok(verdict, our_correct, ncorr) == 1 { 107 let line: *u8 = sys_mmap(256) 108 var lo: i64 = kx_cat(line, 0, "CMSEXCEED feature=caching-perf axis=perf-bandwidth metric=body-bytes rfc=RFC-7232 ours=" as *u8) 109 lo = kx_catnum(line, lo, our_bytes) 110 lo = kx_cat(line, lo, " incumbent=" as *u8); lo = kx_catnum(line, lo, inc_bytes) 111 lo = kx_cat(line, lo, " requests=" as *u8); lo = kx_catnum(line, lo, KQ_REQUESTS) 112 lo = kx_cat(line, lo, " verdict=AHEAD\n" as *u8) 113 let gf: i64 = sys_openat_append("knowledge/status/cms_exceed.log" as *u8, 0x1a4) 114 if gf >= 0 { sys_write(gf, line, lo); sys_close(gf) } 115 kx_w("CMS-CACHE-EXCEED-GATE verdict=AHEAD -- measured EXCEED recorded\n" as *u8) 116 sys_exit(0); return 0 117 } 118 } 119 } 120 kx_w("CMS-CACHE-EXCEED-GATE verdict=NOT-RECORDED (no fake-green)\n" as *u8) 121 sys_exit(1); return 1 122}