clap/benches/02_simple.rs

105 lines
2.9 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};
2015-09-01 16:00:51 +00:00
macro_rules! create_app {
2018-08-02 03:13:51 +00:00
() => {{
2022-02-12 03:48:29 +00:00
Command::new("claptests")
2018-08-02 03:13:51 +00:00
.version("0.1")
.about("tests clap library")
.author("Kevin K. <kbknapp@gmail.com>")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "tests flags"))
.arg(arg!(-o --option <opt> "tests options").required(false))
.arg(arg!([positional] "tests positional"))
2018-08-02 03:13:51 +00:00
}};
2015-09-01 16:00:51 +00:00
}
2020-03-05 09:06:17 +00:00
pub fn build_simple(c: &mut Criterion) {
c.bench_function("build_simple", |b| b.iter(|| create_app!()));
2020-01-31 09:13:44 +00:00
}
2015-09-01 16:00:51 +00:00
2020-03-05 09:06:17 +00:00
pub fn build_with_flag(c: &mut Criterion) {
c.bench_function("build_with_flag", |b| {
2022-02-12 03:48:29 +00:00
b.iter(|| Command::new("claptests").arg(arg!(-s --some "something")))
2020-02-21 00:12:28 +00:00
});
}
2020-03-05 09:06:17 +00:00
pub fn build_with_flag_ref(c: &mut Criterion) {
c.bench_function("build_with_flag_ref", |b| {
2020-02-21 00:12:28 +00:00
b.iter(|| {
2021-11-19 20:33:11 +00:00
let arg = arg!(-s --some "something");
2022-02-12 03:48:29 +00:00
Command::new("claptests").arg(&arg)
2020-02-21 00:12:28 +00:00
})
});
}
2020-03-05 09:06:17 +00:00
pub fn build_with_opt(c: &mut Criterion) {
c.bench_function("build_with_opt", |b| {
2022-02-12 03:48:29 +00:00
b.iter(|| Command::new("claptests").arg(arg!(-s --some <FILE> "something")))
2020-02-21 00:12:28 +00:00
});
}
2020-03-05 09:06:17 +00:00
pub fn build_with_opt_ref(c: &mut Criterion) {
c.bench_function("build_with_opt_ref", |b| {
2020-02-21 00:12:28 +00:00
b.iter(|| {
2021-11-19 20:33:11 +00:00
let arg = arg!(-s --some <FILE> "something");
2022-02-12 03:48:29 +00:00
Command::new("claptests").arg(&arg)
2020-02-21 00:12:28 +00:00
})
});
}
2020-03-05 09:06:17 +00:00
pub fn build_with_pos(c: &mut Criterion) {
c.bench_function("build_with_pos", |b| {
2022-02-12 03:48:29 +00:00
b.iter(|| Command::new("claptests").arg(Arg::new("some")))
2020-02-21 00:12:28 +00:00
});
}
2020-03-05 09:06:17 +00:00
pub fn build_with_pos_ref(c: &mut Criterion) {
c.bench_function("build_with_pos_ref", |b| {
2020-02-21 00:12:28 +00:00
b.iter(|| {
let arg = Arg::new("some");
2022-02-12 03:48:29 +00:00
Command::new("claptests").arg(&arg)
2020-02-21 00:12:28 +00:00
})
});
}
2020-03-05 09:06:17 +00:00
pub fn parse_simple_with_flag(c: &mut Criterion) {
c.bench_function("parse_simple_with_flag", |b| {
2020-02-21 00:12:28 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-f"]))
});
2020-01-31 09:13:44 +00:00
}
2015-09-01 16:00:51 +00:00
2020-03-05 09:06:17 +00:00
pub fn parse_simple_with_opt(c: &mut Criterion) {
c.bench_function("parse_simple_with_opt", |b| {
2020-02-21 00:12:28 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1"]))
});
2020-01-31 09:13:44 +00:00
}
2015-09-01 16:00:51 +00:00
2020-03-05 09:06:17 +00:00
pub fn parse_simple_with_pos(c: &mut Criterion) {
c.bench_function("parse_simple_with_pos", |b| {
2020-02-21 00:12:28 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "arg1"]))
});
2015-09-01 16:00:51 +00:00
}
2020-03-05 09:06:17 +00:00
pub fn parse_simple_with_complex(c: &mut Criterion) {
c.bench_function("parse_simple_with_complex", |b| {
2020-02-21 00:12:28 +00:00
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1", "-f", "arg1"]))
});
2015-09-01 16:00:51 +00:00
}
2020-02-21 00:12:28 +00:00
criterion_group!(
benches,
2020-03-05 09:06:17 +00:00
parse_simple_with_complex,
parse_simple_with_pos,
parse_simple_with_opt,
parse_simple_with_flag,
build_with_pos_ref,
build_with_pos,
build_with_opt_ref,
build_with_opt,
build_with_flag_ref,
build_with_flag,
build_simple
2020-02-17 18:14:19 +00:00
);
criterion_main!(benches);