feat(cli): scope worker limits and benchmark deterministic runs

This commit is contained in:
Momoko-Ayase committed 2026-11-05 23:08:48 +08:00
1 parent dfbbef5226
commit 0182eff772
6 files changed
+412 -15

No files matched your search

+60 -2
View File
@@ -1,5 +1,6 @@
use senbei_engine::android::ResourceLimits;
use senbei_io::{job, pause, scan};
use std::num::NonZeroUsize;
use std::path::Path;
fn main() -> std::process::ExitCode {
@@ -11,7 +12,10 @@ fn main() -> std::process::ExitCode {
return std::process::ExitCode::from(2);
}
};
let code = execute(&mut options);
let code = match options.threads {
Some(count) => senbei_engine::with_thread_cap(count, || execute(&mut options)),
None => execute(&mut options),
};
if !options.machine {
pause::maybe_pause(options.no_pause);
}
@@ -37,6 +41,7 @@ struct Options {
verbose: bool,
scan_all: bool,
strict_integrity: bool,
threads: Option<NonZeroUsize>,
limits: ResourceLimits,
/// Machine-readable output on stdout and nothing else.
machine: bool,
@@ -62,6 +67,7 @@ impl Options {
let mut verbose = false;
let mut scan_all = false;
let mut strict_integrity = false;
let mut threads = None;
let mut machine = false;
let mut dry_run = false;
let mut limits = ResourceLimits::default();
@@ -84,6 +90,7 @@ impl Options {
"--strict-integrity" => strict_integrity = true,
"--json" => machine = true,
"--dry-run" => dry_run = true,
"--threads" => threads = Some(parse_thread_cap(args.next().as_deref())?),
flag
@ ("--max-entry-mib" | "--max-total-mib" | "--max-entries" | "--max-depth") => {
set_resource_limit(&mut limits, flag, args.next().as_deref())?;
@@ -171,6 +178,7 @@ impl Options {
verbose,
scan_all,
strict_integrity,
threads,
limits,
machine,
dry_run,
@@ -178,6 +186,13 @@ impl Options {
}
}
fn parse_thread_cap(value: Option<&str>) -> Result<NonZeroUsize, String> {
value
.filter(|text| !text.is_empty() && text.bytes().all(|byte| byte.is_ascii_digit()))
.and_then(|text| text.parse().ok())
.ok_or_else(|| "--threads requires a positive integer argument".to_owned())
}
/// Run the parsed command. Kept separate from argument parsing and the pause
/// prompt so the execution step can be wrapped on its own.
fn execute(options: &mut Options) -> u8 {
@@ -369,7 +384,7 @@ fn set_resource_limit(
fn print_help() {
println!(
"senbei [inspect|unpack] <file|folder> [--out DIR] [-v|--verbose] [-q|--quiet]... [--json] [--dry-run] [--scan-all] [--strict-integrity] [--max-entry-mib N] [--max-total-mib N] [--max-entries N] [--max-depth N] [--no-log] [--no-pause] [-V|--version] [-h|--help]"
"senbei [inspect|unpack] <file|folder> [--out DIR] [-v|--verbose] [-q|--quiet]... [--json] [--dry-run] [--threads N] [--scan-all] [--strict-integrity] [--max-entry-mib N] [--max-total-mib N] [--max-entries N] [--max-depth N] [--no-log] [--no-pause] [-V|--version] [-h|--help]"
);
println!(
" <file|folder>\n\
@@ -395,6 +410,10 @@ fn print_help() {
\x20 no log. Android extraction still uses its own temporary\n\
\x20 workspace, which is discarded."
);
println!(
" --threads N positive worker cap for this run. Overrides SENBEI_THREADS;\n\
\x20 1 selects the sequential path. The environment is unchanged."
);
println!(
" --scan-all probe selected .exe/.dll/.so/metadata names below the\n\
\x20 size floor; other filenames remain excluded."
@@ -425,6 +444,45 @@ fn print_help() {
);
}
#[cfg(test)]
mod thread_options {
use super::*;
#[test]
fn worker_cap_requires_a_positive_decimal_integer() {
assert_eq!(parse_thread_cap(Some("1")).unwrap().get(), 1);
assert_eq!(parse_thread_cap(Some("4")).unwrap().get(), 4);
for value in [
None,
Some(""),
Some("0"),
Some("-1"),
Some("+1"),
Some("1.5"),
Some("many"),
Some("999999999999999999999999999999"),
] {
assert!(parse_thread_cap(value).is_err(), "{value:?}");
}
}
#[test]
fn caps_are_optional_and_work_with_both_commands() {
for (args, expected) in [
(vec!["x"], None),
(vec!["unpack", "x", "--threads", "1"], Some(1)),
(vec!["--threads", "4", "inspect", "x"], Some(4)),
] {
let Outcome::Run(options) =
Options::parse(args.into_iter().map(str::to_owned)).unwrap()
else {
panic!("expected a parsed command");
};
assert_eq!(options.threads.map(NonZeroUsize::get), expected);
}
}
}
#[cfg(test)]
mod strict_integrity {
use super::exit_code;
+63
View File
@@ -0,0 +1,63 @@
//! CLI worker-cap parsing and composition with report-only inspection.
use std::process::Command;
fn senbei() -> Command {
Command::new(env!("CARGO_BIN_EXE_senbei"))
}
#[test]
fn rejects_missing_zero_negative_and_overflowing_caps() {
for value in [
"",
"0",
"-1",
"+1",
"1.5",
"many",
"999999999999999999999999999999",
] {
let output = senbei()
.args(["--threads", value, "--no-pause"])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(2), "{value:?}");
assert!(String::from_utf8_lossy(&output.stderr).contains("--threads"));
}
assert_eq!(
senbei().arg("--threads").output().unwrap().status.code(),
Some(2)
);
}
#[test]
fn composes_with_inspection_and_machine_output_without_writes() {
let dir = tempfile::tempdir().unwrap();
let input = dir.path().join("plain.bin");
std::fs::write(&input, b"not a protected image").unwrap();
for cap in ["1", "4"] {
let output = senbei()
.env("SENBEI_THREADS", "invalid")
.arg("inspect")
.arg(&input)
.args(["--threads", cap, "--json", "--no-pause"])
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
assert!(output.stderr.is_empty());
let report: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
assert_eq!(report["mode"], "inspect");
assert_eq!(report["format_version"], 1);
}
assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1);
}
#[test]
fn help_describes_the_environment_override() {
let output = senbei().arg("--help").output().unwrap();
assert!(output.status.success());
let help = String::from_utf8(output.stdout).unwrap();
assert!(help.contains("--threads N"));
assert!(help.contains("Overrides SENBEI_THREADS"));
assert!(help.contains("sequential"));
}