nx_trace_query_test.nx source
↩ module page · 99 lines · 4322 B
1// nx_trace_query_test.nx -- filter/count/project shape verification.
2//
3// Builds a synthetic CBOR span stream (5 spans, mixed kinds + traces),
4// runs every query primitive against it, verifies expected counts.
5
6import "nx_syscalls.nx"
7import "nx_tier.nx"
8import "nx_cbor.nx"
9import "nx_trace_emit.nx"
10import "nx_trace_query.nx"
11
12func main() -> nx_int {
13 let buf: *u8 = (sys_mmap(2048)) as *u8
14 let attrs_a: *i64 = (sys_mmap(32)) as *i64
15 let attrs_b: *i64 = (sys_mmap(32)) as *i64
16
17 // ===== Build the test stream =================================
18 //
19 // Span 1: trace=100, span=1, parent=-1, kind=BATCH_OPEN, no attrs
20 // Span 2: trace=100, span=2, parent=1, kind=OUTFIT_PICK, attr=(0xCAFE, 200)
21 // Span 3: trace=100, span=3, parent=1, kind=OUTFIT_PICK, attr=(0xCAFE, 201)
22 // Span 4: trace=100, span=4, parent=1, kind=OUTFIT_PICK, attr=(0xCAFE, 200)
23 // Span 5: trace=100, span=5, parent=1, kind=BATCH_CLOSE, attrs=4 pairs
24 // { (0xAA, 4), (0xBB, 1), (0xCC, 3), (0xDD, 1) } // n_scenes=4 etc
25
26 attrs_a[0] = 0xCAFE; attrs_a[1] = 200
27 var p: nx_int = nx_cbor_emit_span(buf, 0, 100, 1, 0 - 1, NX_SPAN_BATCH_OPEN, 0, 1, attrs_a, 0)
28 p = nx_cbor_emit_span(buf, p, 100, 2, 1, NX_SPAN_OUTFIT_PICK, 1, 2, attrs_a, 1)
29
30 attrs_a[1] = 201
31 p = nx_cbor_emit_span(buf, p, 100, 3, 1, NX_SPAN_OUTFIT_PICK, 2, 3, attrs_a, 1)
32
33 attrs_a[1] = 200
34 p = nx_cbor_emit_span(buf, p, 100, 4, 1, NX_SPAN_OUTFIT_PICK, 3, 4, attrs_a, 1)
35
36 attrs_b[0] = 0xAA; attrs_b[1] = 4
37 attrs_b[2] = 0xBB; attrs_b[3] = 1
38 attrs_b[4] = 0xCC; attrs_b[5] = 3
39 attrs_b[6] = 0xDD; attrs_b[7] = 1
40 p = nx_cbor_emit_span(buf, p, 100, 5, 1, NX_SPAN_BATCH_CLOSE, 4, 5, attrs_b, 4)
41
42 let n_bytes: nx_int = p
43
44 // ===== count_kinds ==========================================
45 let counts: *i64 = (sys_mmap(NX_SPAN_N_KINDS * 8)) as *i64
46 let total: nx_int = nx_tq_count_kinds(buf, n_bytes, counts)
47 if total != 5 { return 1 }
48 if counts[NX_SPAN_BATCH_OPEN] != 1 { return 2 }
49 if counts[NX_SPAN_OUTFIT_PICK] != 3 { return 3 }
50 if counts[NX_SPAN_BATCH_CLOSE] != 1 { return 4 }
51 if counts[NX_SPAN_ARC_PICK] != 0 { return 5 }
52 if counts[NX_SPAN_LOCATION_PICK] != 0 { return 6 }
53
54 // ===== count_for_trace ======================================
55 if nx_tq_count_for_trace(buf, n_bytes, 100) != 5 { return 10 }
56 if nx_tq_count_for_trace(buf, n_bytes, 999) != 0 { return 11 }
57
58 // ===== extract_batch_summaries ==============================
59 //
60 // Should find 1 BATCH_CLOSE span; its attrs land in slot 0.
61 let summaries: *i64 = (sys_mmap(NX_TQ_SUMMARY_STRIDE * 4 * 8)) as *i64
62 let n_sums: nx_int = nx_tq_extract_batch_summaries(buf, n_bytes, summaries, 4)
63 if n_sums != 1 { return 20 }
64 // slot 0 layout: [trace=100, n_attrs=4, k0,v0, k1,v1, k2,v2, k3,v3]
65 if summaries[0] != 100 { return 21 }
66 if summaries[1] != 4 { return 22 }
67 if summaries[2] != 0xAA { return 23 }
68 if summaries[3] != 4 { return 24 }
69 if summaries[4] != 0xBB { return 25 }
70 if summaries[5] != 1 { return 26 }
71 if summaries[6] != 0xCC { return 27 }
72 if summaries[7] != 3 { return 28 }
73 if summaries[8] != 0xDD { return 29 }
74 if summaries[9] != 1 { return 30 }
75
76 // ===== distinct_attr_values =================================
77 //
78 // Distinct values at attr_key=0xCAFE across OUTFIT_PICK spans:
79 // span 2 -> 200, span 3 -> 201, span 4 -> 200 -> distinct = {200, 201}
80 let distinct: *i64 = (sys_mmap(64)) as *i64
81 let n_dist: nx_int = nx_tq_distinct_attr_values(buf, n_bytes,
82 NX_SPAN_OUTFIT_PICK,
83 0xCAFE,
84 distinct, 8)
85 if n_dist != 2 { return 40 }
86 // Order matches insertion order: first 200, then 201
87 if distinct[0] != 200 { return 41 }
88 if distinct[1] != 201 { return 42 }
89
90 // Different kind -> 0 distinct
91 if nx_tq_distinct_attr_values(buf, n_bytes,
92 NX_SPAN_POSE_PICK, 0xCAFE,
93 distinct, 8) != 0 { return 43 }
94
95 // ===== Empty buffer -> 0 total ==============================
96 if nx_tq_count_kinds(buf, 0, counts) != 0 { return 50 }
97
98 return 0
99}