refactor: remove --time flags in favor of cargo time command (#58)

This commit is contained in:
Felix Spöttel 2023-12-13 11:55:38 +01:00 committed by GitHub
parent 234ac70c4e
commit 335f2631a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 36 deletions

View file

@ -89,10 +89,6 @@ cargo solve <day>
The `solve` command runs your solution against real puzzle inputs. To run an optimized build of your code, append the `--release` flag as with any other rust program. The `solve` command runs your solution against real puzzle inputs. To run an optimized build of your code, append the `--release` flag as with any other rust program.
By default, `solve` executes your code once and shows the execution time. If you append the `--time` flag to the command, the runner will run your code between `10` and `10.000` times (depending on execution time of first execution) and print the average execution time.
For example, running a benchmarked, optimized execution of day 1 would look like `cargo solve 1 --release --time`. Displayed _timings_ show the raw execution time of your solution without overhead like file reads.
#### Submitting solutions #### Submitting solutions
> [!IMPORTANT] > [!IMPORTANT]
@ -116,15 +112,36 @@ 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 and the `--time` flag outputs benchmarks. 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.
### ➡️ Update readme benchmarks ### ➡️ Benchmark your solutions
The template can write benchmark times to the readme via the `cargo time` command. ```sh
# example: `cargo time 8 --store`
cargo time <day> [--all] [--store]
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>`. # output:
# Day 08
# ------
# Part 1: 1 (39.0ns @ 10000 samples)
# Part 2: 2 (39.0ns @ 10000 samples)
#
# Total (Run): 0.00ms
#
# Stored updated benchmarks.
```
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. The `cargo time` command allows you to benchmark your code and store timings in the readme. When benching, the runner will run your code between `10` and `10.000` times, depending on execution time of first execution, and print the average execution time.
`cargo time` has three modes of execution:
1. `cargo time` without arguments incrementally benches solutions that do not have been stored in the readme yet and skips the rest.
2. `cargo time <day>` benches a single solution.
3. `cargo time --all` benches all solutions.
By default, `cargo time` does not write to the readme. In order to do so, append the `--store` flag: `cargo time --store`.
> 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

@ -1,4 +1,3 @@
pub mod template; pub mod template;
// Use this file to add helper functions and additional modules. // Use this file to add helper functions and additional modules.

View file

@ -24,17 +24,16 @@ mod args {
Solve { Solve {
day: Day, day: Day,
release: bool, release: bool,
time: bool,
dhat: bool, dhat: bool,
submit: Option<u8>, submit: Option<u8>,
}, },
All { All {
release: bool, release: bool,
time: bool,
}, },
Time { Time {
all: bool, all: bool,
day: Option<Day>, day: Option<Day>,
store: bool,
}, },
#[cfg(feature = "today")] #[cfg(feature = "today")]
Today, Today,
@ -46,14 +45,15 @@ mod args {
let app_args = match args.subcommand()?.as_deref() { let app_args = match args.subcommand()?.as_deref() {
Some("all") => AppArguments::All { Some("all") => AppArguments::All {
release: args.contains("--release"), release: args.contains("--release"),
time: args.contains("--time"),
}, },
Some("time") => { Some("time") => {
let all = args.contains("--all"); let all = args.contains("--all");
let store = args.contains("--store");
AppArguments::Time { AppArguments::Time {
all, all,
day: args.opt_free_from_str()?, day: args.opt_free_from_str()?,
store,
} }
} }
Some("download") => AppArguments::Download { Some("download") => AppArguments::Download {
@ -70,7 +70,6 @@ mod args {
day: args.free_from_str()?, day: args.free_from_str()?,
release: args.contains("--release"), release: args.contains("--release"),
submit: args.opt_value_from_str("--submit")?, submit: args.opt_value_from_str("--submit")?,
time: args.contains("--time"),
dhat: args.contains("--dhat"), dhat: args.contains("--dhat"),
}, },
#[cfg(feature = "today")] #[cfg(feature = "today")]
@ -101,8 +100,8 @@ fn main() {
std::process::exit(1); std::process::exit(1);
} }
Ok(args) => match args { Ok(args) => match args {
AppArguments::All { release, time } => all::handle(release, time), AppArguments::All { release } => all::handle(release),
AppArguments::Time { day, all } => time::handle(day, all), AppArguments::Time { day, all, store } => time::handle(day, all, store),
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 } => {
@ -114,10 +113,9 @@ fn main() {
AppArguments::Solve { AppArguments::Solve {
day, day,
release, release,
time,
dhat, dhat,
submit, submit,
} => solve::handle(day, release, time, dhat, submit), } => solve::handle(day, release, dhat, submit),
#[cfg(feature = "today")] #[cfg(feature = "today")]
AppArguments::Today => { AppArguments::Today => {
match Day::today() { match Day::today() {

View file

@ -1,5 +1,5 @@
use crate::template::{all_days, run_multi::run_multi}; use crate::template::{all_days, run_multi::run_multi};
pub fn handle(is_release: bool, is_timed: bool) { pub fn handle(is_release: bool) {
run_multi(&all_days().collect(), is_release, is_timed); run_multi(&all_days().collect(), is_release, false);
} }

View file

@ -2,7 +2,7 @@ use std::process::{Command, Stdio};
use crate::template::Day; use crate::template::Day;
pub fn handle(day: Day, release: bool, time: bool, dhat: bool, submit_part: Option<u8>) { pub fn handle(day: Day, release: bool, dhat: bool, submit_part: Option<u8>) {
let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day.to_string()]; let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day.to_string()];
if dhat { if dhat {
@ -23,10 +23,6 @@ pub fn handle(day: Day, release: bool, time: bool, dhat: bool, submit_part: Opti
cmd_args.push(submit_part.to_string()); cmd_args.push(submit_part.to_string());
} }
if time {
cmd_args.push("--time".to_string());
}
let mut cmd = Command::new("cargo") let mut cmd = Command::new("cargo")
.args(&cmd_args) .args(&cmd_args)
.stdout(Stdio::inherit()) .stdout(Stdio::inherit())

View file

@ -4,12 +4,12 @@ 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>, recreate_all: bool) { pub fn handle(day: Option<Day>, run_all: bool, store: bool) {
let stored_timings = Timings::read_from_file(); let stored_timings = Timings::read_from_file();
let days_to_run = day.map_or_else( let days_to_run = day.map_or_else(
|| { || {
if recreate_all { if run_all {
all_days().collect() all_days().collect()
} else { } else {
// when the `--all` 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.
@ -23,6 +23,7 @@ pub fn handle(day: Option<Day>, recreate_all: bool) {
let timings = run_multi(&days_to_run, true, true).unwrap(); let timings = run_multi(&days_to_run, true, true).unwrap();
if store {
let merged_timings = stored_timings.merge(&timings); let merged_timings = stored_timings.merge(&timings);
merged_timings.store_file().unwrap(); merged_timings.store_file().unwrap();
@ -35,4 +36,5 @@ pub fn handle(day: Option<Day>, recreate_all: bool) {
eprintln!("Failed to store updated benchmarks."); eprintln!("Failed to store updated benchmarks.");
} }
} }
}
} }