summaryrefslogtreecommitdiffstats
path: root/third_party/rust/clap/benches/02_simple.rs
blob: 667d02f873c859d8a0eeaf144e84037e1b335f43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use clap::{arg, Arg, Command};
use criterion::{criterion_group, criterion_main, Criterion};

macro_rules! create_app {
    () => {{
        Command::new("claptests")
            .version("0.1")
            .about("tests clap library")
            .author("Kevin K. <kbknapp@gmail.com>")
            .arg(arg!(-f --flag         "tests flags"))
            .arg(arg!(-o --option <opt> "tests options").required(false))
            .arg(arg!([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(|| Command::new("claptests").arg(arg!(-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!(-s --some "something");
            Command::new("claptests").arg(&arg)
        })
    });
}

pub fn build_with_opt(c: &mut Criterion) {
    c.bench_function("build_with_opt", |b| {
        b.iter(|| Command::new("claptests").arg(arg!(-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!(-s --some <FILE> "something");
            Command::new("claptests").arg(&arg)
        })
    });
}

pub fn build_with_pos(c: &mut Criterion) {
    c.bench_function("build_with_pos", |b| {
        b.iter(|| Command::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");
            Command::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);