Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 975e18c

Browse files
authored
Fix base64 benchmarks (#9082)
* base64: fix tempdir getting removed * base_common: add -o option to output to file * base64: fix benchmark input files Also pipe benchmark output to /dev/null in order to not spam stdout. * base64: benchmark 50MB files instead of 5GB
1 parent e766645 commit 975e18c

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

docs/src/extensions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Similar to the proc-ps implementation and unlike GNU/Coreutils, `uptime` provide
195195

196196
## `base32/base64/basenc`
197197

198-
Just like on macOS, `base32/base64/basenc` provides `-D` to decode data.
198+
Just like on macOS, `base32/base64/basenc` provides `-D` to decode data and `-o` to output to a file.
199199

200200
## `shred`
201201

src/uu/base32/locales/en-US.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ base-common-read-error = read error: {$error}
5757
base-common-help-decode = decode data
5858
base-common-help-ignore-garbage = when decoding, ignore non-alphabetic characters
5959
base-common-help-wrap = wrap encoded lines after COLS character (default {$default}, 0 to disable wrapping)
60+
base-common-help-output-file = output to OUTPUT_FILE instead of stdout

src/uu/base32/src/base_common.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use clap::{Arg, ArgAction, Command};
99
use std::ffi::OsString;
1010
use std::fs::File;
11-
use std::io::{self, ErrorKind, Read, Seek};
11+
use std::io::{self, BufWriter, ErrorKind, Read, Seek, Write};
1212
use std::path::{Path, PathBuf};
1313
use uucore::display::Quotable;
1414
use uucore::encoding::{
@@ -34,13 +34,15 @@ pub struct Config {
3434
pub ignore_garbage: bool,
3535
pub wrap_cols: Option<usize>,
3636
pub to_read: Option<PathBuf>,
37+
pub output_file: Option<PathBuf>,
3738
}
3839

3940
pub mod options {
4041
pub static DECODE: &str = "decode";
4142
pub static WRAP: &str = "wrap";
4243
pub static IGNORE_GARBAGE: &str = "ignore-garbage";
4344
pub static FILE: &str = "file";
45+
pub static OUTPUT_FILE: &str = "output_file";
4446
}
4547

4648
impl Config {
@@ -86,11 +88,17 @@ impl Config {
8688
})
8789
.transpose()?;
8890

91+
let output_file = match options.get_one::<OsString>(options::OUTPUT_FILE) {
92+
Some(value) if value != "-" => Some(Path::new(value).to_owned()),
93+
_ => None,
94+
};
95+
8996
Ok(Self {
9097
decode: options.get_flag(options::DECODE),
9198
ignore_garbage: options.get_flag(options::IGNORE_GARBAGE),
9299
wrap_cols,
93100
to_read,
101+
output_file,
94102
})
95103
}
96104
}
@@ -138,6 +146,14 @@ pub fn base_app(about: &'static str, usage: &str) -> Command {
138146
.help(translate!("base-common-help-wrap", "default" => WRAP_DEFAULT))
139147
.overrides_with(options::WRAP),
140148
)
149+
.arg(
150+
Arg::new(options::OUTPUT_FILE)
151+
.short('o')
152+
.long(options::OUTPUT_FILE)
153+
.help(translate!("base-common-help-output-file"))
154+
.value_parser(clap::value_parser!(OsString))
155+
.value_hint(clap::ValueHint::FilePath),
156+
)
141157
// "multiple" arguments are used to check whether there is more than one
142158
// file passed in.
143159
.arg(
@@ -194,7 +210,10 @@ pub fn handle_input<R: Read + Seek>(input: &mut R, format: Format, config: Confi
194210
get_supports_fast_decode_and_encode(format, config.decode, has_padding);
195211

196212
let supports_fast_decode_and_encode_ref = supports_fast_decode_and_encode.as_ref();
197-
let mut stdout_lock = io::stdout().lock();
213+
let mut stdout_lock: Box<dyn Write> = match &config.output_file {
214+
Some(path) => Box::new(BufWriter::new(File::create(path)?)),
215+
None => Box::new(io::stdout().lock()),
216+
};
198217
if config.decode {
199218
fast_decode::fast_decode(
200219
read,

src/uu/base64/benches/base64_bench.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,76 @@
66
use divan::{Bencher, black_box};
77
use std::ffi::OsString;
88
use uu_base64::uumain;
9-
use uucore::benchmark::{create_test_file, run_util_function, text_data};
9+
use uucore::benchmark::{create_test_file, run_util_function, setup_test_file, text_data};
1010

1111
fn create_tmp_file(size_mb: usize) -> String {
12-
let temp_dir = tempfile::tempdir().unwrap();
1312
let data = text_data::generate_by_size(size_mb, 80);
14-
let file_path = create_test_file(&data, temp_dir.path());
13+
let file_path = setup_test_file(&data);
1514
String::from(file_path.to_str().unwrap())
1615
}
1716

1817
/// Benchmark for base64 encoding
1918
#[divan::bench()]
2019
fn b64_encode_synthetic(bencher: Bencher) {
21-
let file_path_str = &create_tmp_file(5_000);
20+
let file_path_str = &create_tmp_file(50);
2221

2322
bencher.bench(|| {
24-
black_box(run_util_function(uumain, &[file_path_str]));
23+
black_box(run_util_function(
24+
uumain,
25+
&["-o", "/dev/null", file_path_str],
26+
));
2527
});
2628
}
2729

2830
// Benchmark for base64 decoding
2931
#[divan::bench()]
3032
fn b64_decode_synthetic(bencher: Bencher) {
3133
let temp_dir = tempfile::tempdir().unwrap();
32-
let file_path_str = &create_tmp_file(5_000);
34+
let file_path_str = &create_tmp_file(50);
3335
let in_file = create_test_file(b"", temp_dir.path());
3436
let in_file_str = in_file.to_str().unwrap();
3537
uumain(
3638
[
39+
OsString::from(uucore::util_name()),
40+
OsString::from("-o"),
41+
OsString::from(in_file_str),
3742
OsString::from(file_path_str),
38-
OsString::from(format!(">{in_file_str}")),
3943
]
4044
.iter()
4145
.map(|x| (*x).clone()),
4246
);
4347

4448
bencher.bench(|| {
45-
black_box(run_util_function(uumain, &["-d", in_file_str]));
49+
black_box(run_util_function(
50+
uumain,
51+
&["-d", "-o", "/dev/null", in_file_str],
52+
));
4653
});
4754
}
4855

4956
// Benchmark different file sizes for base64 decoding ignoring garbage characters
5057
#[divan::bench()]
5158
fn b64_decode_ignore_garbage_synthetic(bencher: Bencher) {
5259
let temp_dir = tempfile::tempdir().unwrap();
53-
let file_path_str = &create_tmp_file(5_000);
60+
let file_path_str = &create_tmp_file(50);
5461
let in_file = create_test_file(b"", temp_dir.path());
5562
let in_file_str = in_file.to_str().unwrap();
5663
uumain(
5764
[
65+
OsString::from(uucore::util_name()),
66+
OsString::from("-o"),
67+
OsString::from(in_file_str),
5868
OsString::from(file_path_str),
59-
OsString::from(format!(">{in_file_str}")),
6069
]
6170
.iter()
6271
.map(|x| (*x).clone()),
6372
);
6473

6574
bencher.bench(|| {
66-
black_box(run_util_function(uumain, &["-d", "-i", in_file_str]));
75+
black_box(run_util_function(
76+
uumain,
77+
&["-d", "-i", "-o", "/dev/null", in_file_str],
78+
));
6779
});
6880
}
6981

0 commit comments

Comments
 (0)