Skip to content

Commit daa74e7

Browse files
authored
Merge pull request #8870 from sylvestre/more-bench
More bench
2 parents 97093b4 + b891c85 commit daa74e7

File tree

13 files changed

+757
-102
lines changed

13 files changed

+757
-102
lines changed

Cargo.lock

Lines changed: 113 additions & 102 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/cut/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ memchr = { workspace = true }
2424
bstr = { workspace = true }
2525
fluent = { workspace = true }
2626

27+
[dev-dependencies]
28+
divan = { workspace = true }
29+
tempfile = { workspace = true }
30+
uucore = { workspace = true, features = ["benchmark"] }
31+
2732
[[bin]]
2833
name = "cut"
2934
path = "src/main.rs"
35+
36+
[[bench]]
37+
name = "cut_bench"
38+
harness = false

src/uu/cut/benches/cut_bench.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// This file is part of the uutils coreutils package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
use divan::{Bencher, black_box};
7+
use uu_cut::uumain;
8+
use uucore::benchmark::{run_util_function, setup_test_file, text_data};
9+
10+
/// Benchmark cutting specific byte ranges
11+
#[divan::bench]
12+
fn cut_bytes(bencher: Bencher) {
13+
let data = text_data::generate_by_lines(100_000, 80);
14+
let file_path = setup_test_file(&data);
15+
16+
bencher.bench(|| {
17+
black_box(run_util_function(
18+
uumain,
19+
&["-b", "1-20", file_path.to_str().unwrap()],
20+
));
21+
});
22+
}
23+
24+
/// Benchmark cutting specific character ranges
25+
#[divan::bench]
26+
fn cut_characters(bencher: Bencher) {
27+
let data = text_data::generate_mixed_data(100_000);
28+
let file_path = setup_test_file(&data);
29+
30+
bencher.bench(|| {
31+
black_box(run_util_function(
32+
uumain,
33+
&["-c", "5-30", file_path.to_str().unwrap()],
34+
));
35+
});
36+
}
37+
38+
/// Benchmark cutting fields with tab delimiter
39+
#[divan::bench]
40+
fn cut_fields_tab(bencher: Bencher) {
41+
let mut data = Vec::new();
42+
for i in 0..100_000 {
43+
let line = format!("field1\tfield2_{i}\tfield3\tfield4\tfield5\n");
44+
data.extend_from_slice(line.as_bytes());
45+
}
46+
let file_path = setup_test_file(&data);
47+
48+
bencher.bench(|| {
49+
black_box(run_util_function(
50+
uumain,
51+
&["-f", "2,4", file_path.to_str().unwrap()],
52+
));
53+
});
54+
}
55+
56+
/// Benchmark cutting fields with custom delimiter
57+
#[divan::bench]
58+
fn cut_fields_custom_delim(bencher: Bencher) {
59+
let mut data = Vec::new();
60+
for i in 0..100_000 {
61+
let line = format!("apple,banana_{i},cherry,date,elderberry\n");
62+
data.extend_from_slice(line.as_bytes());
63+
}
64+
let file_path = setup_test_file(&data);
65+
66+
bencher.bench(|| {
67+
black_box(run_util_function(
68+
uumain,
69+
&["-d", ",", "-f", "1,3,5", file_path.to_str().unwrap()],
70+
));
71+
});
72+
}
73+
74+
fn main() {
75+
divan::main();
76+
}

