code wiki / (root) / nx_bench_companion_timed_test.nx

nx_bench_companion_timed_test.nx source

↩ module page · 84 lines · 3281 B

1// nx_bench_companion_timed_test.nx -- smoke for wall-clock bench wrapper. 2 3import "nx_syscalls.nx" 4import "nx_tier.nx" 5import "nx_clock.nx" 6import "nx_actor.nx" 7import "nx_message.nx" 8import "nx_session.nx" 9import "nx_bench_companion.nx" 10import "nx_bench_companion_timed.nx" 11 12func main() -> i64 { 13 let now: nx_size = 1000000 14 15 // 1: verdict enum 16 if nx_bctm_v_is_valid(NX_BCTM_OK) != 1 { return 1 } 17 if nx_bctm_v_is_valid(NX_BCTM_NEGATIVE_ELAPSED) != 1 { return 2 } 18 if nx_bctm_v_is_valid(NX_BCTM_N) != 0 { return 3 } 19 if nx_bctm_v_is_valid(-1) != 0 { return 4 } 20 21 // 2: timed report construction 22 let base: *NxBenchReport = nx_bc_report_new() 23 let t: *NxBenchTimedReport = nx_bctm_new(base) 24 if (t as i64) == 0 { return 5 } 25 if (t.base as i64) != (base as i64) { return 6 } 26 if t.wallclock_elapsed_us != 0 { return 7 } 27 if t.verdict != NX_BCTM_OK { return 8 } 28 29 // 3: null base rejected 30 let null_base: *NxBenchReport = (0 as i64) as *NxBenchReport 31 if nx_bctm_new(null_base) != (0 as *NxBenchTimedReport) { return 9 } 32 33 // 4: session + drive a small workload + measure wall-clock 34 let s: *NxSession = nx_session_new_default(now) 35 if nx_session_is_ready(s) != 1 { return 10 } 36 nx_session_spawn_actor(s, 1001, 1, 80, 0, now) 37 38 t.wallclock_start_ns = nx_clock_monotonic_ns() 39 // Drive 20 cooperative steps -- meaningful enough for wall-clock 40 // to register a positive elapsed_us under qemu. 41 var i: nx_int = 0 42 while i < 20 { 43 nx_ac_step(s.scheduler, 1001, 1000, now + (i as nx_size) * 100) 44 i = i + 1 45 } 46 let v_cap: nx_int = nx_bctm_capture_finish(t, s, 11) 47 if v_cap != NX_BCTM_OK { return 11 } 48 49 // 5: wall-clock elapsed must be non-negative (start_ns <= end_ns) 50 if t.wallclock_elapsed_us < 0 { return 12 } 51 if nx_bctm_elapsed_us(t) < 0 { return 13 } 52 if nx_bctm_verdict(t) != NX_BCTM_OK { return 14 } 53 54 // 6: substrate counters captured into base 55 if base.total_actor_steps != 20 { return 15 } 56 if base.cumulative_runtime_us != 20000 { return 16 } // 20 * 1000us 57 if base.n_actors != 1 { return 17 } 58 59 // 7: end-ns >= start-ns (monotonic invariant) 60 if t.wallclock_end_ns < t.wallclock_start_ns { return 18 } 61 62 // 8: capture_finish on null session rejected 63 let null_s: *NxSession = (0 as i64) as *NxSession 64 if nx_bctm_capture_finish(t, null_s, 11) != NX_BCTM_NULL_SESSION { return 19 } 65 66 // 9: capture_finish on null timed report rejected 67 let null_t: *NxBenchTimedReport = (0 as i64) as *NxBenchTimedReport 68 if nx_bctm_capture_finish(null_t, s, 11) != NX_BCTM_INVALID { return 20 } 69 70 // 10: negative-elapsed detection: synthetic start_ns in the future 71 let base2: *NxBenchReport = nx_bc_report_new() 72 let t2: *NxBenchTimedReport = nx_bctm_new(base2) 73 let now_ns: i64 = nx_clock_monotonic_ns() 74 t2.wallclock_start_ns = now_ns + 1000000000 // 1s in the future 75 let v_neg: nx_int = nx_bctm_capture_finish(t2, s, 11) 76 if v_neg != NX_BCTM_NEGATIVE_ELAPSED { return 21 } 77 if nx_bctm_verdict(t2) != NX_BCTM_NEGATIVE_ELAPSED { return 22 } 78 79 // 11: accessor null guards 80 if nx_bctm_elapsed_us(null_t) != -1 { return 23 } 81 if nx_bctm_verdict(null_t) != NX_BCTM_INVALID { return 24 } 82 83 return 0 84}