Skip to content

Commit a9b912d

Browse files
authored
Allow specifying a day for benchmarks (#19)
1 parent d67a09a commit a9b912d

File tree

4 files changed

+99
-77
lines changed

4 files changed

+99
-77
lines changed

Cargo.lock

Lines changed: 45 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ colored = "2.0.0"
2020
dirs = "3.0.1"
2121

2222
# Optional dependencies
23-
criterion = { version = "0.3.3", optional = true }
23+
criterion = { version = "0.3.4", optional = true }

src/lib.rs

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -68,43 +68,43 @@ macro_rules! base_main {
6868
fn main() {
6969
let mut opt = $crate::args(YEAR).get_matches();
7070

71-
if opt.is_present("bench") {
72-
bench();
73-
} else {
74-
let days: Vec<_> = {
75-
if let Some(opt_days) = opt.values_of("days") {
76-
let opt_days: Vec<_> = opt_days.collect();
77-
let days = parse! { extract_day {}; $( $tail )* };
78-
79-
let ignored_days: Vec<_> = opt_days
80-
.iter()
81-
.filter(|day| !days.contains(&format!("day{}", day).as_str()))
82-
.copied()
83-
.collect();
84-
85-
if !ignored_days.is_empty() {
86-
eprintln!(r"/!\ Ignoring unimplemented days: {}", ignored_days.join(", "));
87-
}
88-
89-
opt_days
90-
.into_iter()
91-
.filter(|day| days.contains(&format!("day{}", day).as_str()))
92-
.collect()
93-
} else if opt.is_present("all") {
94-
parse!(extract_day {}; $( $tail )*)
95-
.iter()
96-
.map(|s| &s[3..])
97-
.collect()
98-
} else {
99-
// Get most recent day, assuming the days are sorted
100-
vec![parse!(extract_day {}; $( $tail )*)
101-
.iter()
102-
.map(|s| &s[3..])
103-
.last()
104-
.expect("No day implemenations found")]
71+
let days: Vec<_> = {
72+
if let Some(opt_days) = opt.values_of("days") {
73+
let opt_days: Vec<_> = opt_days.collect();
74+
let days = parse! { extract_day {}; $( $tail )* };
75+
76+
let ignored_days: Vec<_> = opt_days
77+
.iter()
78+
.filter(|day| !days.contains(&format!("day{}", day).as_str()))
79+
.copied()
80+
.collect();
81+
82+
if !ignored_days.is_empty() {
83+
eprintln!(r"/!\ Ignoring unimplemented days: {}", ignored_days.join(", "));
10584
}
106-
};
10785

86+
opt_days
87+
.into_iter()
88+
.filter(|day| days.contains(&format!("day{}", day).as_str()))
89+
.collect()
90+
} else if opt.is_present("all") {
91+
parse!(extract_day {}; $( $tail )*)
92+
.iter()
93+
.map(|s| &s[3..])
94+
.collect()
95+
} else {
96+
// Get most recent day, assuming the days are sorted
97+
vec![parse!(extract_day {}; $( $tail )*)
98+
.iter()
99+
.map(|s| &s[3..])
100+
.last()
101+
.expect("No day implemenations found")]
102+
}
103+
};
104+
105+
if opt.is_present("bench") {
106+
bench(days);
107+
} else {
108108
if days.len() > 1 && (opt.is_present("stdin") || opt.is_present("file")) {
109109
eprintln!(r"/!\ You are using a personalized output over several days which can");
110110
eprintln!(r" be missleading. If you only intend to run solutions for a");
@@ -130,13 +130,15 @@ macro_rules! main {
130130

131131
use $crate::criterion::Criterion;
132132

133-
fn bench() {
134-
let mut criterion = Criterion::default().configure_from_args();
133+
fn bench(days: Vec<&str>) {
134+
let mut criterion = Criterion::default().with_output_color(true);
135135

136-
parse! {
137-
bench_day { &mut criterion, YEAR };
138-
$( $tail )*
139-
};
136+
for day in days.into_iter() {
137+
parse! {
138+
bench_day { &mut criterion, format!("day{}", day), YEAR };
139+
$( $tail )*
140+
};
141+
}
140142

141143
criterion.final_summary();
142144
}
@@ -149,7 +151,7 @@ macro_rules! main {
149151
( year $year: expr; $( $tail: tt )* ) => {
150152
$crate::base_main! { year $year; $( $tail )* }
151153

152-
fn bench() {
154+
fn bench(days: Vec<&str>) {
153155
println!("Benchmarks not available, please enable `bench` feature for cargo-main.");
154156
}
155157
}

src/parse/gen_bench.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
#[macro_export]
44
macro_rules! bench_day {
55
(
6-
{ $criterion: expr, $year: expr },
6+
{ $criterion: expr, $curr_day: expr, $year: expr },
77
{ day $day: ident { $gen: tt { $( $sol: tt )* } } }
88
) => {{
9-
let day = stringify!($day)[3..].parse().expect("days must be integers");
10-
let data = $crate::input::get_input($year, day).expect("could not fetch input");
11-
let input = $crate::bench_gen!($day, &data, $gen);
9+
if stringify!($day) == $curr_day {
10+
let day = $curr_day[3..].parse().expect("days must be integers");
11+
let data = $crate::input::get_input($year, day).expect("could not fetch input");
12+
let input = $crate::bench_gen!($day, &data, $gen);
1213

13-
let mut group = $criterion.benchmark_group(stringify!($day));
14-
$( $crate::bench_sol!(&mut group, $day, &input, $sol); )+
15-
group.finish();
14+
let mut group = $criterion.benchmark_group(stringify!($day));
15+
$( $crate::bench_sol!(&mut group, $day, &input, $sol); )+
16+
group.finish();
17+
}
1618
}}
1719
}
1820

0 commit comments

Comments
 (0)