clap/benches/03_complex.rs

308 lines
11 KiB
Rust
Raw Normal View History

2022-02-12 03:48:29 +00:00
use clap::{arg, Arg, Command};
2020-02-17 18:14:19 +00:00
use criterion::{criterion_group, criterion_main, Criterion};
static OPT3_VALS: [&str; 2] = ["fast", "slow"];
static POS3_VALS: [&str; 2] = ["vi", "emacs"];
macro_rules! create_app {
2021-11-19 20:33:11 +00:00
() => {{
2022-02-12 03:48:29 +00:00
Command::new("claptests")
2021-11-19 20:33:11 +00:00
.version("0.1")
.about("tests clap library")
.author("Kevin K. <kbknapp@gmail.com>")
.arg(arg!(-o --option <opt> ... "tests options").required(false))
.arg(arg!([positional] "tests positionals"))
.arg(arg!(-f --flag ... "tests flags").global(true))
.args(&[
arg!(flag2: -F "tests flags with exclusions")
.conflicts_with("flag")
.requires("option2"),
arg!(option2: --"long-option-2" <option2> "tests long options with exclusions")
.required(false)
.conflicts_with("option")
.requires("positional2"),
arg!([positional2] "tests positionals with exclusions"),
arg!(-O --Option <option3> "tests options with specific value sets")
.required(false)
2022-05-24 01:16:02 +00:00
.value_parser(OPT3_VALS),
2021-11-19 20:33:11 +00:00
arg!([positional3] ... "tests positionals with specific values")
2022-05-24 01:16:02 +00:00
.value_parser(POS3_VALS),
2021-11-19 20:33:11 +00:00
arg!(--multvals "Tests multiple values not mult occs").required(false).value_names(&["one", "two"]),
arg!(
--multvalsmo "Tests multiple values, not mult occs"
).multiple_values(true).required(false).value_names(&["one", "two"]),
arg!(--minvals2 <minvals> ... "Tests 2 min vals").min_values(2).multiple_values(true).required(false),
arg!(--maxvals3 <maxvals> ... "Tests 3 max vals").max_values(3).multiple_values(true).required(false),
])
.subcommand(
2022-02-12 03:48:29 +00:00
Command::new("subcmd")
2021-11-19 20:33:11 +00:00
.about("tests subcommands")
.version("0.1")
.author("Kevin K. <kbknapp@gmail.com>")
.arg(arg!(-o --option <scoption> ... "tests options").required(false))
.arg(arg!([scpositional] "tests positionals"))
)
}};
}
2020-03-05 09:06:17 +00:00
pub fn build_from_builder(c: &mut Criterion) {
c.bench_function("build_from_builder", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| {
2022-02-12 03:48:29 +00:00
Command::new("claptests")
2020-02-17 18:14:19 +00:00
.version("0.1")
.about("tests clap library")
.author("Kevin K. <kbknapp@gmail.com>")
.arg(
Arg::new("opt")
.help("tests options")
2020-02-17 18:14:19 +00:00
.short('o')
.long("option")
.takes_value(true)
.multiple_values(true)
.multiple_occurrences(true),
2020-02-17 18:14:19 +00:00
)
.arg(Arg::new("positional").help("tests positionals").index(1))
2020-02-17 18:14:19 +00:00
.arg(
Arg::new("flag")
2020-02-17 18:14:19 +00:00
.short('f')
.help("tests flags")
2020-02-17 18:14:19 +00:00
.long("flag")
.global(true)
.multiple_occurrences(true),
2020-02-17 18:14:19 +00:00
)
.arg(
Arg::new("flag2")
2020-02-17 18:14:19 +00:00
.short('F')
.help("tests flags with exclusions")
2020-02-17 18:14:19 +00:00
.conflicts_with("flag")
.requires("option2"),
)
.arg(
Arg::new("option2")
.help("tests long options with exclusions")
2020-02-17 18:14:19 +00:00
.conflicts_with("option")
.requires("positional2")
.takes_value(true)
2020-02-17 18:14:19 +00:00
.long("long-option-2"),
)
.arg(
Arg::new("positional2")
2020-02-17 18:14:19 +00:00
.index(3)
.help("tests positionals with exclusions"),
2020-02-17 18:14:19 +00:00
)
.arg(
Arg::new("option3")
2020-02-17 18:14:19 +00:00
.short('O')
.long("Option")
.takes_value(true)
.help("tests options with specific value sets")
2022-05-24 01:16:02 +00:00
.value_parser(OPT3_VALS),
2020-02-17 18:14:19 +00:00
)
.arg(
Arg::new("positional3")
.takes_value(true)
.multiple_values(true)
.multiple_occurrences(true)
.help("tests positionals with specific values")
2020-02-17 18:14:19 +00:00
.index(4)
2022-05-24 01:16:02 +00:00
.value_parser(POS3_VALS),
2020-02-17 18:14:19 +00:00
)
.arg(
Arg::new("multvals")
2020-02-17 18:14:19 +00:00
.long("multvals")
.help("Tests multiple values, not mult occs")
2020-02-17 18:14:19 +00:00
.value_names(&["one", "two"]),
)
.arg(
Arg::new("multvalsmo")
2020-02-17 18:14:19 +00:00
.long("multvalsmo")
.takes_value(true)
.multiple_values(true)
.multiple_occurrences(true)
.help("Tests multiple values, not mult occs")
2020-02-17 18:14:19 +00:00
.value_names(&["one", "two"]),
)
.arg(
Arg::new("minvals")
2020-02-17 18:14:19 +00:00
.long("minvals2")
.takes_value(true)
.multiple_values(true)
.multiple_occurrences(true)
.help("Tests 2 min vals")
2020-02-17 18:14:19 +00:00
.min_values(2),
)
.arg(
Arg::new("maxvals")
2020-02-17 18:14:19 +00:00
.long("maxvals3")
.takes_value(true)
.multiple_values(true)
.multiple_occurrences(true)
.help("Tests 3 max vals")
2020-02-17 18:14:19 +00:00
.max_values(3),
)
.subcommand(
2022-02-12 03:48:29 +00:00
Command::new("subcmd")
2020-02-17 18:14:19 +00:00
.about("tests subcommands")
.version("0.1")
.author("Kevin K. <kbknapp@gmail.com>")
.arg(
Arg::new("scoption")
2020-02-17 18:14:19 +00:00
.short('o')
.long("option")
.takes_value(true)
.multiple_values(true)
.multiple_occurrences(true)
.help("tests options"),
2020-02-17 18:14:19 +00:00
)
.arg(Arg::new("scpositional").index(1).help("tests positionals")),
2020-02-17 18:14:19 +00:00
)
})
});
}
2020-03-05 09:06:17 +00:00
pub fn parse_complex(c: &mut Criterion) {
c.bench_function("parse_complex", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| create_app!().get_matches_from(vec![""]))
});
2020-01-31 09:13:44 +00:00
}
2020-03-05 09:06:17 +00:00
pub fn parse_complex_with_flag(c: &mut Criterion) {
c.bench_function("parse_complex_with_flag", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-f"]))
});
}
2020-03-05 09:06:17 +00:00
pub fn parse_complex_with_opt(c: &mut Criterion) {
c.bench_function("parse_complex_with_opt", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1"]))
});
}
2020-03-05 09:06:17 +00:00
pub fn parse_complex_with_pos(c: &mut Criterion) {
c.bench_function("parse_complex_with_pos", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "arg1"]))
});
}
2020-03-05 09:06:17 +00:00
pub fn parse_complex_with_sc(c: &mut Criterion) {
c.bench_function("parse_complex_with_sc", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd"]))
});
}
2020-03-05 09:06:17 +00:00
pub fn parse_complex_with_sc_flag(c: &mut Criterion) {
c.bench_function("parse_complex_with_sc_flag", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd", "-f"]))
});
}
2020-03-05 09:06:17 +00:00
pub fn parse_complex_with_sc_opt(c: &mut Criterion) {
c.bench_function("parse_complex_with_sc_opt", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd", "-o", "option1"]))
});
}
2020-03-05 09:06:17 +00:00
pub fn parse_complex_with_sc_pos(c: &mut Criterion) {
c.bench_function("parse_complex_with_sc_pos", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "subcmd", "arg1"]))
2018-01-25 04:05:05 +00:00
});
}
2020-02-17 18:14:19 +00:00
pub fn parse_complex1(c: &mut Criterion) {
c.bench_function("parse_complex1", |b| {
b.iter(|| {
create_app!().get_matches_from(vec![
"myprog",
"-ff",
"-o",
"option1",
"arg1",
"-O",
"fast",
"arg2",
"--multvals",
"one",
"two",
"emacs",
])
})
2018-01-25 04:05:05 +00:00
});
}
2020-02-17 18:14:19 +00:00
pub fn parse_complex2(c: &mut Criterion) {
c.bench_function("parse_complex2", |b| {
b.iter(|| {
create_app!().get_matches_from(vec![
2018-01-25 04:05:05 +00:00
"myprog",
"arg1",
"-f",
"arg2",
"--long-option-2",
"some",
"-O",
"slow",
"--multvalsmo",
"one",
"two",
"--minvals2",
"3",
"2",
"1",
])
2020-02-17 18:14:19 +00:00
})
});
}
2020-03-05 09:06:17 +00:00
pub fn parse_args_negate_scs(c: &mut Criterion) {
c.bench_function("parse_args_negate_scs", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| {
create_app!()
.args_conflicts_with_subcommands(true)
2020-02-17 18:14:19 +00:00
.get_matches_from(vec![
"myprog",
"arg1",
"-f",
"arg2",
"--long-option-2",
"some",
"-O",
"slow",
"--multvalsmo",
"one",
"two",
"--minvals2",
"3",
"2",
"1",
])
})
2018-01-25 04:05:05 +00:00
});
}
2020-03-05 09:06:17 +00:00
pub fn parse_complex_with_sc_complex(c: &mut Criterion) {
c.bench_function("parse_complex_with_sc_complex", |b| {
2020-02-17 18:14:19 +00:00
b.iter(|| {
create_app!().get_matches_from(vec!["myprog", "subcmd", "-f", "-o", "option1", "arg1"])
})
2018-01-25 04:05:05 +00:00
});
}
2020-02-17 18:14:19 +00:00
criterion_group!(
benches,
2020-03-05 09:06:17 +00:00
build_from_builder,
parse_complex,
parse_complex_with_flag,
parse_complex_with_opt,
parse_complex_with_pos,
parse_complex_with_sc,
parse_complex_with_sc_flag,
parse_complex_with_sc_opt,
parse_complex_with_sc_pos,
2020-02-17 18:14:19 +00:00
parse_complex1,
parse_complex2,
2020-03-05 09:06:17 +00:00
parse_args_negate_scs,
parse_complex_with_sc_complex
2020-02-17 18:14:19 +00:00
);
criterion_main!(benches);