Debian Codegraph @

debian/rust-vendor/codspeed-divan-compat-walltime/src/cli.rs

raw
1 use clap::{builder::PossibleValue, value_parser, Arg, ArgAction, ColorChoice, Command, ValueEnum};
2
3 use crate::{
4 config::{ParsedSeconds, SortingAttr},
5 counter::MaxCountUInt,
6 time::TimerKind,
7 };
8
9 pub(crate) fn command() -> Command {
10 fn option(name: &'static str) -> Arg {
11 Arg::new(name).long(name)
12 }
13
14 fn flag(name: &'static str) -> Arg {
15 option(name).action(ArgAction::SetTrue)
16 }
17
18 fn ignored_flag(name: &'static str) -> Arg {
19 flag(name).hide(true)
20 }
21
22 // Custom arguments not supported by libtest:
23 // - bytes-format
24 // - sample-count
25 // - sample-size
26 // - timer
27 // - sort
28 // - sortr
29
30 // TODO: `--format <pretty|terse>`
31
32 Command::new("divan")
33 .arg(
34 Arg::new("filter")
35 .value_name("FILTER")
36 .help("Only run benchmarks whose names match this pattern")
37 .action(ArgAction::Append),
38 )
39 .arg(
40 flag("test")
41 .help("Run benchmarks once to ensure they run successfully")
42 .conflicts_with("list"),
43 )
44 .arg(flag("list").help("Lists benchmarks").conflicts_with("test"))
45 .arg(
46 option("color")
47 .value_name("WHEN")
48 .help("Controls when to use colors")
49 .value_parser(value_parser!(ColorChoice))
50 )
51 .arg(
52 option("skip")
53 .value_name("FILTER")
54 .help("Skip benchmarks whose names match this pattern")
55 .action(ArgAction::Append),
56 )
57 .arg(flag("exact").help("Filter benchmarks by exact name rather than by pattern"))
58 .arg(flag("ignored").help("Run only ignored benchmarks").conflicts_with("include-ignored"))
59 .arg(
60 flag("include-ignored")
61 .help("Run ignored and not-ignored benchmarks")
62 .conflicts_with("ignored"),
63 )
64 .arg(
65 option("sort")
66 .env("DIVAN_SORT")
67 .value_name("ATTRIBUTE")
68 .help("Sort benchmarks in ascending order")
69 .value_parser(value_parser!(SortingAttr))
70 )
71 .arg(
72 option("sortr")
73 .env("DIVAN_SORTR")
74 .value_name("ATTRIBUTE")
75 .help("Sort benchmarks in descending order")
76 .value_parser(value_parser!(SortingAttr))
77 .overrides_with("sort"),
78 )
79 .arg(
80 option("timer")
81 .env("DIVAN_TIMER")
82 .value_name("os|tsc")
83 .help("Set the timer used for measuring samples")
84 .value_parser(value_parser!(TimerKind)),
85 )
86 .arg(
87 option("sample-count")
88 .env("DIVAN_SAMPLE_COUNT")
89 .value_name("N")
90 .help("Set the number of sampling iterations")
91 .value_parser(value_parser!(u32)),
92 )
93 .arg(
94 option("sample-size")
95 .env("DIVAN_SAMPLE_SIZE")
96 .value_name("N")
97 .help("Set the number of iterations inside a single sample")
98 .value_parser(value_parser!(u32)),
99 )
100 .arg(
101 option("threads")
102 .env("DIVAN_THREADS")
103 .value_name("N")
104 .value_delimiter(',')
105 .action(ArgAction::Append)
106 .help("Run across multiple threads to measure contention on atomics and locks")
107 .value_parser(value_parser!(usize)),
108 )
109 .arg(
110 option("min-time")
111 .env("DIVAN_MIN_TIME")
112 .value_name("SECS")
113 .help("Set the minimum seconds spent benchmarking a single function")
114 .value_parser(value_parser!(ParsedSeconds)),
115 )
116 .arg(
117 option("max-time")
118 .env("DIVAN_MAX_TIME")
119 .value_name("SECS")
120 .help("Set the maximum seconds spent benchmarking a single function, with priority over '--min-time'")
121 .value_parser(value_parser!(ParsedSeconds)),
122 )
123 .arg(
124 option("skip-ext-time")
125 .env("DIVAN_SKIP_EXT_TIME")
126 .value_name("true|false")
127 .help("When '--min-time' or '--max-time' is set, skip time external to benchmarked functions")
128 .value_parser(value_parser!(bool))
129 .num_args(0..=1),
130 )
131 .arg(
132 option("items-count")
133 .env("DIVAN_ITEMS_COUNT")
134 .value_name("N")
135 .help("Set every benchmark to have a throughput of N items")
136 .value_parser(value_parser!(MaxCountUInt)),
137 )
138 .arg(
139 option("bytes-count")
140 .env("DIVAN_BYTES_COUNT")
141 .value_name("N")
142 .help("Set every benchmark to have a throughput of N bytes")
143 .value_parser(value_parser!(MaxCountUInt)),
144 )
145 .arg(
146 option("bytes-format")
147 .env("DIVAN_BYTES_FORMAT")
148 .help("Set the numerical base for bytes in output")
149 .value_name("decimal|binary")
150 .value_parser(value_parser!(crate::counter::PrivBytesFormat))
151 )
152 .arg(
153 option("chars-count")
154 .env("DIVAN_CHARS_COUNT")
155 .value_name("N")
156 .help("Set every benchmark to have a throughput of N string scalars")
157 .value_parser(value_parser!(MaxCountUInt)),
158 )
159 .arg(
160 option("cycles-count")
161 .env("DIVAN_CYCLES_COUNT")
162 .value_name("N")
163 .help("Set every benchmark to have a throughput of N cycles, displayed as Hertz")
164 .value_parser(value_parser!(MaxCountUInt)),
165 )
166 // ignored:
167 .args([ignored_flag("bench"), ignored_flag("nocapture"), ignored_flag("show-output")])
168 }
169
170 impl ValueEnum for TimerKind {
171 fn value_variants<'a>() -> &'a [Self] {
172 &[Self::Os, Self::Tsc]
173 }
174
175 fn to_possible_value(&self) -> Option<PossibleValue> {
176 let name = match self {
177 Self::Os => "os",
178 Self::Tsc => "tsc",
179 };
180 Some(PossibleValue::new(name))
181 }
182 }
183
184 impl ValueEnum for SortingAttr {
185 fn value_variants<'a>() -> &'a [Self] {
186 &[Self::Kind, Self::Name, Self::Location]
187 }
188
189 fn to_possible_value(&self) -> Option<PossibleValue> {
190 let name = match self {
191 Self::Kind => "kind",
192 Self::Name => "name",
193 Self::Location => "location",
194 };
195 Some(PossibleValue::new(name))
196 }
197 }