2020-01-09 04:08:41 +00:00
|
|
|
use clap::{clap_app, App, AppSettings, Arg, ArgSettings};
|
2020-02-17 18:14:19 +00:00
|
|
|
use criterion::{criterion_group, criterion_main, Criterion};
|
2015-08-30 04:03:11 +00:00
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static OPT3_VALS: [&str; 2] = ["fast", "slow"];
|
|
|
|
static POS3_VALS: [&str; 2] = ["vi", "emacs"];
|
2015-08-30 18:14:42 +00:00
|
|
|
|
|
|
|
macro_rules! create_app {
|
2018-07-23 19:09:42 +00:00
|
|
|
() => {{
|
2015-08-30 18:14:42 +00:00
|
|
|
App::new("claptests")
|
2018-07-23 19:09:42 +00:00
|
|
|
.version("0.1")
|
|
|
|
.about("tests clap library")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
2018-11-14 03:16:02 +00:00
|
|
|
.arg("-o --option=[opt]... 'tests options'")
|
|
|
|
.arg("[positional] 'tests positionals'")
|
2019-06-19 23:58:18 +00:00
|
|
|
.arg(Arg::from("-f --flag... 'tests flags'").global(true))
|
2018-07-23 19:09:42 +00:00
|
|
|
.args(&[
|
|
|
|
Arg::from("[flag2] -F 'tests flags with exclusions'")
|
|
|
|
.conflicts_with("flag")
|
|
|
|
.requires("option2"),
|
|
|
|
Arg::from("--long-option-2 [option2] 'tests long options with exclusions'")
|
|
|
|
.conflicts_with("option")
|
|
|
|
.requires("positional2"),
|
|
|
|
Arg::from("[positional2] 'tests positionals with exclusions'"),
|
|
|
|
Arg::from("-O --Option [option3] 'tests options with specific value sets'")
|
|
|
|
.possible_values(&OPT3_VALS),
|
|
|
|
Arg::from("[positional3]... 'tests positionals with specific values'")
|
|
|
|
.possible_values(&POS3_VALS),
|
2020-07-18 17:47:04 +00:00
|
|
|
Arg::from("--multvals [one] [two] 'Tests multiple values, not mult occs'"),
|
|
|
|
Arg::from("--multvalsmo... [one] [two] 'Tests multiple values, not mult occs'"),
|
2018-07-23 19:09:42 +00:00
|
|
|
Arg::from("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2),
|
|
|
|
Arg::from("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3),
|
|
|
|
])
|
|
|
|
.subcommand(
|
2018-10-19 20:42:13 +00:00
|
|
|
App::new("subcmd")
|
2018-07-23 19:09:42 +00:00
|
|
|
.about("tests subcommands")
|
|
|
|
.version("0.1")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-o --option [scoption]... 'tests options'")
|
|
|
|
.arg("[scpositional] 'tests positionals'"),
|
2018-07-23 19:09:42 +00:00
|
|
|
)
|
|
|
|
}};
|
2015-08-30 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 09:06:17 +00:00
|
|
|
pub fn build_from_usage(c: &mut Criterion) {
|
|
|
|
c.bench_function("build_from_usage", |b| b.iter(|| create_app!()));
|
2020-01-31 09:13:44 +00:00
|
|
|
}
|
2015-08-30 04:03:11 +00:00
|
|
|
|
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(|| {
|
|
|
|
App::new("claptests")
|
|
|
|
.version("0.1")
|
|
|
|
.about("tests clap library")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("opt")
|
2020-04-27 14:30:36 +00:00
|
|
|
.about("tests options")
|
2020-02-17 18:14:19 +00:00
|
|
|
.short('o')
|
|
|
|
.long("option")
|
2021-02-24 15:07:57 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2020-02-17 18:14:19 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("positional").about("tests positionals").index(1))
|
2020-02-17 18:14:19 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("flag")
|
2020-02-17 18:14:19 +00:00
|
|
|
.short('f')
|
2020-04-27 14:30:36 +00:00
|
|
|
.about("tests flags")
|
2020-02-17 18:14:19 +00:00
|
|
|
.long("flag")
|
|
|
|
.global(true)
|
2021-02-11 00:12:36 +00:00
|
|
|
.setting(ArgSettings::MultipleOccurrences),
|
2020-02-17 18:14:19 +00:00
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("flag2")
|
2020-02-17 18:14:19 +00:00
|
|
|
.short('F')
|
2020-04-27 14:30:36 +00:00
|
|
|
.about("tests flags with exclusions")
|
2020-02-17 18:14:19 +00:00
|
|
|
.conflicts_with("flag")
|
|
|
|
.requires("option2"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option2")
|
2020-04-27 14:30:36 +00:00
|
|
|
.about("tests long options with exclusions")
|
2020-02-17 18:14:19 +00:00
|
|
|
.conflicts_with("option")
|
|
|
|
.requires("positional2")
|
|
|
|
.setting(ArgSettings::TakesValue)
|
|
|
|
.long("long-option-2"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("positional2")
|
2020-02-17 18:14:19 +00:00
|
|
|
.index(3)
|
2020-04-27 14:30:36 +00:00
|
|
|
.about("tests positionals with exclusions"),
|
2020-02-17 18:14:19 +00:00
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option3")
|
2020-02-17 18:14:19 +00:00
|
|
|
.short('O')
|
|
|
|
.long("Option")
|
|
|
|
.setting(ArgSettings::TakesValue)
|
2020-04-27 14:30:36 +00:00
|
|
|
.about("tests options with specific value sets")
|
2020-02-17 18:14:19 +00:00
|
|
|
.possible_values(&OPT3_VALS),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("positional3")
|
2021-02-24 15:07:57 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2020-02-17 18:14:19 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2020-04-27 14:30:36 +00:00
|
|
|
.about("tests positionals with specific values")
|
2020-02-17 18:14:19 +00:00
|
|
|
.index(4)
|
|
|
|
.possible_values(&POS3_VALS),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("multvals")
|
2020-02-17 18:14:19 +00:00
|
|
|
.long("multvals")
|
2020-07-18 17:47:04 +00:00
|
|
|
.about("Tests multiple values, not mult occs")
|
2020-02-17 18:14:19 +00:00
|
|
|
.value_names(&["one", "two"]),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("multvalsmo")
|
2020-02-17 18:14:19 +00:00
|
|
|
.long("multvalsmo")
|
2021-02-24 15:07:57 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2020-02-17 18:14:19 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2020-07-18 17:47:04 +00:00
|
|
|
.about("Tests multiple values, not mult occs")
|
2020-02-17 18:14:19 +00:00
|
|
|
.value_names(&["one", "two"]),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("minvals")
|
2020-02-17 18:14:19 +00:00
|
|
|
.long("minvals2")
|
2021-02-24 15:07:57 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2020-02-17 18:14:19 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2020-04-27 14:30:36 +00:00
|
|
|
.about("Tests 2 min vals")
|
2020-02-17 18:14:19 +00:00
|
|
|
.min_values(2),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("maxvals")
|
2020-02-17 18:14:19 +00:00
|
|
|
.long("maxvals3")
|
2021-02-24 15:07:57 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2020-02-17 18:14:19 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2020-04-27 14:30:36 +00:00
|
|
|
.about("Tests 3 max vals")
|
2020-02-17 18:14:19 +00:00
|
|
|
.max_values(3),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
App::new("subcmd")
|
|
|
|
.about("tests subcommands")
|
|
|
|
.version("0.1")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("scoption")
|
2020-02-17 18:14:19 +00:00
|
|
|
.short('o')
|
|
|
|
.long("option")
|
2021-02-24 15:07:57 +00:00
|
|
|
.setting(ArgSettings::TakesValue)
|
2020-02-17 18:14:19 +00:00
|
|
|
.setting(ArgSettings::MultipleValues)
|
|
|
|
.setting(ArgSettings::MultipleOccurrences)
|
2020-04-27 14:30:36 +00:00
|
|
|
.about("tests options"),
|
2020-02-17 18:14:19 +00:00
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("scpositional").index(1).about("tests positionals")),
|
2020-02-17 18:14:19 +00:00
|
|
|
)
|
|
|
|
})
|
2015-09-10 00:52:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-05 09:06:17 +00:00
|
|
|
pub fn build_from_macros(c: &mut Criterion) {
|
|
|
|
c.bench_function("build_from_macros", |b| {
|
2020-02-17 18:14:19 +00:00
|
|
|
b.iter(|| {
|
|
|
|
clap_app!(claptests =>
|
2015-09-10 12:23:58 +00:00
|
|
|
(version: "0.1")
|
2020-02-17 18:14:19 +00:00
|
|
|
(about: "tests clap library")
|
2015-09-10 12:23:58 +00:00
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
2020-02-17 18:14:19 +00:00
|
|
|
(@arg opt: -o --option +takes_value ... "tests options")
|
|
|
|
(@arg positional: index(1) "tests positionals")
|
|
|
|
(@arg flag: -f --flag ... +global "tests flags")
|
|
|
|
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
|
|
"tests flags with exclusions")
|
|
|
|
(@arg option2: --long_option_2 conflicts_with[option] requires[positional2]
|
|
|
|
"tests long options with exclusions")
|
|
|
|
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
|
|
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
|
|
"tests options with specific value sets")
|
|
|
|
(@arg positional3: index(3) ... possible_value[vi emacs]
|
|
|
|
"tests positionals with specific values")
|
|
|
|
(@arg multvals: --multvals +takes_value value_name[one two]
|
2020-07-18 17:47:04 +00:00
|
|
|
"Tests multiple values, not mult occs")
|
2020-02-17 18:14:19 +00:00
|
|
|
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
2020-07-18 17:47:04 +00:00
|
|
|
"Tests multiple values, not mult occs")
|
2020-02-17 18:14:19 +00:00
|
|
|
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
|
|
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
|
|
(@subcommand subcmd =>
|
|
|
|
(about: "tests subcommands")
|
|
|
|
(version: "0.1")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
|
|
(@arg scpositional: index(1) "tests positionals"))
|
|
|
|
);
|
|
|
|
})
|
2015-09-10 12:23:58 +00:00
|
|
|
});
|
|
|
|
}
|
2015-09-10 00:52:04 +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
|
|
|
}
|
2015-08-30 18:46:53 +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"]))
|
|
|
|
});
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
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"]))
|
|
|
|
});
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
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"]))
|
|
|
|
});
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
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"]))
|
|
|
|
});
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
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"]))
|
|
|
|
});
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
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"]))
|
|
|
|
});
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|
2015-08-30 18:46:53 +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
|
|
|
});
|
2015-08-30 18:46:53 +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!()
|
|
|
|
.setting(AppSettings::ArgsNegateSubcommands)
|
|
|
|
.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
|
|
|
});
|
2017-01-02 19:38:17 +00:00
|
|
|
}
|
2015-08-30 18:46:53 +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
|
|
|
});
|
2015-08-30 18:46:53 +00:00
|
|
|
}
|
2020-02-17 18:14:19 +00:00
|
|
|
|
|
|
|
criterion_group!(
|
|
|
|
benches,
|
2020-03-05 09:06:17 +00:00
|
|
|
build_from_usage,
|
|
|
|
build_from_builder,
|
|
|
|
build_from_macros,
|
|
|
|
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);
|