code wiki / _hdl_build / nx_vroom_loadtest.nx

nx_vroom_loadtest.nx source

↩ module page · 96 lines · 5814 B

1// nx_vroom_loadtest.nx -- REAL load-test of the LIVE daemon (operator: "i dont think you have really 2// measured live performance for optimization"). Hammers a RUNNING nx_vroom_daemon on 127.0.0.1:8446 3// with real frame-sized POSTs over the sovereign HTTP client + measures the ACHIEVED throughput: 4// posts/sec, per-frame latency (us), and a /frames relay round-trip. On localhost there is NO network 5// RTT, so this ISOLATES the daemon's compute ceiling -- if the daemon sustains >>30fps here, the live 6// 9fps is NOT the daemon (it's the client encode / uplink bandwidth / TCP-over-150ms-RTT). Frame = 7// 20KB (384x288 JPEG @Q0.5 ~ this size). license_tier: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host 10import "nx_http_client.nx" 11const K_MAGIC_4096: i64 = 4096 12const K_MAGIC_8446: i64 = 8446 13const K_MAGIC_20000: i64 = 20000 14const K_MAGIC_8192: i64 = 8192 15const K_MAGIC_131072: i64 = 131072 16const K_MAGIC_1000000: i64 = 1000000 17 18func lp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 19func ln2(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; 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} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 } 20func sl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 21 22// one POST to the daemon; the daemon answers keep-alive (won't EOF), so READ THE RESPONSE ONCE then 23// close (2s recv-timeout guard). returns response bytes, <0 on error. 24func post_once(addr: *u8, path: *u8, body: *u8, blen: i64, req: *u8, out: *u8, cap: i64) -> i64 { 25 let fd: i64 = sys_socket(2, 1, 0) // AF_INET, SOCK_STREAM 26 if fd < 0 { return 0 - 1 } 27 if nx_connect_bounded(fd, addr, 16, NX_CONN_DEFAULT_MS) < 0 { sys_close(fd); return 0 - 2 } 28 sys_set_socket_timeout(fd, 2) 29 let rl: i64 = nx_http_client_build_request_post(path, sl(path), "127.0.0.1" as *u8, 9, "image/jpeg" as *u8, 10, body, blen, req) 30 if sys_write(fd, req, rl) != rl { sys_close(fd); return 0 - 3 } 31 let r: i64 = sys_read(fd, out, cap) 32 sys_close(fd) 33 if r <= 0 { return 0 - 4 } 34 return r 35} 36// one GET; read the response once then close (the daemon keep-alives) 37func get_once(addr: *u8, path: *u8, out: *u8, cap: i64) -> i64 { 38 let fd: i64 = sys_socket(2, 1, 0) 39 if fd < 0 { return 0 - 1 } 40 if nx_connect_bounded(fd, addr, 16, NX_CONN_DEFAULT_MS) < 0 { sys_close(fd); return 0 - 2 } 41 sys_set_socket_timeout(fd, 2) 42 let req: *u8 = sys_mmap(K_MAGIC_4096) 43 let rl: i64 = nx_http_client_build_request(path, sl(path), "127.0.0.1" as *u8, 9, req) 44 if sys_write(fd, req, rl) != rl { sys_close(fd); return 0 - 3 } 45 let r: i64 = sys_read(fd, out, cap) 46 sys_close(fd) 47 return r 48} 49 50func main() -> i64 { 51 let addr: *u8 = sys_mmap(16) 52 nx_http_client_sockaddr_ipv4(addr, 127, 0, 0, 1, K_MAGIC_8446) 53 let blen: i64 = K_MAGIC_20000 // 20KB ~ a 384x288 JPEG @Q0.5 54 let body: *u8 = sys_mmap(blen) 55 var i: i64=0; while i < blen { body[i] = ((i*37 + 11) & 255) as u8; i=i+1 } 56 let req: *u8 = sys_mmap(blen + K_MAGIC_8192) 57 let out: *u8 = sys_mmap(K_MAGIC_131072) 58 59 lp("=== nx_vroom_loadtest -- REAL daemon throughput on 127.0.0.1:8446 (no network RTT) ===\n") 60 61 // warm-up + reachability: one POST. if it fails, the daemon isn't up. 62 let w: i64 = post_once(addr, "/post?room=lt&id=100" as *u8, body, blen, req, out, K_MAGIC_131072) 63 if w < 0 { lp(" DAEMON NOT REACHABLE on :8446 (rc="); ln2(w); lp(") -- is nx_vroom_daemon running?\n"); sys_exit(2); return 2 } 64 65 // ---- POST throughput: M frames as fast as possible, wall-clock ---- 66 let M: i64 = 300 67 let t0: i64 = sys_now_us() 68 var fails: i64=0; var lat_max: i64=0 69 i=0 70 while i < M { 71 let s0: i64 = sys_now_us() 72 let r: i64 = post_once(addr, "/post?room=lt&id=100" as *u8, body, blen, req, out, K_MAGIC_131072) 73 let d: i64 = sys_now_us() - s0 74 if r < 0 { fails = fails + 1 } else { if d > lat_max { lat_max = d } } 75 i = i + 1 76 } 77 let elapsed: i64 = sys_now_us() - t0 78 let avg_us: i64 = elapsed / M 79 var pps: i64 = 0; if elapsed > 0 { pps = (M * K_MAGIC_1000000) / elapsed } 80 let mbps: i64 = (pps * blen * 8) / K_MAGIC_1000000 // bandwidth at this post rate 81 82 // ---- relay round-trip: GET the other peer's frame ---- 83 let g: i64 = get_once(addr, "/frames?room=lt&id=101" as *u8, out, K_MAGIC_131072) 84 85 lp(" POSTed "); ln2(M); lp(" x 20KB frames in "); ln2(elapsed/1000); lp("ms -> "); ln2(pps); lp(" frames/sec (avg "); ln2(avg_us); lp("us/frame, max "); ln2(lat_max); lp("us, fails="); ln2(fails); lp(")\n") 86 lp(" at that rate one stream = "); ln2(mbps); lp(" Mbps; /frames relay round-trip returned "); ln2(g); lp(" bytes\n") 87 lp(" ---- WHERE THE 9 FPS IS / ISN'T ----\n") 88 lp(" - DAEMON ceiling here (localhost, no RTT) = "); ln2(pps); lp(" fps. If >> 30, the daemon is NOT the bottleneck.\n") 89 lp(" - The live 9fps is then upstream of the daemon: (a) client canvas.toBlob JPEG encode, (b) the UPLINK\n") 90 lp(" bandwidth (20KB x fps: 15fps=2.4Mbps -- a constrained Belarus uplink caps fps), (c) TCP POST stalls over 150ms RTT.\n") 91 lp(" - Config cap is FPS=15 @384x288 Q0.5 (vroom_client.html:55) -- raising it needs a smaller frame (our codec) + adaptive bitrate first.\n") 92 if fails == 0 { lp("VERDICT: measured. daemon fps="); ln2(pps); lp(" (real). Optimize the MEASURED bottleneck, not the daemon.\n"); sys_exit(0) } 93 lp("VERDICT: "); ln2(fails); lp(" post failures -- daemon under-provisioned or mid-restart\n") 94 sys_exit(1) 95 return 0 96}