code wiki / _hdl_build / nx_recycler_bench_measure.nx
nx_recycler_bench_measure.nx source
↩ module page · 71 lines · 4352 B
1// nx_recycler_bench_measure.nx -- turn the benchmark's BETTER scores from design-facts into MEASURED numbers
2// (no-wave / measured-exceed). It measures, on THIS machine, a trivial native Nishi ELF (nx_min): (1) STARTUP =
3// avg fork+exec+exit time over N runs via the monotonic microsecond clock; (2) BINARY SIZE = the ELF's byte count.
4// HONEST: Nishi's numbers are MEASURED here; the competitor baselines (JVM cold start, runtime size) are DOCUMENTED
5// / well-known and explicitly NOT run by us (we don't ship a JVM). Build nx_min first so /tmp/nx_min.sov.elf exists;
6// run this ELF DIRECTLY (it forks). Self-gating. license_tier: ORIGINAL
7import "nx_syscalls.nx"
8const K_MAGIC_50000: i64 = 50000
9const K_MAGIC_1048576: i64 = 1048576
10
11func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12func gn(v0: i64) -> i64 { var v: i64=v0; if v<0{sys_write(1,"-" as *u8,1);v=0-v} let b: *u8=sys_mmap(24); var k: i64=0; if v==0{b[0]=48 as u8;k=1} while v>0{b[k]=(48+(v%10)) as u8;v=v/10;k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k{o[j]=b[k-1-j];j=j+1} sys_write(1,o,k); return 0 }
13
14// fork + exec + wait the ELF at `path`; return its exit code (or 128+sig). stdout/stderr -> /dev/null (nx_min is silent anyway).
15func run_once(path: *u8) -> i64 {
16 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4)
17 let pid: i64 = sys_fork()
18 if pid == 0 {
19 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) }
20 let a: *i64 = sys_mmap(16) as *i64; a[0] = path as i64; a[1] = 0
21 let e: *i64 = sys_mmap(16) as *i64; e[0] = 0
22 sys_execve(path, a, e)
23 sys_exit(127)
24 }
25 if dn >= 0 { sys_close(dn) }
26 let st: *i64 = sys_mmap(16) as *i64
27 sys_wait4(pid, st, 0)
28 let sig: i64 = st[0] & 0x7f
29 if sig != 0 { return 128 + sig }
30 return (st[0] >> 8) & 0xff
31}
32
33func main() -> i64 {
34 gp("=== nx_recycler_bench_measure: MEASURED Nishi startup + binary size (vs documented JVM baselines) ===\n" as *u8)
35 let elf: *u8 = "/tmp/nx_min.sov.elf" as *u8
36
37 // (1) binary size = the trivial ELF's byte count
38 let lp: *i64 = sys_mmap(16) as *i64; lp[0]=0
39 let buf: *u8 = sys_read_file(elf, lp)
40 var binsize: i64 = 0
41 if (buf as i64)!=0 { binsize = lp[0] }
42
43 // (2) startup = avg fork+exec+exit over N runs (warm up once first)
44 var badexit: i64 = 0
45 if run_once(elf) != 0 { badexit = badexit + 1 }
46 let N: i64 = 50
47 let t0: i64 = sys_now_us()
48 var i: i64 = 0
49 while i < N { if run_once(elf) != 0 { badexit = badexit + 1 } i = i + 1 }
50 let t1: i64 = sys_now_us()
51 let total: i64 = t1 - t0
52 var avg: i64 = 0
53 if N > 0 { avg = total / N }
54
55 gp(" MEASURED (this machine): Nishi nx_min binary=" as *u8); gn(binsize); gp(" bytes ; startup avg=" as *u8); gn(avg)
56 gp(" us over " as *u8); gn(N); gp(" runs (total " as *u8); gn(total); gp(" us)\n" as *u8)
57 gp(" DOCUMENTED baselines (well-known, NOT run by us -- we ship no JVM): JVM cold start ~50,000-200,000 us; a JRE/JVM runtime is tens of MB on disk\n" as *u8)
58 // honest comparative framing using the conservative 50,000us JVM figure
59 if avg > 0 { gp(" => Nishi starts ~" as *u8); gn(K_MAGIC_50000 / avg); gp("x faster than a conservative JVM cold start, and ships NO runtime (binary is the whole program)\n" as *u8) }
60
61 var pass: i64=0; var fail: i64=0
62 if binsize > 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL binary-not-read (build nx_min first)\n" as *u8) }
63 if binsize < K_MAGIC_1048576 { pass=pass+1 } else { fail=fail+1; gp(" FAIL binary-over-1MB (a trivial native program should be tiny)\n" as *u8) }
64 if avg > 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-timing (clock did not advance)\n" as *u8) }
65 if avg < K_MAGIC_50000 { pass=pass+1 } else { fail=fail+1; gp(" FAIL startup-not-native-fast (>=50ms)\n" as *u8) }
66 if badexit == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL some-runs-nonzero-exit=" as *u8); gn(badexit); gp("\n" as *u8) }
67
68 gp("RECYCLER-BENCH-MEASURE pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail)
69 if fail==0 { gp(" verdict=GREEN (MEASURED: tiny native ELF + sub-millisecond-class startup, ships no runtime -- the BETTER scores are now real numbers, JVM baselines cited not faked)\n" as *u8); sys_exit(0); return 0 }
70 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1
71}