clap/benches/02_simple.rs
CreepySkeleton d466a56aa1 Fix benches
2020-03-04 21:31:18 +03:00

104 lines
3 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("-f --flag 'tests flags'")
.arg("-o --option=[opt] 'tests options'")
.arg("[positional] 'tests positional'")
}};
}
pub fn build_simple_app(c: &mut Criterion) {
c.bench_function("build_simple_app", |b| b.iter(|| create_app!()));
}
pub fn build_app_with_flag(c: &mut Criterion) {
c.bench_function("build_app_with_flag", |b| {
b.iter(|| App::new("claptests").arg(Arg::from("-s, --some 'something'")))
});
}
pub fn build_app_with_flag_ref(c: &mut Criterion) {
c.bench_function("build_app_with_flag_ref", |b| {
b.iter(|| {
let arg = Arg::from("-s, --some 'something'");
App::new("claptests").arg(&arg)
})
});
}
pub fn build_app_with_opt(c: &mut Criterion) {
c.bench_function("build_app_with_opt", |b| {
b.iter(|| App::new("claptests").arg(Arg::from("-s, --some <FILE> 'something'")))
});
}
pub fn build_app_with_opt_ref(c: &mut Criterion) {
c.bench_function("build_app_with_opt_ref", |b| {
b.iter(|| {
let arg = Arg::from("-s, --some <FILE> 'something'");
App::new("claptests").arg(&arg)
})
});
}
pub fn build_app_with_pos(c: &mut Criterion) {
c.bench_function("build_app_with_pos", |b| {
b.iter(|| App::new("claptests").arg(Arg::with_name("some")))
});
}
pub fn build_app_with_pos_ref(c: &mut Criterion) {
c.bench_function("build_app_with_pos_ref", |b| {
b.iter(|| {
let arg = Arg::with_name("some");
App::new("claptests").arg(&arg)
})
});
}
pub fn parse_simple_app_with_flag(c: &mut Criterion) {
c.bench_function("parse_simple_app_with_flag", |b| {
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-f"]))
});
}
pub fn parse_simple_app_with_option(c: &mut Criterion) {
c.bench_function("parse_simple_app_with_option", |b| {
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1"]))
});
}
pub fn parse_simple_app_with_positional(c: &mut Criterion) {
c.bench_function("parse_simple_app_with_positional", |b| {
b.iter(|| create_app!().get_matches_from(vec!["myprog", "arg1"]))
});
}
pub fn parse_simple_app_with_complex(c: &mut Criterion) {
c.bench_function("parse_simple_app_with_complex", |b| {
b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1", "-f", "arg1"]))
});
}
criterion_group!(
benches,
parse_simple_app_with_complex,
parse_simple_app_with_positional,
parse_simple_app_with_option,
parse_simple_app_with_flag,
build_app_with_pos_ref,
build_app_with_pos,
build_app_with_opt_ref,
build_app_with_opt,
build_app_with_flag_ref,
build_app_with_flag,
build_simple_app
);
criterion_main!(benches);