Skip to content

Commit 60b9b87

Browse files
author
Andrea Calabrese
committed
Add benchmark for base64
For performance monitoring, added benchmark for base64. Those benchmarks include testing encoding, decoding and decoding ignoring garbage characters. Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com>
1 parent 5c15d79 commit 60b9b87

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

‎Cargo.lock‎

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

‎src/uu/base64/Cargo.toml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ uucore = { workspace = true, features = ["encoding"] }
2323
uu_base32 = { workspace = true }
2424
fluent = { workspace = true }
2525

26+
[dev-dependencies]
27+
divan = { workspace = true }
28+
tempfile = { workspace = true }
29+
uucore = { workspace = true, features = ["benchmark"] }
30+
2631
[[bin]]
2732
name = "base64"
2833
path = "src/main.rs"
34+
35+
[[bench]]
36+
name = "base64_bench"
37+
harness = false
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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::ffi::OsString;
8+
use uu_base64::uumain;
9+
use uucore::benchmark::{create_test_file, run_util_function, text_data};
10+
11+
fn create_tmp_file(size_mb: usize) -> String {
12+
let temp_dir = tempfile::tempdir().unwrap();
13+
let data = text_data::generate_by_size(size_mb, 80);
14+
let file_path = create_test_file(&data, temp_dir.path());
15+
String::from(file_path.to_str().unwrap())
16+
}
17+
18+
/// Benchmark for base64 encoding
19+
#[divan::bench()]
20+
fn b64_encode_synthetic(bencher: Bencher) {
21+
let file_path_str = &create_tmp_file(5_000);
22+
23+
bencher.bench(|| {
24+
black_box(run_util_function(uumain, &[file_path_str]));
25+
});
26+
}
27+
28+
// Benchmark for base64 decoding
29+
#[divan::bench()]
30+
fn b64_decode_synthetic(bencher: Bencher) {
31+
let temp_dir = tempfile::tempdir().unwrap();
32+
let file_path_str = &create_tmp_file(5_000);
33+
let in_file = create_test_file(b"", temp_dir.path());
34+
let in_file_str = in_file.to_str().unwrap();
35+
uumain(
36+
[
37+
OsString::from(file_path_str),
38+
OsString::from(format!(">{in_file_str}")),
39+
]
40+
.iter()
41+
.map(|x| (*x).clone()),
42+
);
43+
44+
bencher.bench(|| {
45+
black_box(run_util_function(uumain, &["-d", in_file_str]));
46+
});
47+
}
48+
49+
// Benchmark different file sizes for base64 decoding ignoring garbage characters
50+
#[divan::bench()]
51+
fn b64_decode_ignore_garbage_synthetic(bencher: Bencher) {
52+
let temp_dir = tempfile::tempdir().unwrap();
53+
let file_path_str = &create_tmp_file(5_000);
54+
let in_file = create_test_file(b"", temp_dir.path());
55+
let in_file_str = in_file.to_str().unwrap();
56+
uumain(
57+
[
58+
OsString::from(file_path_str),
59+
OsString::from(format!(">{in_file_str}")),
60+
]
61+
.iter()
62+
.map(|x| (*x).clone()),
63+
);
64+
65+
bencher.bench(|| {
66+
black_box(run_util_function(uumain, &["-d", "-i", in_file_str]));
67+
});
68+
}
69+
70+
fn main() {
71+
divan::main();
72+
}

0 commit comments

Comments
 (0)