Skip to content

Commit 758c801

Browse files
committed
build: add bench
1 parent 17ebe2c commit 758c801

File tree

8 files changed

+171
-87
lines changed

8 files changed

+171
-87
lines changed

crates/examples/bench/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name = "example-bench"
3+
version = "0.0.0"
4+
edition.workspace = true
5+
6+
[dependencies]
7+
tracing = "0.1.40"
8+
tracing-subscriber = "0.3.18"
9+
10+
[[bin]]
11+
name = "kill_tree_windows"
12+
path = "src/kill_tree_windows.rs"
13+
14+
[[bin]]
15+
name = "kill_tree_linux"
16+
path = "src/kill_tree_linux.rs"
17+
18+
[[bin]]
19+
name = "kill_tree_macos"
20+
path = "src/kill_tree_macos.rs"
21+
22+
[[bin]]
23+
name = "taskkill"
24+
path = "src/taskkill.rs"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use example_bench::{init_log, run};
2+
use std::process::Command;
3+
4+
static EXE_PATH: &str = "target/release/kill_tree_cli";
5+
6+
fn main() {
7+
init_log();
8+
let output = run(200, |target_process_id| {
9+
let mut command = Command::new(EXE_PATH);
10+
command.arg(target_process_id.to_string());
11+
command
12+
});
13+
println!(
14+
"platform: {}, arch: {}, exe: {EXE_PATH}, count: {}, total_ms: {}, average_ms: {}",
15+
std::env::consts::OS,
16+
std::env::consts::ARCH,
17+
output.count,
18+
output.total_ms,
19+
output.average_ms
20+
);
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use example_bench::{init_log, run};
2+
use std::process::Command;
3+
4+
static EXE_PATH: &str = "target/release/kill_tree_cli";
5+
6+
fn main() {
7+
init_log();
8+
let output = run(200, |target_process_id| {
9+
let mut command = Command::new(EXE_PATH);
10+
command.arg(target_process_id.to_string());
11+
command
12+
});
13+
println!(
14+
"platform: {}, arch: {}, exe: {EXE_PATH}, count: {}, total_ms: {}, average_ms: {}",
15+
std::env::consts::OS,
16+
std::env::consts::ARCH,
17+
output.count,
18+
output.total_ms,
19+
output.average_ms
20+
);
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use example_bench::{init_log, run};
2+
use std::process::Command;
3+
4+
static EXE_PATH: &str = "target/release/kill_tree_cli.exe";
5+
6+
fn main() {
7+
init_log();
8+
let output = run(200, |target_process_id| {
9+
let mut command = Command::new(EXE_PATH);
10+
command.arg(target_process_id.to_string());
11+
command
12+
});
13+
println!(
14+
"platform: {}, arch: {}, exe: {EXE_PATH}, count: {}, total_ms: {}, average_ms: {}",
15+
std::env::consts::OS,
16+
std::env::consts::ARCH,
17+
output.count,
18+
output.total_ms,
19+
output.average_ms
20+
);
21+
}

crates/examples/bench/src/lib.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::{process::Command, sync::mpsc, thread};
2+
use tracing::{subscriber, Level};
3+
use tracing_subscriber::FmtSubscriber;
4+
5+
pub fn init_log() {
6+
subscriber::set_global_default(
7+
FmtSubscriber::builder()
8+
.with_max_level(Level::TRACE)
9+
.finish(),
10+
)
11+
.expect("setting default subscriber failed");
12+
}
13+
14+
pub struct Unit {
15+
pub handle: thread::JoinHandle<()>,
16+
pub target_process_id: u32,
17+
}
18+
19+
pub struct Output {
20+
pub count: u32,
21+
pub total_ms: u128,
22+
pub average_ms: u128,
23+
}
24+
25+
pub fn run(count: u32, command_builder: fn(u32) -> Command) -> Output {
26+
let mut units = Vec::with_capacity(count as usize);
27+
for _ in 0..count {
28+
let (tx, rx) = mpsc::channel();
29+
let handle = thread::spawn(move || {
30+
let mut child = Command::new("node")
31+
.arg("-e")
32+
.arg("setInterval(() => {}, 1000)")
33+
.spawn()
34+
.unwrap();
35+
let target_process_id = child.id();
36+
tx.send(target_process_id).unwrap();
37+
let _ = child.wait();
38+
});
39+
let target_process_id = rx.recv().unwrap();
40+
units.push(Unit {
41+
handle,
42+
target_process_id,
43+
});
44+
}
45+
let start = std::time::Instant::now();
46+
for unit in &units {
47+
let output = command_builder(unit.target_process_id)
48+
.output()
49+
.expect("failed to execute process");
50+
assert!(output.status.success());
51+
}
52+
let total = start.elapsed();
53+
let average = total / count;
54+
for unit in units {
55+
let _ = unit.handle.join();
56+
}
57+
Output {
58+
count,
59+
total_ms: total.as_millis(),
60+
average_ms: average.as_millis(),
61+
}
62+
}

crates/examples/bench/src/taskkill.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use example_bench::{init_log, run};
2+
use std::process::Command;
3+
4+
static EXE_PATH: &str = "taskkill.exe";
5+
6+
fn main() {
7+
init_log();
8+
let output = run(200, |target_process_id| {
9+
let mut command = Command::new(EXE_PATH);
10+
command.arg("/F").arg("/T").arg("/PID");
11+
command.arg(target_process_id.to_string());
12+
command
13+
});
14+
println!(
15+
"platform: {}, arch: {}, exe: {EXE_PATH}, count: {}, total_ms: {}, average_ms: {}",
16+
std::env::consts::OS,
17+
std::env::consts::ARCH,
18+
output.count,
19+
output.total_ms,
20+
output.average_ms
21+
);
22+
}

tests/resources/bench/server.mjs

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/resources/bench/test.mjs

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)