fix(review): address review feedback

Co-authored-by: Tristan Guichaoua <33934311+tguichaoua@users.noreply.github.com>
This commit is contained in:
Felix Spöttel 2023-12-10 12:56:25 +01:00
parent 28252e21e8
commit 00435287c9
5 changed files with 73 additions and 24 deletions

View file

@ -116,15 +116,15 @@ cargo all
# Total: 0.20ms # Total: 0.20ms
``` ```
This runs all solutions sequentially and prints output to the command-line. Same as for the `solve` command, the `--release` flag runs an optimized build. This runs all solutions sequentially and prints output to the command-line. Same as for the `solve` command, the `--release` flag runs an optimized build and the `--time` flag outputs benchmarks.
#### Update readme benchmarks ### ➡️ Update readme benchmarks
The template can output a table with solution times to your readme. The template can write benchmark times to the README via the `cargo time` command.
In order to generate the benchmarking table, run `cargo time`. By default, this command checks for missing benchmarks, runs those solutions, and then updates the table. If you want to (re-)time all solutions, run `cargo time --force` flag. If you want to (re-)time a specific solution, run `cargo time <day>`. By default, this command checks for missing benchmarks, runs those solutions, and then updates the table. If you want to (re-)time all solutions, run `cargo time --all`. If you want to (re-)time one specific solution, run `cargo time <day>`.
Please note that these are not "scientific" benchmarks, understand them as a fun approximation. 😉 Timings, especially in the microseconds range, might change a bit between invocations. Please note that these are not _scientific_ benchmarks, understand them as a fun approximation. 😉 Timings, especially in the microseconds range, might change a bit between invocations.
### ➡️ Run all tests ### ➡️ Run all tests

View file

@ -33,8 +33,8 @@ mod args {
time: bool, time: bool,
}, },
Time { Time {
all: bool,
day: Option<Day>, day: Option<Day>,
force: bool,
}, },
#[cfg(feature = "today")] #[cfg(feature = "today")]
Today, Today,
@ -49,10 +49,10 @@ mod args {
time: args.contains("--time"), time: args.contains("--time"),
}, },
Some("time") => { Some("time") => {
let force = args.contains("--force"); let all = args.contains("--all");
AppArguments::Time { AppArguments::Time {
force, all,
day: args.opt_free_from_str()?, day: args.opt_free_from_str()?,
} }
} }
@ -102,7 +102,7 @@ fn main() {
} }
Ok(args) => match args { Ok(args) => match args {
AppArguments::All { release, time } => all::handle(release, time), AppArguments::All { release, time } => all::handle(release, time),
AppArguments::Time { day, force } => time::handle(day, force), AppArguments::Time { day, all } => time::handle(day, all),
AppArguments::Download { day } => download::handle(day), AppArguments::Download { day } => download::handle(day),
AppArguments::Read { day } => read::handle(day), AppArguments::Read { day } => read::handle(day),
AppArguments::Scaffold { day, download } => { AppArguments::Scaffold { day, download } => {

View file

@ -4,7 +4,7 @@ use crate::template::run_multi::run_multi;
use crate::template::timings::Timings; use crate::template::timings::Timings;
use crate::template::{all_days, readme_benchmarks, Day}; use crate::template::{all_days, readme_benchmarks, Day};
pub fn handle(day: Option<Day>, force: bool) { pub fn handle(day: Option<Day>, recreate_all: bool) {
let stored_timings = Timings::read_from_file(); let stored_timings = Timings::read_from_file();
let mut days_to_run = HashSet::new(); let mut days_to_run = HashSet::new();
@ -15,13 +15,8 @@ pub fn handle(day: Option<Day>, force: bool) {
} }
None => { None => {
all_days().for_each(|day| { all_days().for_each(|day| {
// when the force flag is not set, filter out days that are fully benched. // when the `--all` flag is not set, filter out days that are fully benched.
if force if recreate_all || !stored_timings.is_day_complete(&day) {
|| !stored_timings
.data
.iter()
.any(|t| t.day == day && t.part_1.is_some() && t.part_2.is_some())
{
days_to_run.insert(day); days_to_run.insert(day);
} }
}); });

View file

@ -8,7 +8,7 @@ use super::{
}; };
pub fn run_multi(days_to_run: HashSet<Day>, is_release: bool, is_timed: bool) -> Option<Timings> { pub fn run_multi(days_to_run: HashSet<Day>, is_release: bool, is_timed: bool) -> Option<Timings> {
let mut timings: Vec<Timing> = vec![]; let mut timings: Vec<Timing> = Vec::with_capacity(days_to_run.len());
all_days().for_each(|day| { all_days().for_each(|day| {
if day > 1 { if day > 1 {

View file

@ -25,9 +25,8 @@ impl Timings {
/// Dehydrate timings to a JSON file. /// Dehydrate timings to a JSON file.
pub fn store_file(&self) -> Result<(), Error> { pub fn store_file(&self) -> Result<(), Error> {
let json = JsonValue::from(self.clone()); let json = JsonValue::from(self.clone());
let mut bytes = vec![]; let mut file = fs::File::create(TIMINGS_FILE_PATH)?;
json.format_to(&mut bytes)?; json.format_to(&mut file)
fs::write(TIMINGS_FILE_PATH, bytes)
} }
/// Rehydrate timings from a JSON file. If not present, returns empty timings. /// Rehydrate timings from a JSON file. If not present, returns empty timings.
@ -67,6 +66,12 @@ impl Timings {
pub fn total_millis(&self) -> f64 { pub fn total_millis(&self) -> f64 {
self.data.iter().map(|x| x.total_nanos).sum::<f64>() / 1_000_000_f64 self.data.iter().map(|x| x.total_nanos).sum::<f64>() / 1_000_000_f64
} }
pub fn is_day_complete(&self, day: &Day) -> bool {
self.data
.iter()
.any(|t| &t.day == day && t.part_1.is_some() && t.part_2.is_some())
}
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -101,8 +106,8 @@ impl TryFrom<String> for Timings {
Ok(Timings { Ok(Timings {
data: json_data data: json_data
.iter() .iter()
.map(|value| Timing::try_from(value).unwrap()) .map(Timing::try_from)
.collect(), .collect::<Result<_, _>>()?,
}) })
} }
} }
@ -270,7 +275,56 @@ mod tests {
} }
} }
mod helpers { mod is_day_complete {
use crate::{
day,
template::timings::{Timing, Timings},
};
#[test]
fn handles_completed_days() {
let timings = Timings {
data: vec![Timing {
day: day!(1),
part_1: Some("1ms".into()),
part_2: Some("2ms".into()),
total_nanos: 3_000_000_000_f64,
}],
};
assert_eq!(timings.is_day_complete(&day!(1)), true);
}
#[test]
fn handles_partial_days() {
let timings = Timings {
data: vec![Timing {
day: day!(1),
part_1: Some("1ms".into()),
part_2: None,
total_nanos: 1_000_000_000_f64,
}],
};
assert_eq!(timings.is_day_complete(&day!(1)), false);
}
#[test]
fn handles_uncompleted_days() {
let timings = Timings {
data: vec![Timing {
day: day!(1),
part_1: None,
part_2: None,
total_nanos: 0.0,
}],
};
assert_eq!(timings.is_day_complete(&day!(1)), false);
}
}
mod merge {
use crate::{ use crate::{
day, day,
template::timings::{Timing, Timings}, template::timings::{Timing, Timings},