Skip to content

Commit

Permalink
Set up codspeed benching
Browse files Browse the repository at this point in the history
  • Loading branch information
EbbDrop committed Dec 7, 2024
1 parent 51d3ddb commit ff68c18
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CodSpeed

on:
push:
branches:
- "main"
pull_request:
# `workflow_dispatch` allows CodSpeed to trigger backtest
# performance analysis in order to generate initial data.
workflow_dispatch:

jobs:
benchmarks:
name: Run benchmarks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup rust toolchain, cache and cargo-codspeed binary
uses: moonrepo/setup-rust@v1
with:
cache-target: release
bins: cargo-codspeed, aoc-cli

- name: Download available input files
env:
ADVENT_OF_CODE_SESSION: ${{ secrets.AOC_SESSION }}
run: |
mkdir -p inputs
for i in {1..25}; do
aoc download -I -i input/2024/day$i.txt --year 2024 -d $i || true
done
- name: Build the benchmark target(s)
run: cargo codspeed build

- name: Run the benchmarks
uses: CodSpeedHQ/action@v3
with:
run: cargo codspeed run
token: ${{ secrets.CODSPEED_TOKEN }}
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ tinyvec = "1.8.0"

[dev-dependencies]
criterion = { version = "2.7.2", package = "codspeed-criterion-compat" }
indoc = "2.0.5"
paste = "1.0.15"

[[bench]]
name = "bench_days"
harness = false
41 changes: 41 additions & 0 deletions benches/bench_days.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use criterion::{criterion_group, criterion_main, Criterion};
use paste::paste;

/// Get input for a single day
macro_rules! get_day_input {
($day_num:literal) => {
include_str!(concat!("../input/2024/day", $day_num, ".txt"))
};
}

/// Define benchmarks for a single day with part1 and part2
macro_rules! benches_day {
($day_num:literal) => {
paste! {
use advent_of_code_2024::[<day $day_num>]; // Replace `aoc24` with your crate name

pub fn [<bench_day $day_num>](c: &mut Criterion) {
let mut group = c.benchmark_group(concat!("day", $day_num));
let input = get_day_input!($day_num);
group.bench_function(format!("day{}_part1", $day_num), |b| b.iter(|| [<day $day_num>]::part1(input)));
group.bench_function(format!("day{}_part2", $day_num), |b| b.iter(|| [<day $day_num>]::part2(input)));
}
}
};
}

/// Create benchmarks for included days
macro_rules! benches {
($($day_num:literal),*) => {
paste! {
$(
benches_day!($day_num);
)*

criterion_group!(benches, $([<bench_day $day_num>]),*);
criterion_main!(benches);
}
};
}

benches!(6);

0 comments on commit ff68c18

Please sign in to comment.