code wiki / _hdl_build / nx_apm_mock.nx
nx_apm_mock.nx source
↩ module page · 82 lines · 4207 B
1// nx_apm_mock.nx -- SOVEREIGN controllable mock HTTP daemon for gating the internal APM collector.
2//
3// WHY: the collector (nx_apm_collect) must be PROVEN to (a) measure low latency on a healthy daemon,
4// (b) detect a daemon that SERIALIZES + wedges under concurrent load (the live :8791 reader symptom),
5// (c) survive a daemon that is slower than the probe timeout (graceful degradation, no infinite hang),
6// and (d) NOT report a DOWN daemon as healthy (liar-kill). This mock provides all four conditions
7// deterministically without touching any real daemon.
8//
9// It is SINGLE-THREADED-SERIAL on purpose: it accepts one connection at a time, optionally sleeps
10// `delay_ms`, then responds 200 + closes. Under N concurrent connects the Nth client therefore waits
11// ~N*delay_ms -- exactly the serialization-amplification that makes "tabs don't load". It serves
12// `max_reqs` requests then exits (SELF-TERMINATING -> the gate needs no kill primitive; it just reaps).
13//
14// usage: nx_apm_mock <port> <delay_ms> <max_reqs> (binds 127.0.0.1 only -- never LAN-exposed)
15// license_tier: ORIGINAL
16import "nx_syscalls.nx"
17const AMK_MAGIC_4096: i64 = 4096
18const AMK_MAGIC_18099: i64 = 18099
19const AMK_MAGIC_1000000: i64 = 1000000
20
21const AMK_RESP: *u8 = "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok"
22
23func amk_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
24
25// decimal C-string -> i64
26func amk_atoi(s: *u8) -> i64 {
27 var v: i64 = 0; var i: i64 = 0
28 while s[i] != (0 as u8) { let c: i64 = s[i] as i64; if c >= 48 { if c <= 57 { v = v*10 + (c-48) } } i = i + 1 }
29 return v
30}
31
32// build a 16-byte sockaddr_in for a.b.c.d:port (family AF_INET, port network-order).
33func amk_addr(out: *u8, a: i64, b: i64, c: i64, d: i64, port: i64) -> i64 {
34 out[0] = 2 as u8; out[1] = 0 as u8 // AF_INET (little-endian 16-bit = 2,0)
35 out[2] = ((port >> 8) & 0xff) as u8; out[3] = (port & 0xff) as u8
36 out[4] = a as u8; out[5] = b as u8; out[6] = c as u8; out[7] = d as u8
37 var i: i64 = 8; while i < 16 { out[i] = 0 as u8; i = i + 1 }
38 return 16
39}
40
41// bind 127.0.0.1:port, serve exactly max_reqs requests, then return.
42// delay_ms = sleep BEFORE responding -> simulates slow-to-FIRST-BYTE = a REAL wedge (slow ttfb).
43// hold_ms = sleep AFTER the body, before close -> simulates an idle-held/keep-alive connection = the
44// read-until-close MEASUREMENT ARTIFACT (ttfb fast, total slow). A ttfb-based monitor must
45// call this GREEN; only a read-until-close monitor false-flags it.
46func apm_mock_listen(port: i64, delay_ms: i64, hold_ms: i64, max_reqs: i64) -> i64 {
47 let fd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0)
48 if fd < 0 { return 0 - 1 }
49 let one: *i64 = sys_mmap(8) as *i64; one[0] = 1
50 sys_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, one as *u8, 4) // re-runnable: no EADDRINUSE on TIME_WAIT
51 let sa: *u8 = sys_mmap(16); amk_addr(sa, 127, 0, 0, 1, port)
52 if sys_bind(fd, sa, 16) < 0 { sys_close(fd); return 0 - 2 }
53 if sys_listen(fd, 64) < 0 { sys_close(fd); return 0 - 3 }
54 let rbuf: *u8 = sys_mmap(AMK_MAGIC_4096)
55 let rlen: i64 = amk_slen(AMK_RESP)
56 var i: i64 = 0
57 while i < max_reqs {
58 let cfd: i64 = sys_accept(fd)
59 if cfd >= 0 {
60 if delay_ms > 0 { sys_sleep_ms(delay_ms) } // SERIAL stall before first byte -> Nth client waits N*delay
61 sys_read(cfd, rbuf, AMK_MAGIC_4096) // consume the request line/headers
62 sys_write(cfd, AMK_RESP, rlen)
63 if hold_ms > 0 { sys_sleep_ms(hold_ms) } // hold connection open AFTER body = idle-close artifact
64 sys_close(cfd)
65 }
66 i = i + 1
67 }
68 sys_close(fd)
69 return 0
70}
71
72func main(argc: i64, argv: *i64) -> i64 {
73 var port: i64 = AMK_MAGIC_18099
74 var delay: i64 = 0
75 var hold: i64 = 0
76 var maxr: i64 = AMK_MAGIC_1000000
77 if argc >= 2 { port = amk_atoi(argv[1] as *u8) }
78 if argc >= 3 { delay = amk_atoi(argv[2] as *u8) }
79 if argc >= 4 { hold = amk_atoi(argv[3] as *u8) }
80 if argc >= 5 { maxr = amk_atoi(argv[4] as *u8) }
81 return apm_mock_listen(port, delay, hold, maxr)
82}