code wiki / _hdl_build / nx_dtroundtrip_bench.nx

nx_dtroundtrip_bench.nx source

↩ module page · 72 lines · 3416 B

1// nx_dtroundtrip_bench.nx -- dt_parse -> dt_serialize round-trip on a REAL page. 2// Grades the 07-23 claim "the DOM parse->mutate->serialize round-trip EMPTIES real pages" 3// under the sealed-terminator-fixed compiler. GREEN iff: out_bytes >= 60% of in_bytes, 4// a known content marker survives, and the whole trip stays under 2s. 5// Usage: nx_dtroundtrip_bench <htmlfile> <marker-cstr> 6import "nx_syscalls.nx" 7import "nx_domtree.nx" 8const K_MAGIC_65536: i64 = 65536 9const K_MAGIC_2000: i64 = 2000 10 11func rt_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 12func rt_pn(v: i64) -> i64 { 13 var m: i64 = v 14 if m < 0 { rt_p("-\x00" as *u8); m = 0 - m } 15 let t: *u8 = sys_mmap(24); var k: i64 = 0 16 if m == 0 { t[0] = 48 as u8; k = 1 } else { while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } } 17 let o: *u8 = sys_mmap(24); var j: i64 = 0 18 while j < k { o[j] = t[k-1-j]; j = j + 1 } 19 sys_write(1, o, k); return 0 20} 21func rt_has(buf: *u8, blen: i64, needle: *u8) -> i64 { 22 var nl: i64 = 0 23 while needle[nl] != (0 as u8) { nl = nl + 1 } 24 if nl == 0 { return 1 } 25 var i: i64 = 0 26 while (i + nl) <= blen { 27 var j: i64 = 0 28 var ok: i64 = 1 29 while j < nl { if (buf[i + j] & 0xff) != (needle[j] & 0xff) { ok = 0; j = nl } else { j = j + 1 } } 30 if ok == 1 { return 1 } 31 i = i + 1 32 } 33 return 0 34} 35 36func main(argc: i64, argv: *i64) -> i64 { 37 if argc < 3 { rt_p("usage: nx_dtroundtrip_bench <htmlfile> <marker>\x0a\x00" as *u8); return 2 } 38 let path: *u8 = argv[1] as *u8 39 let marker: *u8 = argv[2] as *u8 40 let lenp: *i64 = sys_mmap(16) as *i64 41 let html: *u8 = sys_read_file(path, lenp) 42 if (html as i64) == 0 { rt_p("READ-FAIL\x0a\x00" as *u8); return 3 } 43 let hlen: i64 = lenp[0] 44 let t0: i64 = sys_now_ms() 45 let t: *DomTree = dt_parse(html, hlen) 46 let cap: i64 = hlen * 2 + K_MAGIC_65536 47 let out: *u8 = sys_mmap(cap) 48 let olen: i64 = dt_serialize(t, out, cap) 49 let t1: i64 = sys_now_ms() 50 // Emit the serialized result so the ROUND-TRIPPED page can itself be rendered and 51 // measured. Same-bytes-in/out is not the only question -- what matters downstream is 52 // whether the re-serialized DOM LAYS OUT the same, which needs the artifact. 53 let ofd: i64 = sys_openat_wr("_build/roundtrip_out.html\x00" as *u8, 420) 54 if ofd >= 0 { sys_write(ofd, out, olen); sys_close(ofd) } 55 rt_p("in=\x00" as *u8); rt_pn(hlen) 56 rt_p(" out=\x00" as *u8); rt_pn(olen) 57 rt_p(" nodes=\x00" as *u8); rt_pn(t.n) 58 rt_p(" ms=\x00" as *u8); rt_pn(t1 - t0) 59 let mk_in: i64 = rt_has(html, hlen, marker) 60 let mk_out: i64 = rt_has(out, olen, marker) 61 rt_p(" marker_in=\x00" as *u8); rt_pn(mk_in) 62 rt_p(" marker_out=\x00" as *u8); rt_pn(mk_out) 63 rt_p("\x0a\x00" as *u8) 64 var red: i64 = 0 65 if olen * 10 < hlen * 6 { rt_p("RED: output under 60pct of input (content loss)\x0a\x00" as *u8); red = 1 } 66 if mk_in == 1 { if mk_out == 0 { rt_p("RED: content marker LOST in round-trip\x0a\x00" as *u8); red = 1 } } 67 if mk_in == 0 { rt_p("RED: marker absent from INPUT (bad fixture/marker)\x0a\x00" as *u8); red = 1 } 68 if (t1 - t0) >= K_MAGIC_2000 { rt_p("RED: round-trip over 2s\x0a\x00" as *u8); red = 1 } 69 if red == 1 { rt_p("VERDICT=RED\x0a\x00" as *u8); return 1 } 70 rt_p("VERDICT=GREEN\x0a\x00" as *u8) 71 return 0 72}