interplane.Get Omega
Read in your language

mailhand

mailhand: agents look after a person's email in seeded Omega. Maildir + MIME parsing, JSONL CLI and MCP, rule triage, policy-gated sending, hash-chained audit. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

mailhand / tests / bench.om

682 lines · 16063 bytes Raw Blame History
1note ======== tests/bench.om: mailhand speed benchmark (GateBuilder) ========
2note Usage: bench MAILHAND DIR [N [SEED]] N default 10000, SEED default 1 (decimal, 0..2^64-1).
3note Build: ./build.sh tests/BENCH_UNITS build/bench. Example from the repository root:
4note build/bench build/mailhand build/benchrun (build/benchrun must not exist yet)
5note MAILHAND is passed to execprog as given (no PATH search; relative to the current directory).
6note DIR (made absolute with the current directory) must not exist; its parent must. The bench:
7note 1. creates DIR and generates the Maildir DIR/Mail with N messages from SEED (tests/bench-lib.om,
8note the same bytes as `mkmaildir DIR/Mail N SEED`), timed with monons: line "MHBENCH gen";
9note 2. runs MAILHAND setup --home DIR/h --maildir DIR/Mail --me bench@example.org --name Bench
10note (not a measurement; it must exit 0): line "MHBENCH setup";
11note 3. runs MAILHAND list --home DIR/h --folder INBOX: line "MHBENCH list";
12note 4. runs MAILHAND stats --home DIR/h --folder INBOX (stats parses every message and prints only
13note counts: the parse-only mode of src/ops.om op_stats): line "MHBENCH stats";
14note 5. prints "MHBENCH PASS ..." (exit 0) or "MHBENCH FAIL ..." (exit 2).
15note Each run: stdin /dev/null, stdout DIR/OP.out, stderr DIR/OP.err (kept for inspection), the empty
16note environment of execprog. wall_ns = monons just after the child is reaped minus monons just before
17note fork: process start, config load, the op over all N messages, writing OP.out, the audit record
18note (src/audit.om, fsync) and exit. The wait has no time limit (no-hard-caps rule: a hang shows as a
19note bench that does not end). One run per op, no warm-up: the page cache holds the files just written.
20note Fields: N messages generated, bytes = sum of their file sizes, messages_per_s = N * 10^9 / wall_ns,
21note MB_per_s = bytes * 10^3 / wall_ns (decimal MB, 10^6 bytes), both truncated to 3 decimals by exact
22note integer long division (bn_ratio; "n/a" when wall_ns is 0 or above 1844674407370955161),
23note exit = the child's exit status, 1000 + signal number, or 2000 when the bench could not open the
24note files, fork or wait (stderr says which); cmd = the exact argument vector, joined with spaces.
25note list adds records (LF-ended lines of list.out: one JSONL record per message) and refused (records
26note with a "refused":" key). stats adds the op's own record: messages, parsed, refused, path_refused,
27note op_bytes, op_wall_ns (monons around the scan inside mailhand, without process start and output)
28note and op_messages_per_s = messages * 10^9 / op_wall_ns, op_MB_per_s = op_bytes * 10^3 / op_wall_ns.
29note Checks (each failure prints "MHBENCH CHECK_FAIL NAME got=X want=Y" and counts): setup exit 0;
30note list exit 0, records N, refused 0; stats exit 0, messages N, parsed N, refused 0, path_refused 0,
31note op_bytes = bytes. A measurement is printed only together with its checks; no comparison is made
32note with anything not run here.
33note Exit 0 PASS; 1 usage (arguments, DIR exists, a path too long); 2 FAIL (a check failed);
34note 6 I/O (DIR or the Maildir could not be created or written).
35
36const BN_OW int 557633
37note bn_pk: the execprog argument packet (u16 count, then u16 length + bytes per argument); 65536 bytes
38note is the execprog packet limit of the language (OMEGESE section 10), a boundary, not our cap.
39global bn_pk uchar 65536
40note bn_g cells: 0 packet bytes, 1 packet count, 2 packet too big, 3 wall ns of the last run,
41note 4 failed checks, 5 last key found, 6 generated bytes, 7 N
42global bn_g ulong 16
43const BNG_PKN ulong 0
44const BNG_PKC ulong 1
45const BNG_PKBAD ulong 2
46const BNG_NS ulong 3
47const BNG_FAILS ulong 4
48const BNG_KOK ulong 5
49const BNG_BYTES ulong 6
50const BNG_N ulong 7
51const BNG_REC ulong 8
52const BNG_REF ulong 9
53
54text bnt_us "usage: bench MAILHAND DIR [N [SEED]] (N default 10000, SEED default 1; DIR must not exist)"
55text bnt_null "/dev/null"
56text bnt_out ".out"
57text bnt_err ".err"
58text bnt_mail "/Mail"
59text bnt_h "/h"
60text bnt_setup "setup"
61text bnt_list "list"
62text bnt_stats "stats"
63text bnt_home "--home"
64text bnt_maildir "--maildir"
65text bnt_me "--me"
66text bnt_mev "bench@example.org"
67text bnt_name "--name"
68text bnt_namev "Bench"
69text bnt_folder "--folder"
70text bnt_inbox "INBOX"
71text bnt_gen "MHBENCH gen "
72text bnt_ksetup "MHBENCH setup"
73text bnt_klist "MHBENCH list"
74text bnt_kstats "MHBENCH stats"
75text bnt_kwall " wall_ns="
76text bnt_kmps " messages_per_s="
77text bnt_kmbps " MB_per_s="
78text bnt_kexit " exit="
79text bnt_kcmd " cmd="
80text bnt_kn " N="
81text bnt_kbytes " bytes="
82text bnt_krec " records="
83text bnt_kref " refused="
84text bnt_kmd " maildir="
85text bnt_kmsgs " messages="
86text bnt_kpar " parsed="
87text bnt_kpath " path_refused="
88text bnt_ksb " op_bytes="
89text bnt_kons " op_wall_ns="
90text bnt_komps " op_messages_per_s="
91text bnt_kombps " op_MB_per_s="
92text bnt_missing "missing"
93text bnt_jmsg "\"messages\":"
94text bnt_jpar "\"parsed\":"
95text bnt_jref "\"refused\":"
96text bnt_jpath "\"path_refused\":"
97text bnt_jbytes "\"bytes\":"
98text bnt_jns "\"wall_ns\":"
99text bnt_refq "\"refused\":\""
100text bnt_cf "MHBENCH CHECK_FAIL "
101text bnt_kgot " got="
102text bnt_kwant " want="
103text bnt_pass "MHBENCH PASS"
104text bnt_fail "MHBENCH FAIL"
105text bnt_kfails " checks_failed="
106text bnt_csetup "setup_exit"
107text bnt_clexit "list_exit"
108text bnt_clout "list_output_readable"
109text bnt_clrec "list_records"
110text bnt_clref "list_refused"
111text bnt_csexit "stats_exit"
112text bnt_csout "stats_output_readable"
113text bnt_csmsg "stats_messages"
114text bnt_cspar "stats_parsed"
115text bnt_csref "stats_refused"
116text bnt_cspath "stats_path_refused"
117text bnt_csbytes "stats_op_bytes"
118text bnt_eexist "bench: DIR exists; give a directory that does not exist yet (its parent must exist)"
119text bnt_emkdir "bench: cannot create DIR, errno "
120text bnt_epath "bench: DIR is empty, longer than 4095 bytes or holds a NUL byte"
121text bnt_eopen "bench: cannot open /dev/null or an output file under DIR"
122text bnt_efork "bench: fork failed"
123text bnt_ewait "bench: waitpid failed"
124text bnt_epk "bench: the argument packet would pass 65536 bytes (the execprog packet limit)"
125text bnt_eread "bench: cannot read an output file, errno "
126text bnt_ecwd "bench: cannot read the current directory"
127
128note ---- arguments and directories ----
129note bn_args: store S_Y = MAILHAND (argument 1), store S_X = DIR (argument 2) made absolute
130fn bn_args to bool
131b = new uchar 131072 0
132w = new uchar 4096 0
133k = argread 1 b 0 131072
134if le k 0
135ret false
136end
137z = s_mem S_Y
138s_addt S_Y part b 0 ulong k
139k2 = argread 2 b 0 131072
140if le k2 0
141ret false
142end
143z = s_mem S_X
144if ne get b 0 47
145cw = cwd w 0 4096
146if le cw 1
147mk_err bnt_ecwd
148ret false
149end
150s_addt S_X part w 0 sub ulong cw 1
151zc = s_add S_X 47
152end
153s_addt S_X part b 0 ulong k2
154ret true
155end
156
157note bn_mkdir: create DIR; 0, 1 (unusable path or DIR exists), 6 (other error)
158fn bn_mkdir to int
159p_reset
160p_s S_X 0 s_len S_X
161if not p_ok
162mk_err bnt_epath
163ret 1
164end
165r = p_mkdir
166if eq r -17
167mk_err bnt_eexist
168ret 1
169end
170if lt r 0
171mk_errno bnt_emkdir r
172ret 6
173end
174ret 0
175end
176
177note bn_pfile: gp = DIR/NAME + sfx
178fn bn_pfile nm view uchar * sfx view uchar * to void
179p_reset
180p_s S_X 0 s_len S_X
181p_c 47
182p_t nm
183p_t sfx
184end
185
186fn bn_ostore s ulong to void
187for i ulong s_len s bound 9223372036854775807
188o_b s_get s i
189end
190end
191
192note bn_orates: " wall_ns=.. messages_per_s=.. MB_per_s=.."
193fn bn_orates n ulong b ulong ns ulong to void
194o_raw bnt_kwall
195o_digits ns
196o_raw bnt_kmps
197bn_ratio n 9 ns 3
198o_raw bnt_kmbps
199bn_ratio b 3 ns 3
200end
201
202note ---- 1. generate ----
203fn bn_gen n ulong seed ulong to int
204z = s_mem S_AUX
205s_adds S_AUX S_X 0 s_len S_X
206s_addt S_AUX bnt_mail
207t0 = monons
208r = mk_gen n seed
209t1 = monons
210if ne r 0
211ret r
212end
213ns = ulong sub t1 t0
214tb = mk_bytes
215put bn_g BNG_BYTES tb
216put bn_g BNG_N n
217o_raw bnt_gen
218mk_osum
219bn_orates n tb ns
220o_raw bnt_kmd
221bn_ostore S_AUX
222o_b 10
223o_flush
224ret 0
225end
226
227note ---- argument packet ----
228fn bn_pkreset to void
229put bn_g BNG_PKN 2
230put bn_g BNG_PKC 0
231put bn_g BNG_PKBAD 0
232put bn_pk 0 0
233put bn_pk 1 0
234end
235
236note bn_pkhdr: start a record of k bytes; false (and BNG_PKBAD 1) when it would pass 65536 bytes
237fn bn_pkhdr k ulong to bool
238n = get bn_g BNG_PKN
239if gt add add n 2 k 65536
240put bn_g BNG_PKBAD 1
241ret false
242end
243put bn_pk n uchar and k 255
244put bn_pk add n 1 uchar shr k 8
245c = add get bn_g BNG_PKC 1
246put bn_g BNG_PKC c
247put bn_pk 0 uchar and c 255
248put bn_pk 1 uchar shr c 8
249put bn_g BNG_PKN add n 2
250ret true
251end
252
253fn bn_pkt t view uchar * to void
254if not bn_pkhdr len t
255ret
256end
257n = get bn_g BNG_PKN
258for i ulong len t bound 65536
259put bn_pk add n i get t i
260end
261put bn_g BNG_PKN add n len t
262end
263
264fn bn_pks s ulong off ulong k ulong to void
265if not bn_pkhdr k
266ret
267end
268n = get bn_g BNG_PKN
269for i ulong k bound 65536
270put bn_pk add n i s_get s add off i
271end
272put bn_g BNG_PKN add n k
273end
274
275note bn_pkdir: one argument: DIR + sfx
276fn bn_pkdir sfx view uchar * to void
277s_reset S_TMP
278s_adds S_TMP S_X 0 s_len S_X
279s_addt S_TMP sfx
280bn_pks S_TMP 0 s_len S_TMP
281end
282
283note bn_pkop: MAILHAND op --home DIR/h --folder INBOX
284fn bn_pkop op view uchar * to void
285bn_pkreset
286bn_pks S_Y 0 s_len S_Y
287bn_pkt op
288bn_pkt bnt_home
289bn_pkdir bnt_h
290bn_pkt bnt_folder
291bn_pkt bnt_inbox
292end
293
294note bn_ocmd: the packet's arguments joined with spaces
295fn bn_ocmd to void
296c = or ulong get bn_pk 0 shl ulong get bn_pk 1 8
297p = ulong 2
298for a ulong c bound 1024
299k = or ulong get bn_pk p shl ulong get bn_pk add p 1 8
300p = add p 2
301if gt a 0
302o_b 32
303end
304for i ulong k bound 65536
305o_b get bn_pk add p i
306end
307p = add p k
308end
309end
310
311note ---- running ----
312note bn_spawn: run the packet with stdin /dev/null, stdout DIR/NAME.out, stderr DIR/NAME.err and wait
313note (no time limit); BNG_NS = wall ns from just before fork to just after the reap. Result: exit
314note status 0..255, 1000 + signal, 2000 when files, fork or wait failed (stderr says which).
315fn bn_spawn nm view uchar * to ulong
316put bn_g BNG_NS 0
317if ne get bn_g BNG_PKBAD 0
318mk_err bnt_epk
319ret 2000
320end
321fi = fopen bnt_null 0 len bnt_null ORD 0
322bn_pfile nm bnt_out
323fo = long -1
324if p_ok
325fo = fopen gp 0 get gv VPL BN_OW 420
326end
327bn_pfile nm bnt_err
328fx = long -1
329if p_ok
330fx = fopen gp 0 get gv VPL BN_OW 420
331end
332if or or lt fi 0 lt fo 0 lt fx 0
333mk_err bnt_eopen
334ret 2000
335end
336ret bn_run fi fo fx
337end
338
339note bn_run: fork; the child binds fi/fo/fx to 0/1/2 and execs the packet (exit 127 if exec fails)
340fn bn_run fi long fo long fx long to ulong
341t0 = monons
342pid = fork
343if lt pid 0
344mk_err bnt_efork
345ret 2000
346end
347if eq pid 0
348r = dupfd int fi 0 0
349r = dupfd int fo 1 0
350r = dupfd int fx 2 0
351r = execprog bn_pk 0 get bn_g BNG_PKN
352exit 127
353end
354z = fclose int fi
355z = fclose int fo
356z = fclose int fx
357ret bn_reap pid t0
358end
359
360note bn_reap: blocking wait for pid (EINTR retried), BNG_NS = monons now - t0, the decoded status
361fn bn_reap pid long t0 long to ulong
362st = new uchar 4 0
363w = waitpid int pid st 0 4 0
364while eq w -4 bound 9223372036854775807
365w = waitpid int pid st 0 4 0
366end
367t1 = monons
368put bn_g BNG_NS ulong sub t1 t0
369if ne w pid
370mk_err bnt_ewait
371ret 2000
372end
373s = or ulong get st 0 shl ulong get st 1 8
374if eq and s 127 0
375ret and shr s 8 255
376end
377ret add 1000 and s 127
378end
379
380note bn_load: store S_MSG reads DIR/NAME.out; false (stderr names errno) when it cannot
381fn bn_load nm view uchar * to bool
382bn_pfile nm bnt_out
383r = s_file S_MSG
384if lt r 0
385mk_errno bnt_eread r
386ret false
387end
388ret true
389end
390
391note ---- checks ----
392fn bn_chk t view uchar * got ulong want ulong to void
393if eq got want
394ret
395end
396put bn_g BNG_FAILS add get bn_g BNG_FAILS 1
397o_raw bnt_cf
398o_raw t
399o_raw bnt_kgot
400o_digits got
401o_raw bnt_kwant
402o_digits want
403o_b 10
404end
405
406note bn_dec: the decimal number at S_MSG[p, n); BNG_KOK 0 when it does not fit a ulong
407fn bn_dec p ulong n ulong to ulong
408v = ulong 0
409q = p
410while lt q n bound 9223372036854775807
411c = s_get S_MSG q
412if or lt c 48 gt c 57
413ret v
414end
415dg = ulong sub c 48
416if gt v 1844674407370955161
417put bn_g BNG_KOK 0
418ret 0
419end
420if eq v 1844674407370955161
421if gt dg 5
422put bn_g BNG_KOK 0
423ret 0
424end
425end
426v = add mul v 10 dg
427q = add q 1
428end
429ret v
430end
431
432note bn_key: the number after the first occurrence of key t (e.g. "parsed": with quotes) in S_MSG;
433note BNG_KOK 1 when found and in range
434fn bn_key t view uchar * to ulong
435n = s_len S_MSG
436put bn_g BNG_KOK 0
437for i ulong n bound 9223372036854775807
438if eq s_get S_MSG i 34
439if s_eqt S_MSG i len t t
440put bn_g BNG_KOK 1
441ret bn_dec add i len t n
442end
443end
444end
445ret 0
446end
447
448note bn_okey: " label=VALUE" (or "missing") for key t of the stats record
449fn bn_okey lb view uchar * t view uchar * to void
450v = bn_key t
451o_raw lb
452if eq get bn_g BNG_KOK 1
453o_digits v
454else
455o_raw bnt_missing
456end
457end
458
459note bn_chkkey: key t of the stats record equals want (missing counts as a failure)
460fn bn_chkkey lb view uchar * t view uchar * want ulong to void
461v = bn_key t
462if eq get bn_g BNG_KOK 1
463bn_chk lb v want
464ret
465end
466put bn_g BNG_FAILS add get bn_g BNG_FAILS 1
467o_raw bnt_cf
468o_raw lb
469o_raw bnt_kgot
470o_raw bnt_missing
471o_raw bnt_kwant
472o_digits want
473o_b 10
474end
475
476note bn_lscan: LF-ended records and records with a "refused":" key in S_MSG
477fn bn_lscan to void
478n = s_len S_MSG
479rec = ulong 0
480nref = ulong 0
481for i ulong n bound 9223372036854775807
482c = s_get S_MSG i
483if eq c 10
484rec = add rec 1
485end
486if eq c 34
487if s_eqt S_MSG i len bnt_refq bnt_refq
488nref = add nref 1
489end
490end
491end
492put bn_g BNG_REC rec
493put bn_g BNG_REF nref
494end
495
496note ---- 2. setup ----
497fn bn_setup to int
498bn_pkreset
499bn_pks S_Y 0 s_len S_Y
500bn_pkt bnt_setup
501bn_pkt bnt_home
502bn_pkdir bnt_h
503bn_pkt bnt_maildir
504bn_pkdir bnt_mail
505bn_pkt bnt_me
506bn_pkt bnt_mev
507bn_pkt bnt_name
508bn_pkt bnt_namev
509o_flush
510x = bn_spawn bnt_setup
511o_raw bnt_ksetup
512o_raw bnt_kexit
513o_digits x
514o_raw bnt_kwall
515o_digits get bn_g BNG_NS
516o_raw bnt_kcmd
517bn_ocmd
518o_b 10
519bn_chk bnt_csetup x 0
520o_flush
521if ne x 0
522ret 2
523end
524ret 0
525end
526
527note ---- 3. list ----
528fn bn_list to void
529bn_pkop bnt_list
530o_flush
531x = bn_spawn bnt_list
532ns = get bn_g BNG_NS
533n = get bn_g BNG_N
534b = get bn_g BNG_BYTES
535put bn_g BNG_REC 0
536put bn_g BNG_REF 0
537ok = bn_load bnt_list
538if ok
539bn_lscan
540end
541o_raw bnt_klist
542o_raw bnt_kn
543o_digits n
544o_raw bnt_kbytes
545o_digits b
546bn_orates n b ns
547o_raw bnt_kexit
548o_digits x
549o_raw bnt_krec
550o_digits get bn_g BNG_REC
551o_raw bnt_kref
552o_digits get bn_g BNG_REF
553o_raw bnt_kcmd
554bn_ocmd
555o_b 10
556bn_chk bnt_clexit x 0
557if not ok
558bn_chk bnt_clout 0 1
559end
560bn_chk bnt_clrec get bn_g BNG_REC n
561bn_chk bnt_clref get bn_g BNG_REF 0
562o_flush
563end
564
565note ---- 4. stats (parse only) ----
566fn bn_stats to void
567bn_pkop bnt_stats
568o_flush
569x = bn_spawn bnt_stats
570ns = get bn_g BNG_NS
571n = get bn_g BNG_N
572b = get bn_g BNG_BYTES
573ok = bn_load bnt_stats
574o_raw bnt_kstats
575o_raw bnt_kn
576o_digits n
577o_raw bnt_kbytes
578o_digits b
579bn_orates n b ns
580o_raw bnt_kexit
581o_digits x
582bn_okey bnt_kmsgs bnt_jmsg
583bn_okey bnt_kpar bnt_jpar
584bn_okey bnt_kref bnt_jref
585bn_okey bnt_kpath bnt_jpath
586bn_okey bnt_ksb bnt_jbytes
587bn_okey bnt_kons bnt_jns
588bn_oprates
589o_raw bnt_kcmd
590bn_ocmd
591o_b 10
592bn_chk bnt_csexit x 0
593if not ok
594bn_chk bnt_csout 0 1
595end
596bn_chkkey bnt_csmsg bnt_jmsg n
597bn_chkkey bnt_cspar bnt_jpar n
598bn_chkkey bnt_csref bnt_jref 0
599bn_chkkey bnt_cspath bnt_jpath 0
600bn_chkkey bnt_csbytes bnt_jbytes b
601o_flush
602end
603
604note bn_oprates: the op's own rates from its record (n/a when a key is missing)
605fn bn_oprates to void
606m = bn_key bnt_jmsg
607okm = get bn_g BNG_KOK
608ob = bn_key bnt_jbytes
609okb = get bn_g BNG_KOK
610ons = bn_key bnt_jns
611okn = get bn_g BNG_KOK
612if eq and and okm okb okn 1
613o_raw bnt_komps
614bn_ratio m 9 ons 3
615o_raw bnt_kombps
616bn_ratio ob 3 ons 3
617ret
618end
619o_raw bnt_komps
620o_raw mkt_na
621o_raw bnt_kombps
622o_raw mkt_na
623end
624
625note ---- 5. verdict ----
626fn bn_end to int
627f = get bn_g BNG_FAILS
628if eq f 0
629o_raw bnt_pass
630else
631o_raw bnt_fail
632end
633o_raw bnt_kn
634o_digits get bn_g BNG_N
635o_raw bnt_kbytes
636o_digits get bn_g BNG_BYTES
637o_raw bnt_kfails
638o_digits f
639o_b 10
640o_flush
641if eq f 0
642ret 0
643end
644ret 2
645end
646
647fn main to int
648c = ulong argcnt
649if or lt c 3 gt c 5
650mk_err bnt_us
651ret 1
652end
653n = ulong 10000
654seed = ulong 1
655if ge c 4
656n = mk_argnum 3
657end
658if ge c 5
659seed = mk_argnum 4
660end
661if ne get mk_g MKG_BAD 0
662mk_err bnt_us
663ret 1
664end
665if not bn_args
666mk_err bnt_us
667ret 1
668end
669r = bn_mkdir
670if ne r 0
671ret r
672end
673r = bn_gen n seed
674if ne r 0
675ret r
676end
677if eq bn_setup 0
678bn_list
679bn_stats
680end
681ret bn_end
682end