code wiki / (root) / bench_emit.nx

bench_emit.nx source

↩ module page · 106 lines · 2646 B

1// bench_emit.nx -- stream workload through OUR primitives, emit numbers to stdout. 2// 3// Used by the cross-implementation harness (bench_vs_ds.sh). Writes 4// key=value lines to stdout that the harness parses and compares 5// against Apache DataSketches output. 6 7import "syscalls.nx" 8import "sketch_hll.nx" 9import "sketch_cpc_dense.nx" 10 11// === decimal printer (i64 to stdout) ============================= 12 13func nx_emit_putc(c: i64) -> i64 { 14 let buf: *u8 = sys_mmap(1) 15 buf[0] = c & 0xFF 16 sys_write(1, buf, 1) 17 return 0 18} 19 20func nx_emit_str(s: *u8, len: i64) -> i64 { 21 sys_write(1, s, len) 22 return 0 23} 24 25func nx_emit_i64(n: i64) -> i64 { 26 if n < 0 { 27 nx_emit_putc(45) // '-' 28 return nx_emit_i64(-n) 29 } 30 if n == 0 { 31 nx_emit_putc(48) // '0' 32 return 0 33 } 34 // Build digits in reverse. 35 let digits: *u8 = sys_mmap(32) 36 var d: i64 = 0 37 var v: i64 = n 38 while v > 0 { 39 digits[d] = (v % 10) + 48 40 v = v / 10 41 d = d + 1 42 } 43 // Print in correct order. 44 while d > 0 { 45 d = d - 1 46 nx_emit_putc(digits[d]) 47 } 48 return 0 49} 50 51func nx_emit_newline() -> i64 { 52 nx_emit_putc(10) 53 return 0 54} 55 56// === main ======================================================== 57 58func main() -> i64 { 59 let n: i64 = 1000 60 61 // OUR HLL_8 at lg_k=7 (m=128, ~9.2% stddev, 24+128 = 152 bytes) 62 let hll: *Hll = nx_hll_alloc(7, 42) 63 64 // OUR CPC dense at lg_k=3, w=16 (big_M=128, ~8.8% stddev, 72+16 = 88 bytes) 65 let cpc: *CpcDense = nx_cpcd_alloc(3, 16, 42) 66 67 // Stream N distinct keys through both. 68 let buf: *u8 = sys_mmap(8) 69 var i: i64 = 0 70 while i < n { 71 buf[0] = (i ) & 0xFF 72 buf[1] = (i >> 8 ) & 0xFF 73 buf[2] = (i >> 16) & 0xFF 74 buf[3] = 0xA1 75 buf[4] = 0 76 buf[5] = 0 77 buf[6] = 0 78 buf[7] = 0 79 nx_hll_add(hll, buf, 8) 80 nx_cpcd_add(cpc, buf, 8) 81 i = i + 1 82 } 83 84 // Output: our_hll_estimate, our_hll_bytes, our_cpc_estimate, our_cpc_bytes 85 nx_emit_str("our_hll_estimate=", 17) 86 nx_emit_i64(nx_hll_estimate(hll)) 87 nx_emit_newline() 88 89 nx_emit_str("our_hll_bytes=", 14) 90 nx_emit_i64(24 + 128) 91 nx_emit_newline() 92 93 nx_emit_str("our_cpc_estimate=", 17) 94 nx_emit_i64(nx_cpcd_estimate(cpc)) 95 nx_emit_newline() 96 97 nx_emit_str("our_cpc_bytes=", 14) 98 nx_emit_i64(nx_cpcd_memory_bytes(cpc)) 99 nx_emit_newline() 100 101 nx_emit_str("workload_n=", 11) 102 nx_emit_i64(n) 103 nx_emit_newline() 104 105 return 0 106}