code wiki / (root) / nx_perf_bench_test.nx

nx_perf_bench_test.nx source

↩ module page · 52 lines · 1763 B

1// nx_perf_bench_test.nx -- smoke for the perf-bench primitive. 2 3import "syscalls.nx" 4import "nx_clock.nx" 5import "nx_perf_bench.nx" 6 7func main() -> i64 { 8 // Bench a simple arithmetic loop 100 times. 9 let r: *PerfRun = nx_perf_run_alloc("trivial_loop" as *u8, 100) 10 11 var i: i64 = 0 12 while i < 100 { 13 let t0: i64 = nx_clock_gettime(NX_CLOCK_MONOTONIC) 14 var sum: i64 = 0 15 var k: i64 = 0 16 while k < 1000 { 17 sum = sum + k 18 k = k + 1 19 } 20 let t1: i64 = nx_clock_gettime(NX_CLOCK_MONOTONIC) 21 nx_perf_record_sample(r, i, t1 - t0) 22 i = i + 1 23 } 24 nx_perf_summarize(r) 25 26 // After 100 samples we should have valid min <= median <= max. 27 if r.min_ns > r.median_ns { return 1 } 28 if r.median_ns > r.max_ns { return 2 } 29 if r.mean_ns < 0 { return 3 } 30 31 // Declare a generous baseline (e.g., 1ms = 1e6 ns) -> we should win. 32 nx_perf_set_baseline(r, "synthetic_1ms_baseline" as *u8, 1000000) 33 if r.verdict != NX_PERF_VERDICT_WINS { return 10 } 34 35 // Declare a baseline tighter than our median -> should LOSE. 36 nx_perf_set_baseline(r, "tight_baseline" as *u8, 1) 37 if r.verdict != NX_PERF_VERDICT_LOSES { return 20 } 38 39 // Declare baseline = our median -> should WIN (we tie or beat). 40 nx_perf_set_baseline(r, "median_eq" as *u8, r.median_ns) 41 if r.verdict != NX_PERF_VERDICT_WINS { return 30 } 42 43 // Declare 0 baseline -> NO_BASELINE. 44 nx_perf_set_baseline(r, "none" as *u8, 0) 45 if r.verdict != NX_PERF_VERDICT_NO_BASELINE { return 40 } 46 47 // Emit card for inspection. 48 nx_perf_set_baseline(r, "Mathematica_simulated_1ms" as *u8, 1000000) 49 nx_perf_emit_card(2, r) 50 51 return 0 52}