src/uu/hashsum/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@ fluent = { workspace = true }
2525
[[bin]]
2626
name = "hashsum"
2727
path = "src/main.rs"
28+
29+
[dev-dependencies]
30+
divan = { workspace = true }
31+
tempfile = { workspace = true }
32+
uucore = { workspace = true, features = ["benchmark"] }
33+
34+
[[bench]]
35+
name = "hashsum_bench"
36+
harness = false
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// This file is part of the uutils coreutils package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
use divan::{Bencher, black_box};
7+
use std::io::Write;
8+
use tempfile::NamedTempFile;
9+
use uu_hashsum::uumain;
10+
use uucore::benchmark::{run_util_function, setup_test_file, text_data};
11+
12+
/// Benchmark MD5 hashing
13+
#[divan::bench]
14+
fn hashsum_md5(bencher: Bencher) {
15+
let data = text_data::generate_by_size(10, 80);
16+
let file_path = setup_test_file(&data);
17+
18+
bencher.bench(|| {
19+
black_box(run_util_function(
20+
uumain,
21+
&["--md5", file_path.to_str().unwrap()],
22+
));
23+
});
24+
}
25+
26+
/// Benchmark SHA1 hashing
27+
#[divan::bench]
28+
fn hashsum_sha1(bencher: Bencher) {
29+
let data = text_data::generate_by_size(10, 80);
30+
let file_path = setup_test_file(&data);
31+
32+
bencher.bench(|| {
33+
black_box(run_util_function(
34+
uumain,
35+
&["--sha1", file_path.to_str().unwrap()],
36+
));
37+
});
38+
}
39+
40+
/// Benchmark SHA256 hashing
41+
#[divan::bench]
42+
fn hashsum_sha256(bencher: Bencher) {
43+
let data = text_data::generate_by_size(10, 80);
44+
let file_path = setup_test_file(&data);
45+
46+
bencher.bench(|| {
47+
black_box(run_util_function(
48+
uumain,
49+
&["--sha256", file_path.to_str().unwrap()],
50+
));
51+
});
52+
}
53+
54+
/// Benchmark SHA512 hashing
55+
#[divan::bench]
56+
fn hashsum_sha512(bencher: Bencher) {
57+
let data = text_data::generate_by_size(10, 80);
58+
let file_path = setup_test_file(&data);
59+
60+
bencher.bench(|| {
61+
black_box(run_util_function(
62+
uumain,
63+
&["--sha512", file_path.to_str().unwrap()],
64+
));
65+
});
66+
}
67+
68+
/// Benchmark MD5 checksum verification
69+
#[divan::bench]
70+
fn hashsum_md5_check(bencher: Bencher) {
71+
bencher
72+
.with_inputs(|| {
73+
// Create test file
74+
let data = text_data::generate_by_size(10, 80);
75+
let test_file = setup_test_file(&data);
76+
77+
// Create checksum file - keep it alive by returning it
78+
let checksum_file = NamedTempFile::new().unwrap();
79+
let checksum_path = checksum_file.path().to_str().unwrap().to_string();
80+
81+
// Write checksum content
82+
{
83+
let mut file = std::fs::File::create(&checksum_path).unwrap();
84+
writeln!(
85+
file,
86+
"d41d8cd98f00b204e9800998ecf8427e {}",
87+
test_file.to_str().unwrap()
88+
)
89+
.unwrap();
90+
}
91+
92+
(checksum_file, checksum_path)
93+
})
94+
.bench_values(|(_checksum_file, checksum_path)| {
95+
black_box(run_util_function(
96+
uumain,
97+
&["--md5", "--check", &checksum_path],
98+
));
99+
});
100+
}
101+
102+
/// Benchmark SHA256 checksum verification
103+
#[divan::bench]
104+
fn hashsum_sha256_check(bencher: Bencher) {
105+
bencher
106+
.with_inputs(|| {
107+
// Create test file
108+
let data = text_data::generate_by_size(10, 80);
109+
let test_file = setup_test_file(&data);
110+
111+
// Create checksum file - keep it alive by returning it
112+
let checksum_file = NamedTempFile::new().unwrap();
113+
let checksum_path = checksum_file.path().to_str().unwrap().to_string();
114+
115+
// Write checksum content
116+
{
117+
let mut file = std::fs::File::create(&checksum_path).unwrap();
118+
writeln!(
119+
file,
120+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 {}",
121+
test_file.to_str().unwrap()
122+
)
123+
.unwrap();
124+
}
125+
126+
(checksum_file, checksum_path)
127+
})
128+
.bench_values(|(_checksum_file, checksum_path)| {
129+
black_box(run_util_function(
130+
uumain,
131+
&["--sha256", "--check", &checksum_path],
132+
));
133+
});
134+
}
135+
136+
fn main() {
137+
divan::main();
138+
}

src/uu/mv/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ selinux = ["uucore/selinux"]
4747
[[bin]]
4848
name = "mv"
4949
path = "src/main.rs"
50+
51+
[dev-dependencies]
52+
divan = { workspace = true }
53+
tempfile = { workspace = true }
54+
uucore = { workspace = true, features = ["benchmark"] }
55+
56+
[[bench]]
57+
name = "mv_bench"
58+
harness = false

