Skip to content

Commit

Permalink
fix(review): address review feedback
Browse files Browse the repository at this point in the history
Co-authored-by: Tristan Guichaoua <[email protected]>
  • Loading branch information
fspoettel and tguichaoua committed Dec 10, 2023
1 parent 28252e2 commit 0043528
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 24 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ cargo all
# 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

Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ mod args {
time: bool,
},
Time {
all: bool,
day: Option<Day>,
force: bool,
},
#[cfg(feature = "today")]
Today,
Expand All @@ -49,10 +49,10 @@ mod args {
time: args.contains("--time"),
},
Some("time") => {
let force = args.contains("--force");
let all = args.contains("--all");

AppArguments::Time {
force,
all,
day: args.opt_free_from_str()?,
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ fn main() {
}
Ok(args) => match args {
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::Read { day } => read::handle(day),
AppArguments::Scaffold { day, download } => {
Expand Down
11 changes: 3 additions & 8 deletions src/template/commands/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::template::run_multi::run_multi;
use crate::template::timings::Timings;
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 mut days_to_run = HashSet::new();
Expand All @@ -15,13 +15,8 @@ pub fn handle(day: Option<Day>, force: bool) {
}
None => {
all_days().for_each(|day| {
// when the force flag is not set, filter out days that are fully benched.
if force
|| !stored_timings
.data
.iter()
.any(|t| t.day == day && t.part_1.is_some() && t.part_2.is_some())
{
// when the `--all` flag is not set, filter out days that are fully benched.
if recreate_all || !stored_timings.is_day_complete(&day) {
days_to_run.insert(day);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/template/run_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
};

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| {
if day > 1 {
Expand Down
66 changes: 60 additions & 6 deletions src/template/timings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ impl Timings {
/// Dehydrate timings to a JSON file.
pub fn store_file(&self) -> Result<(), Error> {
let json = JsonValue::from(self.clone());
let mut bytes = vec![];
json.format_to(&mut bytes)?;
fs::write(TIMINGS_FILE_PATH, bytes)
let mut file = fs::File::create(TIMINGS_FILE_PATH)?;
json.format_to(&mut file)
}

/// Rehydrate timings from a JSON file. If not present, returns empty timings.
Expand Down Expand Up @@ -67,6 +66,12 @@ impl Timings {
pub fn total_millis(&self) -> 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())
}
}

/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -101,8 +106,8 @@ impl TryFrom<String> for Timings {
Ok(Timings {
data: json_data
.iter()
.map(|value| Timing::try_from(value).unwrap())
.collect(),
.map(Timing::try_from)
.collect::<Result<_, _>>()?,
})
}
}
Expand Down Expand Up @@ -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::{
day,
template::timings::{Timing, Timings},
Expand Down

0 comments on commit 0043528

Please sign in to comment.