From 335f2631a0bd17c99560fb61ce0e7624355e321d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Wed, 13 Dec 2023 11:55:38 +0100 Subject: [PATCH] refactor: remove `--time` flags in favor of `cargo time` command (#58) --- README.md | 35 +++++++++++++++++++++++++--------- src/lib.rs | 1 - src/main.rs | 14 ++++++-------- src/template/commands/all.rs | 4 ++-- src/template/commands/solve.rs | 6 +----- src/template/commands/time.rs | 24 ++++++++++++----------- 6 files changed, 48 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index e5548cf..a155efa 100644 --- a/README.md +++ b/README.md @@ -89,10 +89,6 @@ cargo solve 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 > [!IMPORTANT] @@ -116,15 +112,36 @@ 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 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. + +### ➡️ Benchmark your solutions + +```sh +# example: `cargo time 8 --store` +cargo time [--all] [--store] + +# output: +# Day 08 +# ------ +# Part 1: 1 (39.0ns @ 10000 samples) +# Part 2: 2 (39.0ns @ 10000 samples) +# +# Total (Run): 0.00ms +# +# Stored updated benchmarks. +``` + +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. -### ➡️ Update readme benchmarks +`cargo time` has three modes of execution: -The template can write benchmark times to the readme via the `cargo time` command. + 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 ` benches a single solution. + 3. `cargo time --all` benches all solutions. -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 `. +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. +> 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 diff --git a/src/lib.rs b/src/lib.rs index 72e8b28..27d7df8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ pub mod template; // Use this file to add helper functions and additional modules. - diff --git a/src/main.rs b/src/main.rs index 57d4fe3..9322423 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,17 +24,16 @@ mod args { Solve { day: Day, release: bool, - time: bool, dhat: bool, submit: Option, }, All { release: bool, - time: bool, }, Time { all: bool, day: Option, + store: bool, }, #[cfg(feature = "today")] Today, @@ -46,14 +45,15 @@ mod args { let app_args = match args.subcommand()?.as_deref() { Some("all") => AppArguments::All { release: args.contains("--release"), - time: args.contains("--time"), }, Some("time") => { let all = args.contains("--all"); + let store = args.contains("--store"); AppArguments::Time { all, day: args.opt_free_from_str()?, + store, } } Some("download") => AppArguments::Download { @@ -70,7 +70,6 @@ mod args { day: args.free_from_str()?, release: args.contains("--release"), submit: args.opt_value_from_str("--submit")?, - time: args.contains("--time"), dhat: args.contains("--dhat"), }, #[cfg(feature = "today")] @@ -101,8 +100,8 @@ fn main() { std::process::exit(1); } Ok(args) => match args { - AppArguments::All { release, time } => all::handle(release, time), - AppArguments::Time { day, all } => time::handle(day, all), + AppArguments::All { release } => all::handle(release), + AppArguments::Time { day, all, store } => time::handle(day, all, store), AppArguments::Download { day } => download::handle(day), AppArguments::Read { day } => read::handle(day), AppArguments::Scaffold { day, download } => { @@ -114,10 +113,9 @@ fn main() { AppArguments::Solve { day, release, - time, dhat, submit, - } => solve::handle(day, release, time, dhat, submit), + } => solve::handle(day, release, dhat, submit), #[cfg(feature = "today")] AppArguments::Today => { match Day::today() { diff --git a/src/template/commands/all.rs b/src/template/commands/all.rs index 151692b..b844e1e 100644 --- a/src/template/commands/all.rs +++ b/src/template/commands/all.rs @@ -1,5 +1,5 @@ use crate::template::{all_days, run_multi::run_multi}; -pub fn handle(is_release: bool, is_timed: bool) { - run_multi(&all_days().collect(), is_release, is_timed); +pub fn handle(is_release: bool) { + run_multi(&all_days().collect(), is_release, false); } diff --git a/src/template/commands/solve.rs b/src/template/commands/solve.rs index 66771c0..ec92a6f 100644 --- a/src/template/commands/solve.rs +++ b/src/template/commands/solve.rs @@ -2,7 +2,7 @@ use std::process::{Command, Stdio}; use crate::template::Day; -pub fn handle(day: Day, release: bool, time: bool, dhat: bool, submit_part: Option) { +pub fn handle(day: Day, release: bool, dhat: bool, submit_part: Option) { let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day.to_string()]; 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()); } - if time { - cmd_args.push("--time".to_string()); - } - let mut cmd = Command::new("cargo") .args(&cmd_args) .stdout(Stdio::inherit()) diff --git a/src/template/commands/time.rs b/src/template/commands/time.rs index efbafbe..49b91a8 100644 --- a/src/template/commands/time.rs +++ b/src/template/commands/time.rs @@ -4,12 +4,12 @@ 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, recreate_all: bool) { +pub fn handle(day: Option, run_all: bool, store: bool) { let stored_timings = Timings::read_from_file(); let days_to_run = day.map_or_else( || { - if recreate_all { + if run_all { all_days().collect() } else { // when the `--all` flag is not set, filter out days that are fully benched. @@ -23,16 +23,18 @@ pub fn handle(day: Option, recreate_all: bool) { let timings = run_multi(&days_to_run, true, true).unwrap(); - let merged_timings = stored_timings.merge(&timings); - merged_timings.store_file().unwrap(); + if store { + let merged_timings = stored_timings.merge(&timings); + merged_timings.store_file().unwrap(); - println!(); - match readme_benchmarks::update(merged_timings) { - Ok(()) => { - println!("Stored updated benchmarks."); - } - Err(_) => { - eprintln!("Failed to store updated benchmarks."); + println!(); + match readme_benchmarks::update(merged_timings) { + Ok(()) => { + println!("Stored updated benchmarks."); + } + Err(_) => { + eprintln!("Failed to store updated benchmarks."); + } } } }