src/uu/mv/benches/mv_bench.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// This file is part of the uutils coreutils package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
use divan::{Bencher, black_box};
7+
use tempfile::TempDir;
8+
use uu_mv::uumain;
9+
use uucore::benchmark::{fs_tree, run_util_function};
10+
11+
/// Benchmark moving a single file (repeated to reach 100ms)
12+
#[divan::bench]
13+
fn mv_single_file(bencher: Bencher) {
14+
bencher
15+
.with_inputs(|| {
16+
let temp_dir = TempDir::new().unwrap();
17+
fs_tree::create_wide_tree(temp_dir.path(), 1000, 0);
18+
let files: Vec<(String, String)> = (0..1000)
19+
.map(|i| {
20+
let src = temp_dir.path().join(format!("f{i}"));
21+
let dst = temp_dir.path().join(format!("moved_{i}"));
22+
(
23+
src.to_str().unwrap().to_string(),
24+
dst.to_str().unwrap().to_string(),
25+
)
26+
})
27+
.collect();
28+
(temp_dir, files)
29+
})
30+
.bench_values(|(temp_dir, files)| {
31+
for (src, dst) in &files {
32+
black_box(run_util_function(uumain, &[src, dst]));
33+
}
34+
drop(temp_dir);
35+
});
36+
}
37+
38+
/// Benchmark moving multiple files to directory
39+
#[divan::bench]
40+
fn mv_multiple_to_dir(bencher: Bencher) {
41+
bencher
42+
.with_inputs(|| {
43+
let temp_dir = TempDir::new().unwrap();
44+
fs_tree::create_wide_tree(temp_dir.path(), 1000, 0);
45+
let dest_dir = temp_dir.path().join("dest");
46+
std::fs::create_dir(&dest_dir).unwrap();
47+
48+
let mut args: Vec<String> = (0..1000)
49+
.map(|i| {
50+
temp_dir
51+
.path()
52+
.join(format!("f{i}"))
53+
.to_str()
54+
.unwrap()
55+
.to_string()
56+
})
57+
.collect();
58+
args.push(dest_dir.to_str().unwrap().to_string());
59+
(temp_dir, args)
60+
})
61+
.bench_values(|(temp_dir, args)| {
62+
let arg_refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
63+
black_box(run_util_function(uumain, &arg_refs));
64+
drop(temp_dir);
65+
});
66+
}
67+
68+
/// Benchmark moving directory recursively
69+
#[divan::bench]
70+
fn mv_directory(bencher: Bencher) {
71+
bencher
72+
.with_inputs(|| {
73+
let temp_dir = TempDir::new().unwrap();
74+
let src_dir = temp_dir.path().join("src_tree");
75+
std::fs::create_dir(&src_dir).unwrap();
76+
// Increase tree size for longer benchmark
77+
fs_tree::create_balanced_tree(&src_dir, 5, 5, 10);
78+
let dst_dir = temp_dir.path().join("dest_tree");
79+
(
80+
temp_dir,
81+
src_dir.to_str().unwrap().to_string(),
82+
dst_dir.to_str().unwrap().to_string(),
83+
)
84+
})
85+
.bench_values(|(temp_dir, src, dst)| {
86+
black_box(run_util_function(uumain, &[&src, &dst]));
87+
drop(temp_dir);
88+
});
89+
}
90+
91+
/// Benchmark force overwrite
92+
#[divan::bench]
93+
fn mv_force_overwrite(bencher: Bencher) {
94+
bencher
95+
.with_inputs(|| {
96+
let temp_dir = TempDir::new().unwrap();
97+
fs_tree::create_wide_tree(temp_dir.path(), 2000, 0);
98+
let files: Vec<(String, String)> = (0..1000)
99+
.map(|i| {
100+
let src = temp_dir.path().join(format!("f{i}"));
101+
let dst = temp_dir.path().join(format!("f{}", i + 1000));
102+
(
103+
src.to_str().unwrap().to_string(),
104+
dst.to_str().unwrap().to_string(),
105+
)
106+
})
107+
.collect();
108+
(temp_dir, files)
109+
})
110+
.bench_values(|(temp_dir, files)| {
111+
for (src, dst) in &files {
112+
black_box(run_util_function(uumain, &["-f", src, dst]));
113+
}
114+
drop(temp_dir);
115+
});
116+
}
117+
118+
fn main() {
119+
divan::main();
120+
}

src/uu/rm/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,12 @@ windows-sys = { workspace = true, features = ["Win32_Storage_FileSystem"] }
3232
[[bin]]
3333
name = "rm"
3434
path = "src/main.rs"
35+
36+
[dev-dependencies]
37+
divan = { workspace = true }
38+
tempfile = { workspace = true }
39+
uucore = { workspace = true, features = ["benchmark"] }
40+
41+
[[bench]]
42+
name = "rm_bench"
43+
harness = false

0 commit comments

Comments
 (0)