Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/uu/base64/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ uucore = { workspace = true, features = ["encoding"] }
uu_base32 = { workspace = true }
fluent = { workspace = true }

[dev-dependencies]
divan = { workspace = true }
tempfile = { workspace = true }
uucore = { workspace = true, features = ["benchmark"] }

[[bin]]
name = "base64"
path = "src/main.rs"

[[bench]]
name = "base64_bench"
harness = false
72 changes: 72 additions & 0 deletions src/uu/base64/benches/base64_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use divan::{Bencher, black_box};
use std::ffi::OsString;
use uu_base64::uumain;
use uucore::benchmark::{create_test_file, run_util_function, text_data};

fn create_tmp_file(size_mb: usize) -> String {
let temp_dir = tempfile::tempdir().unwrap();
let data = text_data::generate_by_size(size_mb, 80);
let file_path = create_test_file(&data, temp_dir.path());
String::from(file_path.to_str().unwrap())
}

/// Benchmark for base64 encoding
#[divan::bench()]
fn b64_encode_synthetic(bencher: Bencher) {
let file_path_str = &create_tmp_file(5_000);

bencher.bench(|| {
black_box(run_util_function(uumain, &[file_path_str]));
});
}

// Benchmark for base64 decoding
#[divan::bench()]
fn b64_decode_synthetic(bencher: Bencher) {
let temp_dir = tempfile::tempdir().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look like it is duplicated code with the two other tests ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this, I see wc_bench is also the same... but I can make a function that at least takes away the first part

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could also improve wc_bench then :)

let file_path_str = &create_tmp_file(5_000);
let in_file = create_test_file(b"", temp_dir.path());
let in_file_str = in_file.to_str().unwrap();
uumain(
[
OsString::from(file_path_str),
OsString::from(format!(">{in_file_str}")),
]
.iter()
.map(|x| (*x).clone()),
);

bencher.bench(|| {
black_box(run_util_function(uumain, &["-d", in_file_str]));
});
}

// Benchmark different file sizes for base64 decoding ignoring garbage characters
#[divan::bench()]
fn b64_decode_ignore_garbage_synthetic(bencher: Bencher) {
let temp_dir = tempfile::tempdir().unwrap();
let file_path_str = &create_tmp_file(5_000);
let in_file = create_test_file(b"", temp_dir.path());
let in_file_str = in_file.to_str().unwrap();
uumain(
[
OsString::from(file_path_str),
OsString::from(format!(">{in_file_str}")),
]
.iter()
.map(|x| (*x).clone()),
);

bencher.bench(|| {
black_box(run_util_function(uumain, &["-d", "-i", in_file_str]));
});
}

fn main() {
divan::main();
}
Loading