clap/benches/02_simple.rs
Ed Page 88fff13e71 Revert rename of from_yaml / from_usage
Since usage parser and yaml are on the way to being deprecated (#8, #9),
doing a rename also seems excessive, so rolling it back.

Past relevant PRs:
- clap-rs/clap#1157
- clap-rs/clap#1257
2021-11-22 16:17:46 -06:00

104 lines
2.9 KiB
Rust

use clap::{App, Arg};
use criterion::{criterion_group, criterion_main, Criterion};
macro_rules! create_app {
() => {{
App::new("claptests")
.version("0.1")
.about("tests clap library")
.author("Kevin K. <kbknapp@gmail.com>")
.arg(Arg::from_usage("-f --flag 'tests flags'"))
.arg(Arg::from_usage("-o --option=[opt] 'tests options'"))
.arg(Arg::from_usage("[positional] 'tests positional'"))
}};
}
pub fn build_simple(c: &mut Criterion) {
c.bench_function("build_simple", |b| b.iter(|| create_app!()));
}
pub fn build_with_flag(c: &mut Criterion) {
c.bench_function("build_with_flag", |b| {
b.iter(|| App::new("claptests").arg(Arg::from_usage("-s, --some 'something'")))
});
}
pub fn build_with_flag_ref(c: &mut Criterion) {
c.bench_function("build_with_flag_ref", |b| {
b.iter(|| {
let arg = Arg::from_usage("-s, --some 'something'");
App::new("claptests").arg(&arg)
})
});
}
pub fn build_with_opt(c: &mut Criterion) {
c.bench_function("build_with_opt", |b| {
b.iter(|| App::new("claptests").arg(Arg::from_usage("-s, --some <FILE> 'something'")))
});
}
pub fn build_with_opt_ref(c: &mut Criterion) {
c.bench_function("build_with_opt_ref", |b| {
b.iter(|| {
let arg = Arg::from_usage("-s, --some <FILE> 'something'");
App::new("claptests").arg(&arg)
})
});
}
pub fn build_with_pos(c: &mut Criterion) {
c.bench_function("build_with_pos", |b| {
b.iter(|| App::new("claptests").arg(Arg::new("some")))
});
}
pub fn build_with_pos_ref(c: &mut Criterion) {
c.bench_function("build_with_pos_ref", |b| {
b.iter(|| {
let arg = Arg::new("some");
App::new("claptests").arg(&arg)
})
});
}
pub fn parse_simple_with_flag(c: &mut Criterion) {
c.bench_function("parse_simple_with_flag", |b| {
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-f"]))
});
}
pub fn parse_simple_with_opt(c: &mut Criterion) {
c.bench_function("parse_simple_with_opt", |b| {
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1"]))
});
}
pub fn parse_simple_with_pos(c: &mut Criterion) {
c.bench_function("parse_simple_with_pos", |b| {
b.iter(|| create_app!().get_matches_from(vec!["myprog", "arg1"]))
});
}
pub fn parse_simple_with_complex(c: &mut Criterion) {
c.bench_function("parse_simple_with_complex", |b| {
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1", "-f", "arg1"]))
});
}
criterion_group!(
benches,
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
);
criterion_main!(benches);