Added benchmarks

This commit is contained in:
Dreaded_X 2022-12-06 02:17:08 +01:00
parent ee9dcc92c1
commit c75da6c077
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
7 changed files with 104 additions and 5 deletions

View File

@ -1,3 +1,4 @@
#![feature(test)]
use anyhow::Result; use anyhow::Result;
use aoc::Solver; use aoc::Solver;
@ -26,6 +27,19 @@ mod tests {
fn part2_solution() -> Result<()> { fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 206582) 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 -- // -- Solution --

View File

@ -1,3 +1,4 @@
#![feature(test)]
use anyhow::Result; use anyhow::Result;
use aoc::Solver; use aoc::Solver;
@ -26,6 +27,19 @@ mod tests {
fn part2_solution() -> Result<()> { fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 12382) 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 -- // -- Implementation for hand --

View File

@ -1,3 +1,4 @@
#![feature(test)]
use anyhow::Result; use anyhow::Result;
use aoc::Solver; use aoc::Solver;
@ -26,6 +27,19 @@ mod tests {
fn part2_solution() -> Result<()> { fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 2708) 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 -- // -- Helpers --

View File

@ -1,3 +1,4 @@
#![feature(test)]
use std::cmp; use std::cmp;
use anyhow::Result; use anyhow::Result;
use aoc::Solver; use aoc::Solver;
@ -27,6 +28,19 @@ mod tests {
fn part2_solution() -> Result<()> { fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 907) 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 -- // -- Implementation --

View File

@ -1,3 +1,4 @@
#![feature(test)]
use core::fmt; use core::fmt;
use std::collections::HashMap; use std::collections::HashMap;
use lazy_static::lazy_static; use lazy_static::lazy_static;
@ -31,6 +32,19 @@ mod tests {
fn part2_solution() -> Result<()> { fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", "CNSFCGJSM".to_string()) 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 -- // -- Implementation --

View File

@ -1,3 +1,6 @@
#![feature(test)]
extern crate test;
use core::fmt; use core::fmt;
use std::{fs, fmt::Debug}; use std::{fs, fmt::Debug};
@ -15,18 +18,21 @@ pub trait Solver {
fn part1(input: &str) -> Self::Output; fn part1(input: &str) -> Self::Output;
fn part2(input: &str) -> Self::Output; fn part2(input: &str) -> Self::Output;
fn test(part: Part, name: &str, result: Self::Output) -> Result<()> { fn select(part: Part) -> for<'a> fn(&'a str) -> <Self as Solver>::Output {
// Select the right function match part {
let fun = match part {
Part::ONE => Self::part1, Part::ONE => Self::part1,
Part::TWO => Self::part2, Part::TWO => Self::part2,
}; }
}
fn test(part: Part, name: &str, result: Self::Output) -> Result<()> {
// Select the right function
// Read the test input // 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()))?; 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 that the result matches the expected value
assert_eq!(fun(&input), result); assert_eq!(Self::select(part)(&input), result);
Ok(()) Ok(())
} }
@ -38,4 +44,13 @@ pub trait Solver {
Ok(()) 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)
});
}
} }

View File

@ -1,3 +1,4 @@
#![feature(test)]
use anyhow::Result; use anyhow::Result;
use aoc::Solver; use aoc::Solver;
@ -14,6 +15,19 @@ mod tests {
fn part1_test1() -> Result<()> { fn part1_test1() -> Result<()> {
Day::test(aoc::Part::ONE, "test-1", TEST) 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 -- // -- Solution --