Skip to content

Commit 26240c2

Browse files
committed
refactor(bench): simplify timeout benchmark by removing child process logic
Replace complex child mode spawning with direct command arguments for cleaner and more maintainable benchmarks, reducing overhead and improving readability. Use "true" for quick-exit and "sleep" for enforced timeout tests.
1 parent 98d6a10 commit 26240c2

File tree

1 file changed

+4
-75
lines changed

1 file changed

+4
-75
lines changed

src/uu/timeout/benches/timeout_bench.rs

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,6 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use std::env;
7-
use std::process;
8-
use std::time::Duration;
9-
10-
const CHILD_FLAG: &str = "--timeout-bench-child";
11-
12-
fn maybe_run_child_mode() {
13-
let mut args = env::args();
14-
let _ = args.next(); // skip executable path
15-
16-
while let Some(arg) = args.next() {
17-
if arg == CHILD_FLAG {
18-
let mode = args
19-
.next()
20-
.unwrap_or_else(|| panic!("missing child mode after {CHILD_FLAG}"));
21-
run_child(mode);
22-
}
23-
}
24-
}
25-
26-
#[cfg(unix)]
27-
fn run_child(mode: String) -> ! {
28-
match mode.as_str() {
29-
"quick-exit" => process::exit(0),
30-
"short-sleep" => {
31-
std::thread::sleep(Duration::from_millis(5));
32-
process::exit(0);
33-
}
34-
"long-sleep" => {
35-
std::thread::sleep(Duration::from_millis(200));
36-
process::exit(0);
37-
}
38-
"ignore-term" => {
39-
use nix::sys::signal::{SigHandler, Signal, signal};
40-
41-
unsafe {
42-
signal(Signal::SIGTERM, SigHandler::SigIgn)
43-
.expect("failed to ignore SIGTERM in bench child");
44-
}
45-
46-
loop {
47-
std::thread::sleep(Duration::from_millis(100));
48-
}
49-
}
50-
other => {
51-
eprintln!("unknown child mode: {other}");
52-
process::exit(1);
53-
}
54-
}
55-
}
56-
57-
#[cfg(not(unix))]
58-
fn run_child(_: String) -> ! {
59-
// The timeout benchmarks are Unix-only, but ensure child invocations still terminate.
60-
process::exit(0);
61-
}
62-
636
#[cfg(unix)]
647
use divan::{Bencher, black_box};
658
#[cfg(unix)]
@@ -68,41 +11,27 @@ use uu_timeout::uumain;
6811
use uucore::benchmark::run_util_function;
6912

7013
#[cfg(unix)]
71-
fn bench_timeout_with_mode(bencher: Bencher, args: &[&str], child_mode: &str) {
72-
let child_path = env::current_exe()
73-
.expect("failed to locate timeout bench executable")
74-
.into_os_string()
75-
.into_string()
76-
.expect("bench executable path must be valid UTF-8");
77-
78-
let mut owned_args: Vec<String> = args.iter().map(|s| (*s).to_string()).collect();
79-
owned_args.push(child_path);
80-
owned_args.push(CHILD_FLAG.into());
81-
owned_args.push(child_mode.to_string());
82-
83-
let arg_refs: Vec<&str> = owned_args.iter().map(|s| s.as_str()).collect();
84-
14+
fn bench_timeout(bencher: Bencher, args: &[&str]) {
8515
bencher.bench(|| {
86-
black_box(run_util_function(uumain, &arg_refs));
16+
black_box(run_util_function(uumain, args));
8717
});
8818
}
8919

9020
/// Benchmark the fast path where the command exits immediately.
9121
#[cfg(unix)]
9222
#[divan::bench]
9323
fn timeout_quick_exit(bencher: Bencher) {
94-
bench_timeout_with_mode(bencher, &["0.02"], "quick-exit");
24+
bench_timeout(bencher, &["0.02", "true"]);
9525
}
9626

9727
/// Benchmark a command that runs longer than the threshold and receives the default signal.
9828
#[cfg(unix)]
9929
#[divan::bench]
10030
fn timeout_enforced(bencher: Bencher) {
101-
bench_timeout_with_mode(bencher, &["0.02"], "long-sleep");
31+
bench_timeout(bencher, &["0.02", "sleep", "0.2"]);
10232
}
10333

10434
fn main() {
105-
maybe_run_child_mode();
10635
#[cfg(unix)]
10736
divan::main();
10837
}

0 commit comments

Comments
 (0)