mirror of
https://github.com/Momoko-Ayase/Senbei.git
synced 2026-11-24 02:52:47 +08:00
feat(cli): scope worker limits and benchmark deterministic runs
This commit is contained in:
1 parent
dfbbef5226
commit
0182eff772
6 files changed
+412
-15
No files matched your search
+60
-2
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user