Skip to content

Commit 70dac93

Browse files
authored
feat: implement benchmarks (#30)
1 parent d10ec05 commit 70dac93

21 files changed

+988
-562
lines changed

Diff for: .cargo/config.toml

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
[alias]
2-
scaffold = "run --bin scaffold --quiet --release -- "
3-
download = "run --bin download --quiet --release -- "
4-
read = "run --bin read --quiet --release -- "
2+
scaffold = "run --quiet --release -- scaffold"
3+
download = "run --quiet --release -- download"
4+
read = "run --quiet --release -- read"
55

6-
solve = "run --bin solve --quiet --release -- "
7-
all = "run"
6+
solve = "run --quiet --release -- solve"
7+
all = "run --quiet --release -- all"
8+
time = "run --quiet --release -- all --release --time"
89

910
[env]
1011
AOC_YEAR = "2022"

Diff for: .gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ target/
1616

1717
# Advent of Code
1818
# @see https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3
19-
/src/inputs
20-
!/src/inputs/.keep
19+
/data
20+
!/data/inputs/.keep
21+
!/data/examples/.keep
22+
!/data/puzzles/.keep

Diff for: Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,11 @@ default-run = "advent_of_code"
77
publish = false
88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

10+
[lib]
11+
doctest = false
12+
13+
[features]
14+
test_lib = []
15+
1016
[dependencies]
1117
pico-args = "0.5.0"

Diff for: README.md

+37-43
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
66

77
<!--- advent_readme_stars table --->
88

9+
<!--- benchmarking table --->
10+
911
---
1012

1113
## Template setup
@@ -17,7 +19,7 @@ This template supports all major OS (macOS, Linux, Windows).
1719
1. Open [the template repository](https://github.com/fspoettel/advent-of-code-rust) on Github.
1820
2. Click [Use this template](https://github.com/fspoettel/advent-of-code-rust/generate) and create your repository.
1921
3. Clone your repository to your computer.
20-
4. If you are solving a previous year's aoc and want to use the `aoc-cli` integration, change the `AOC_YEAR` variable in `.cargo/config.toml` to reflect that.
22+
4. If you are solving a previous year's advent of code, change the `AOC_YEAR` variable in `.cargo/config.toml` to reflect the year you are solving.
2123

2224
### Setup rust 💻
2325

@@ -38,18 +40,18 @@ This template supports all major OS (macOS, Linux, Windows).
3840
cargo scaffold <day>
3941

4042
# output:
41-
# Created module "src/bin/01.rs"
42-
# Created empty input file "src/inputs/01.txt"
43-
# Created empty example file "src/examples/01.txt"
43+
# Created module file "src/bin/01.rs"
44+
# Created empty input file "data/inputs/01.txt"
45+
# Created empty example file "data/examples/01.txt"
4446
# ---
4547
# 🎄 Type `cargo solve 01` to run your solution.
4648
```
4749

48-
Individual solutions live in the `./src/bin/` directory as separate binaries.
50+
Individual solutions live in the `./src/bin/` directory as separate binaries. _Inputs_ and _examples_ live in the the `./data` directory.
4951

50-
Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/bin/scaffold.rs#L11-L41) has _unit tests_ referencing its _example_ file. Use these unit tests to develop and debug your solution against the example input. For some puzzles, it might be easier to forgo the example file and hardcode inputs into the tests.
52+
Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/bin/scaffold.rs#L11-L41) has _unit tests_ referencing its _example_ file. Use these unit tests to develop and debug your solutions against the example input.
5153

52-
When editing a solution, `rust-analyzer` will display buttons for running / debugging unit tests above the unit test blocks.
54+
Tip: when editing a solution, `rust-analyzer` will display buttons for running / debugging unit tests above the unit test blocks.
5355

5456
### Download input & description for a day
5557

@@ -61,39 +63,32 @@ When editing a solution, `rust-analyzer` will display buttons for running / debu
6163
cargo download <day>
6264

6365
# output:
64-
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
65-
# Fetching puzzle for day 1, 2022...
66-
# Saving puzzle description to "src/puzzles/01.md"...
67-
# Downloading input for day 1, 2022...
68-
# Saving puzzle input to "src/inputs/01.txt"...
69-
# Done!
66+
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
67+
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
68+
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
7069
# ---
71-
# 🎄 Successfully wrote input to "src/inputs/01.txt".
72-
# 🎄 Successfully wrote puzzle to "src/puzzles/01.md".
70+
# 🎄 Successfully wrote input to "data/inputs/01.txt".
71+
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
7372
```
7473

75-
Puzzle descriptions are stored in `src/puzzles` as markdown files. Puzzle inputs are not checked into git. [Reasoning](https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3).
76-
7774
### Run solutions for a day
7875

7976
```sh
8077
# example: `cargo solve 01`
8178
cargo solve <day>
8279

8380
# output:
81+
# Finished dev [unoptimized + debuginfo] target(s) in 0.13s
8482
# Running `target/debug/01`
85-
# 🎄 Part 1 🎄
86-
#
87-
# 6 (elapsed: 37.03µs)
88-
#
89-
# 🎄 Part 2 🎄
90-
#
91-
# 9 (elapsed: 33.18µs)
83+
# Part 1: 42 (166.0ns)
84+
# Part 2: 42 (41.0ns)
9285
```
9386

94-
`solve` is an alias for `cargo run --bin`. To run an optimized version for benchmarking, append the `--release` flag.
87+
The `solve` command runs your solution. If you set the `--release` flag, real puzzle _inputs_ will be passed to your solution, otherwise the _example_ inputs will be used.
88+
89+
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.
9590

96-
Displayed _timings_ show the raw execution time of your solution without overhead (e.g. file reads).
91+
For example, a benchmarked execution against real inputs 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.
9792

9893
#### Submitting solutions
9994

@@ -112,22 +107,21 @@ cargo all
112107
# ----------
113108
# | Day 01 |
114109
# ----------
115-
# 🎄 Part 1 🎄
116-
#
117-
# 0 (elapsed: 170.00µs)
118-
#
119-
# 🎄 Part 2 🎄
120-
#
121-
# 0 (elapsed: 30.00µs)
110+
# Part 1: 42 (19.0ns)
111+
# Part 2: 42 (19.0ns)
122112
# <...other days...>
123113
# Total: 0.20ms
124114
```
125115

126-
`all` is an alias for `cargo run`. To run an optimized version for benchmarking, use the `--release` flag.
116+
This runs all solutions sequentially and prints output to the command-line. Same as for the `solve` command, `--release` controls whether real inputs will be used.
127117

128-
_Total timing_ is computed from individual solution _timings_ and excludes as much overhead as possible.
118+
#### Update readme benchmarks
129119

130-
### Run all solutions against the example input
120+
The template can output a table with solution times to your readme. Please note that these are not "scientific" benchmarks, understand them as a fun approximation. 😉
121+
122+
In order to generate a benchmarking table, run `cargo all --release --time`. If everything goes well, the command will output "_Successfully updated README with benchmarks._" after the execution finishes.
123+
124+
### Run all tests
131125

132126
```sh
133127
cargo test
@@ -148,6 +142,13 @@ cargo clippy
148142
```
149143
## Optional template features
150144

145+
### Download puzzle inputs via aoc-cli
146+
147+
1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0`
148+
2. Create an `.adventofcode.session` file in your home directory and paste your session cookie. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in _Cookies_ under the _Application_ or _Storage_ tab, and copy out the `session` cookie value. [^1]
149+
150+
Once installed, you can use the [download command](#download-input--description-for-a-day).
151+
151152
### Read puzzle description in terminal
152153

153154
> **Note**
@@ -163,13 +164,6 @@ cargo read <day>
163164
# ...the input...
164165
```
165166

166-
### Download puzzle inputs via aoc-cli
167-
168-
1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0`
169-
2. Create an `.adventofcode.session` file in your home directory and paste your session cookie[^1] into it. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in your Cookies under the Application or Storage tab, and copy out the `session` cookie value.
170-
171-
Once installed, you can use the [download command](#download-input--description-for-a-day).
172-
173167
### Check code formatting in CI
174168

175169
Uncomment the `format` job in the `ci.yml` workflow to enable fmt checks in CI.

Diff for: src/bin/.keep

Whitespace-only changes.

Diff for: src/bin/download.rs

-44
This file was deleted.

Diff for: src/bin/read.rs

-44
This file was deleted.

Diff for: src/bin/solve.rs

-58
This file was deleted.

Diff for: src/helpers.rs

-4
This file was deleted.

0 commit comments

Comments
 (0)