1use clap::{arg, Arg, Command};
2use criterion::{criterion_group, criterion_main, Criterion};
3
4macro_rules! create_app {
5    () => {{
6        Command::new("claptests")
7            .version("0.1")
8            .about("tests clap library")
9            .author("Kevin K. <kbknapp@gmail.com>")
10            .arg(arg!(-f --flag         "tests flags"))
11            .arg(arg!(-o --option <opt> "tests options"))
12            .arg(arg!([positional]      "tests positional"))
13    }};
14}
15
16pub fn build_simple(c: &mut Criterion) {
17    c.bench_function("build_simple", |b| b.iter(|| create_app!()));
18}
19
20pub fn build_with_flag(c: &mut Criterion) {
21    c.bench_function("build_with_flag", |b| {
22        b.iter(|| Command::new("claptests").arg(arg!(-s --some "something")))
23    });
24}
25
26pub fn build_with_flag_ref(c: &mut Criterion) {
27    c.bench_function("build_with_flag_ref", |b| {
28        b.iter(|| {
29            let arg = arg!(-s --some "something");
30            Command::new("claptests").arg(&arg)
31        })
32    });
33}
34
35pub fn build_with_opt(c: &mut Criterion) {
36    c.bench_function("build_with_opt", |b| {
37        b.iter(|| Command::new("claptests").arg(arg!(-s --some <FILE> "something").required(true)))
38    });
39}
40
41pub fn build_with_opt_ref(c: &mut Criterion) {
42    c.bench_function("build_with_opt_ref", |b| {
43        b.iter(|| {
44            let arg = arg!(-s --some <FILE> "something").required(true);
45            Command::new("claptests").arg(&arg)
46        })
47    });
48}
49
50pub fn build_with_pos(c: &mut Criterion) {
51    c.bench_function("build_with_pos", |b| {
52        b.iter(|| Command::new("claptests").arg(Arg::new("some")))
53    });
54}
55
56pub fn build_with_pos_ref(c: &mut Criterion) {
57    c.bench_function("build_with_pos_ref", |b| {
58        b.iter(|| {
59            let arg = Arg::new("some");
60            Command::new("claptests").arg(&arg)
61        })
62    });
63}
64
65pub fn parse_simple_with_flag(c: &mut Criterion) {
66    c.bench_function("parse_simple_with_flag", |b| {
67        b.iter(|| create_app!().get_matches_from(vec!["myprog", "-f"]))
68    });
69}
70
71pub fn parse_simple_with_opt(c: &mut Criterion) {
72    c.bench_function("parse_simple_with_opt", |b| {
73        b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1"]))
74    });
75}
76
77pub fn parse_simple_with_pos(c: &mut Criterion) {
78    c.bench_function("parse_simple_with_pos", |b| {
79        b.iter(|| create_app!().get_matches_from(vec!["myprog", "arg1"]))
80    });
81}
82
83pub fn parse_simple_with_complex(c: &mut Criterion) {
84    c.bench_function("parse_simple_with_complex", |b| {
85        b.iter(|| create_app!().get_matches_from(vec!["myprog", "-o", "option1", "-f", "arg1"]))
86    });
87}
88
89criterion_group!(
90    benches,
91    parse_simple_with_complex,
92    parse_simple_with_pos,
93    parse_simple_with_opt,
94    parse_simple_with_flag,
95    build_with_pos_ref,
96    build_with_pos,
97    build_with_opt_ref,
98    build_with_opt,
99    build_with_flag_ref,
100    build_with_flag,
101    build_simple
102);
103
104criterion_main!(benches);
105