diff --git a/2022/src/bin/day1.rs b/2022/src/bin/day1.rs index 49df3b5..2b22da4 100644 --- a/2022/src/bin/day1.rs +++ b/2022/src/bin/day1.rs @@ -1,3 +1,4 @@ +#![feature(test)] use anyhow::Result; use aoc::Solver; @@ -26,6 +27,19 @@ mod tests { fn part2_solution() -> Result<()> { Day::test(aoc::Part::TWO, "input", 206582) } + + // Benchmarks + extern crate test; + #[bench] + #[ignore] + fn part1_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::ONE, b) + } + #[bench] + #[ignore] + fn part2_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::TWO, b) + } } // -- Solution -- diff --git a/2022/src/bin/day2.rs b/2022/src/bin/day2.rs index a6e24ee..64fcf4f 100644 --- a/2022/src/bin/day2.rs +++ b/2022/src/bin/day2.rs @@ -1,3 +1,4 @@ +#![feature(test)] use anyhow::Result; use aoc::Solver; @@ -26,6 +27,19 @@ mod tests { fn part2_solution() -> Result<()> { Day::test(aoc::Part::TWO, "input", 12382) } + + // Benchmarks + extern crate test; + #[bench] + #[ignore] + fn part1_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::ONE, b) + } + #[bench] + #[ignore] + fn part2_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::TWO, b) + } } // -- Implementation for hand -- diff --git a/2022/src/bin/day3.rs b/2022/src/bin/day3.rs index 7d810dd..b1718ea 100644 --- a/2022/src/bin/day3.rs +++ b/2022/src/bin/day3.rs @@ -1,3 +1,4 @@ +#![feature(test)] use anyhow::Result; use aoc::Solver; @@ -26,6 +27,19 @@ mod tests { fn part2_solution() -> Result<()> { Day::test(aoc::Part::TWO, "input", 2708) } + + // Benchmarks + extern crate test; + #[bench] + #[ignore] + fn part1_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::ONE, b) + } + #[bench] + #[ignore] + fn part2_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::TWO, b) + } } // -- Helpers -- diff --git a/2022/src/bin/day4.rs b/2022/src/bin/day4.rs index 35cd4d8..dadc083 100644 --- a/2022/src/bin/day4.rs +++ b/2022/src/bin/day4.rs @@ -1,3 +1,4 @@ +#![feature(test)] use std::cmp; use anyhow::Result; use aoc::Solver; @@ -27,6 +28,19 @@ mod tests { fn part2_solution() -> Result<()> { Day::test(aoc::Part::TWO, "input", 907) } + + // Benchmarks + extern crate test; + #[bench] + #[ignore] + fn part1_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::ONE, b) + } + #[bench] + #[ignore] + fn part2_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::TWO, b) + } } // -- Implementation -- diff --git a/2022/src/bin/day5.rs b/2022/src/bin/day5.rs index 00eaacc..ddc4838 100644 --- a/2022/src/bin/day5.rs +++ b/2022/src/bin/day5.rs @@ -1,3 +1,4 @@ +#![feature(test)] use core::fmt; use std::collections::HashMap; use lazy_static::lazy_static; @@ -31,6 +32,19 @@ mod tests { fn part2_solution() -> Result<()> { Day::test(aoc::Part::TWO, "input", "CNSFCGJSM".to_string()) } + + // Benchmarks + extern crate test; + #[bench] + #[ignore] + fn part1_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::ONE, b) + } + #[bench] + #[ignore] + fn part2_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::TWO, b) + } } // -- Implementation -- diff --git a/2022/src/lib.rs b/2022/src/lib.rs index be75821..1d2e041 100644 --- a/2022/src/lib.rs +++ b/2022/src/lib.rs @@ -1,3 +1,6 @@ +#![feature(test)] +extern crate test; + use core::fmt; use std::{fs, fmt::Debug}; @@ -15,18 +18,21 @@ pub trait Solver { fn part1(input: &str) -> Self::Output; fn part2(input: &str) -> Self::Output; - fn test(part: Part, name: &str, result: Self::Output) -> Result<()> { - // Select the right function - let fun = match part { + fn select(part: Part) -> for<'a> fn(&'a str) -> ::Output { + match part { Part::ONE => Self::part1, Part::TWO => Self::part2, - }; + } + } + + fn test(part: Part, name: &str, result: Self::Output) -> Result<()> { + // Select the right function // Read the test input let input = fs::read_to_string(format!("input/{}/{name}", Self::day())).with_context(|| format!("Failed to read '{}' for day {}", name, Self::day()))?; // Assert that the result matches the expected value - assert_eq!(fun(&input), result); + assert_eq!(Self::select(part)(&input), result); Ok(()) } @@ -38,4 +44,13 @@ pub trait Solver { Ok(()) } + + fn benchmark(part: Part, b: &mut test::Bencher) { + let f = Self::select(part); + + b.iter(|| { + let input = fs::read_to_string(format!("input/{}/input", Self::day())).unwrap(); + f(&input) + }); + } } diff --git a/2022/template.rs b/2022/template.rs index f937e62..f870988 100644 --- a/2022/template.rs +++ b/2022/template.rs @@ -1,3 +1,4 @@ +#![feature(test)] use anyhow::Result; use aoc::Solver; @@ -14,6 +15,19 @@ mod tests { fn part1_test1() -> Result<()> { Day::test(aoc::Part::ONE, "test-1", TEST) } + + // Benchmarks + extern crate test; + #[bench] + #[ignore] + fn part1_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::ONE, b) + } + #[bench] + #[ignore] + fn part2_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::TWO, b) + } } // -- Solution --