From d3606d51a72994240323bd2af0fb8226eee07348 Mon Sep 17 00:00:00 2001 From: ruv Date: Fri, 12 Jun 2026 08:01:29 -0400 Subject: [PATCH] bench(wasm-edge): host process_frame latency benches (ADR-163 T1) Criterion benches over the M6-audit-named heaviest hot paths: exo_time_crystal 256x128 autocorrelation, exo_ghost_hunter periodicity, sec_weapon_detect per-subcarrier Welford, med_seizure_detect clonic rhythm (medical-experimental-gated). Drives each through the public process_frame on a fixed synthetic CSI frame after warming the relevant buffers. Crate is workspace-excluded: run from the crate dir with --features std. Set lib bench=false so libtest does not intercept criterion CLI flags. HOST-MEASURED medians (Intel Core Ultra 9 285H, native --release), NOT the ESP32/WASM3 doc budget (that needs hardware): time_crystal 17.3us, ghost_hunter 1.44us, weapon 0.42us, seizure 0.10us. Closes the ADR-160 deferred 'criterion benches for process_frame budget claims' item on host. No production-code behavior change. Co-Authored-By: claude-flow --- v2/crates/wifi-densepose-wasm-edge/Cargo.lock | 577 ++++++++++++++++++ v2/crates/wifi-densepose-wasm-edge/Cargo.toml | 19 + .../benches/process_frame_bench.rs | 259 ++++++++ 3 files changed, 855 insertions(+) create mode 100644 v2/crates/wifi-densepose-wasm-edge/benches/process_frame_bench.rs diff --git a/v2/crates/wifi-densepose-wasm-edge/Cargo.lock b/v2/crates/wifi-densepose-wasm-edge/Cargo.lock index a3f74aa3..77d7ccc0 100644 --- a/v2/crates/wifi-densepose-wasm-edge/Cargo.lock +++ b/v2/crates/wifi-densepose-wasm-edge/Cargo.lock @@ -2,6 +2,33 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + [[package]] name = "block-buffer" version = "0.10.4" @@ -11,12 +38,76 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cfg-if" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + +[[package]] +name = "clap" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstyle", + "clap_lex", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + [[package]] name = "cpufeatures" version = "0.2.17" @@ -26,6 +117,73 @@ dependencies = [ "libc", ] +[[package]] +name = "criterion" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" +dependencies = [ + "anes", + "cast", + "ciborium", + "clap", + "criterion-plot", + "is-terminal", + "itertools", + "num-traits", + "once_cell", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +dependencies = [ + "cast", + "itertools", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + [[package]] name = "crypto-common" version = "0.1.7" @@ -46,6 +204,36 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "either" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e" + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -56,6 +244,60 @@ dependencies = [ "version_check", ] +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "zerocopy", +] + +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + +[[package]] +name = "is-terminal" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "js-sys" +version = "0.3.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2025f20d7a4fa7785846e7b63d10a76d3f1cee98ee5cb79ea59703f95e42162" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + [[package]] name = "libc" version = "0.2.182" @@ -68,6 +310,192 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" +[[package]] +name = "memchr" +version = "2.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88904434abc2901f197fe8cc55f0445e7ded921dba5911dad2e2b39b48e663c4" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "oorandom" +version = "11.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "plotters" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" + +[[package]] +name = "plotters-svg" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" +dependencies = [ + "plotters-backend", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "regex" +version = "1.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1292b7759ae1cb9ec195452d1390a074f0cd8541ab7a5a8c31cd6db45d4a6ba" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + [[package]] name = "sha2" version = "0.10.9" @@ -79,22 +507,171 @@ dependencies = [ "digest", ] +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "typenum" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + [[package]] name = "version_check" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a254a4b10c19a76f09a27640e7ffbf9bc30bf67e16a3bf28aaefa4920fe81563" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24a40fc75b0ec6f3746ceb10d36f53a93dcd68a93b11b6445983945d79eba0dc" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "908f34bd9b9ce3d4caf07b72dfab63d61504d156856c6bd3cd87fa350cf3985b" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7acbf7616c27b194bbb550bf77ed0c2c3e5b7fd1260a93082b95fb7f47959b92" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e0871acf327f283dc6da28a1696cdc64fb355ba9f935d052021fa77f35cce69" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "wifi-densepose-wasm-edge" version = "0.3.0" dependencies = [ + "criterion", "libm", "sha2", ] + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "zerocopy" +version = "0.8.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce1022995ff5ff5d841ad7d994facc23098cd40152f2c1d11cd607c6f530653f" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ae7f38b72ec2a254e2b87ef277cf2cd4fb97cbebf944faa6f33354da0867930" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/v2/crates/wifi-densepose-wasm-edge/Cargo.toml b/v2/crates/wifi-densepose-wasm-edge/Cargo.toml index 2dee4614..f4e7a7b0 100644 --- a/v2/crates/wifi-densepose-wasm-edge/Cargo.toml +++ b/v2/crates/wifi-densepose-wasm-edge/Cargo.toml @@ -11,6 +11,20 @@ categories = ["embedded", "wasm", "science"] [lib] crate-type = ["cdylib", "rlib"] +# The lib's libtest harness does not understand criterion CLI flags +# (`--warm-up-time` etc.), so exclude it from `cargo bench` — only the criterion +# bench target below should receive bench args (ADR-163). +bench = false + +# ADR-163: host-measured process_frame latency benches (closes the ADR-160 +# "criterion benches for process_frame budget claims" deferred item — HOST only; +# the ESP32-S3 WASM3 budget remains unmeasured, see the bench header). +# `std` is required (criterion is a host crate); the crate is workspace-EXCLUDED +# so run from the crate dir: `cargo bench --features std`. +[[bench]] +name = "process_frame_bench" +harness = false +required-features = ["std"] [dependencies] # no_std math @@ -18,6 +32,11 @@ libm = "0.2" # SHA-256 for RVF build hash (optional, used by builder) sha2 = { version = "0.10", optional = true, default-features = false } +[dev-dependencies] +# Host-only latency regression benches (ADR-163). Pinned to match the rest of +# the workspace's bench crates. +criterion = { version = "0.5", features = ["html_reports"] } + [features] default = ["default-pipeline"] # Enable std for testing on host + RVF builder diff --git a/v2/crates/wifi-densepose-wasm-edge/benches/process_frame_bench.rs b/v2/crates/wifi-densepose-wasm-edge/benches/process_frame_bench.rs new file mode 100644 index 00000000..1d15781f --- /dev/null +++ b/v2/crates/wifi-densepose-wasm-edge/benches/process_frame_bench.rs @@ -0,0 +1,259 @@ +//! Criterion benches for the heaviest `process_frame` hot paths in the edge +//! skill library (ADR-163, closing the ADR-160 §"Deferred Backlog" item +//! "Criterion benches for process_frame budget claims"). +//! +//! ## HONEST SCOPE — read this before citing any number here +//! +//! These benches measure **HOST** wall-clock latency on a development laptop. +//! The per-module doc budgets (e.g. `exo_time_crystal` "H (heavy, <10ms) on +//! ESP32-S3 WASM3") are **for a different target**: an Xtensa ESP32-S3 running +//! the WASM3 interpreter. A native x86_64 host with `-O` is an **upper-bound +//! proxy for the ALGORITHM cost only**; it is NOT the ESP32 number and does NOT +//! reproduce the ESP32 budget. WASM3 interpretation on a ~240 MHz Xtensa core is +//! typically 1-2 orders of magnitude slower than native host code, so a host +//! median well under the budget does NOT prove the ESP32 meets it — it only +//! bounds the work. The ESP32 figure remains UNMEASURED (needs hardware). +//! +//! What these benches DO prove (MEASURED-on-host): +//! * the hot paths run, on a fixed synthetic CSI frame, with a real median; +//! * a regression guard exists so a future change that 10×'s the host cost +//! is caught in CI/dev even before anyone reflashes an ESP32. +//! +//! Run (the crate is EXCLUDED from the v2 workspace — bench from the crate dir): +//! cd v2/crates/wifi-densepose-wasm-edge +//! cargo bench --features std +//! # quick smoke: +//! cargo bench --features std -- --warm-up-time 1 --measurement-time 2 +//! +//! `med_seizure_detect` is gated behind `medical-experimental`; its bench is +//! `#[cfg(feature = "medical-experimental")]` and only runs when that feature is +//! also enabled: +//! cargo bench --features std,medical-experimental + +use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; +use std::hint::black_box; + +use wifi_densepose_wasm_edge::exo_ghost_hunter::GhostHunterDetector; +use wifi_densepose_wasm_edge::exo_time_crystal::TimeCrystalDetector; +use wifi_densepose_wasm_edge::sec_weapon_detect::WeaponDetector; + +// ── Fixed synthetic CSI fixtures (deterministic LCG, seed-stable) ──────────── + +/// Deterministic pseudo-random in [lo, hi) from a 32-bit LCG, matching the +/// generator style used by `tests/budget_compliance.rs`. +fn lcg(seed: &mut u32) -> f32 { + *seed = seed.wrapping_mul(1103515245).wrapping_add(12345); + (*seed >> 16) as f32 / 32768.0 +} + +fn synthetic_phases(n: usize, seed: u32) -> Vec { + let mut s = seed; + (0..n).map(|_| lcg(&mut s) * 6.2832 - 3.1416).collect() +} + +fn synthetic_amplitudes(n: usize, seed: u32) -> Vec { + let mut s = seed; + (0..n).map(|_| lcg(&mut s) * 10.0 + 0.1).collect() +} + +fn synthetic_variance(n: usize, seed: u32) -> Vec { + let mut s = seed; + (0..n).map(|_| lcg(&mut s) * 2.0 + 0.05).collect() +} + +const N_SC: usize = 32; // per-subcarrier width (matches both modules' MAX_SC) + +// ── exo_time_crystal: compute_autocorrelation 256×128 hot path ─────────────── +// +// `compute_autocorrelation` is private, so we drive it through the public +// `process_frame`. To hit the full 256-point × 128-lag autocorrelation the +// circular buffer must be FULL (≥256 samples) and the signal must be +// non-constant (the module early-outs on `buf_var < 1e-8`). We pre-fill once +// with a periodic-plus-noise motion-energy stream, then bench a single +// `process_frame` (each call recomputes the full 256×128 autocorrelation = +// ~32K multiply-accumulates, the M6-audit-named hot path). + +fn prefilled_time_crystal() -> TimeCrystalDetector { + let mut d = TimeCrystalDetector::new(); + let mut s = 0xC0FFEEu32; + // 300 frames (> BUF_LEN=256) so the buffer is full and statistics are warm. + for i in 0..300 { + // period-10 square wave + small noise → guarantees buf_var > 0 and a + // genuine autocorrelation structure (the expensive path runs). + let base = if (i % 10) < 5 { 1.0 } else { 0.0 }; + let me = base + lcg(&mut s) * 0.05; + black_box(d.process_frame(black_box(me))); + } + d +} + +fn bench_exo_time_crystal(c: &mut Criterion) { + c.bench_function("exo_time_crystal::process_frame[autocorr_256x128]", |b| { + let mut s = 0x1357_9BDFu32; + b.iter_batched( + prefilled_time_crystal, + |mut d| { + // One frame = one full 256×128 autocorrelation pass. + let me = if (d.frame_count() % 10) < 5 { 1.0 } else { 0.0 } + lcg(&mut s) * 0.05; + black_box(d.process_frame(black_box(me))); + }, + BatchSize::SmallInput, + ); + }); +} + +// ── exo_ghost_hunter: periodicity + hidden-breathing hot path ──────────────── +// +// Heaviest path runs only when the room is reported EMPTY (presence == 0): +// per-group anomaly accumulation + aggregate-phase autocorrelation for hidden +// periodic (breathing) signatures. We warm the noise floor + phase buffer first, +// then bench one empty-room frame. + +fn prefilled_ghost_hunter() -> GhostHunterDetector { + let mut d = GhostHunterDetector::new(); + let mut s = 0xBADC0DEu32; + // Warm the per-group EWMA noise floors + fill the phase buffer (PHASE_BUF_LEN=64) + // with a periodic phase signal so the periodicity autocorrelation has structure. + for i in 0..120u32 { + let phases: Vec = (0..N_SC) + .map(|k| libm::sinf(i as f32 * 0.4 + k as f32 * 0.1) * 0.3 + lcg(&mut s) * 0.02) + .collect(); + let amps = synthetic_amplitudes(N_SC, 4000 + i); + let var = synthetic_variance(N_SC, 4500 + i); + black_box(d.process_frame(&phases, &s, &var, 0, 0.05)); + } + d +} + +fn bench_exo_ghost_hunter(c: &mut Criterion) { + let amps = synthetic_amplitudes(N_SC, 9000); + let var = synthetic_variance(N_SC, 9500); + c.bench_function("exo_ghost_hunter::process_frame[empty_room_periodicity]", |b| { + let mut s = 0x2468_ACE0u32; + b.iter_batched( + prefilled_ghost_hunter, + |mut d| { + let i = d.frame_count(); + let phases: Vec = (0..N_SC) + .map(|k| libm::sinf(i as f32 * 0.4 + k as f32 * 0.1) * 0.3 + lcg(&mut s) * 0.02) + .collect(); + black_box(d.process_frame( + black_box(&phases), + black_box(&s), + black_box(&var), + black_box(0), + black_box(0.05), + )); + }, + BatchSize::SmallInput, + ); + }); +} + +// ── sec_weapon_detect: per-subcarrier Welford hot path ─────────────────────── +// +// After calibration the detector runs a per-subcarrier online Welford update +// over MAX_SC=32 subcarriers each frame (the M6-audit-named hot path). We +// calibrate first (the early frames just accumulate baseline stats), then bench +// one steady-state frame. + +fn calibrated_weapon_detector() -> WeaponDetector { + let mut d = WeaponDetector::new(); + // Drive enough empty-room frames to complete calibration + warm the running + // Welford state. Calibration window is internal; 200 frames is comfortably + // past it for MAX_SC=32. + for i in 0..200u32 { + let phases = synthetic_phases(N_SC, 6000 + i); + let amps = synthetic_amplitudes(N_SC, 6500 + i); + let var = synthetic_variance(N_SC, 7000 + i); + black_box(d.process_frame(&phases, &s, &var, 0.05, 0)); + } + d +} + +fn bench_sec_weapon_detect(c: &mut Criterion) { + c.bench_function("sec_weapon_detect::process_frame[per_sc_welford]", |b| { + let mut seed = 8000u32; + b.iter_batched( + calibrated_weapon_detector, + |mut d| { + seed = seed.wrapping_add(1); + let phases = synthetic_phases(N_SC, seed); + let amps = synthetic_amplitudes(N_SC, seed.wrapping_add(500)); + let var = synthetic_variance(N_SC, seed.wrapping_add(1000)); + black_box(d.process_frame( + black_box(&phases), + black_box(&s), + black_box(&var), + black_box(0.3), + black_box(1), + )); + }, + BatchSize::SmallInput, + ); + }); +} + +// ── med_seizure_detect: detect_rhythm / clonic autocorrelation hot path ────── +// +// Gated behind `medical-experimental` (ADR-160 §A1). The clonic-phase rhythm +// detection autocorrelates the amplitude ring buffer (PHASE_WINDOW=100); we warm +// the buffers with a high-energy rhythmic signal, then bench one frame. +#[cfg(feature = "medical-experimental")] +mod med { + use super::*; + use wifi_densepose_wasm_edge::med_seizure_detect::SeizureDetector; + + fn warmed_seizure_detector() -> SeizureDetector { + let mut d = SeizureDetector::new(); + let mut s = 0x5EE_D00Du32; + // High-energy ~4 Hz rhythmic (period ~5 frames at 20 Hz) → exercises the + // clonic-phase rhythm/autocorrelation path, with presence asserted. + for i in 0..150u32 { + let me = 2.5 + libm::sinf(i as f32 * 1.25) * 1.5; + let amp = 1.0 + lcg(&mut s) * 0.2; + black_box(d.process_frame(0.0, amp, me, 1)); + } + d + } + + pub fn bench_med_seizure_detect(c: &mut Criterion) { + c.bench_function("med_seizure_detect::process_frame[clonic_rhythm]", |b| { + let mut s = 0x9A_BCDE_F0u32; + b.iter_batched( + warmed_seizure_detector, + |mut d| { + let i = d.frame_count(); + let me = 2.5 + libm::sinf(i as f32 * 1.25) * 1.5; + let amp = 1.0 + lcg(&mut s) * 0.2; + black_box(d.process_frame( + black_box(0.0), + black_box(amp), + black_box(me), + black_box(1), + )); + }, + BatchSize::SmallInput, + ); + }); + } +} + +#[cfg(feature = "medical-experimental")] +criterion_group!( + benches, + bench_exo_time_crystal, + bench_exo_ghost_hunter, + bench_sec_weapon_detect, + med::bench_med_seizure_detect, +); + +#[cfg(not(feature = "medical-experimental"))] +criterion_group!( + benches, + bench_exo_time_crystal, + bench_exo_ghost_hunter, + bench_sec_weapon_detect, +); + +criterion_main!(benches);