more bench and report impls
cargo devel CI / cargo CI (push) Successful in 1m27s Details

This commit is contained in:
Christoph J. Scherr 2024-04-04 21:07:52 +02:00
parent 84d4afdb18
commit 2ae53bc464
Signed by: PlexSheep
GPG Key ID: 7CDD0B14851A08EF
4 changed files with 42 additions and 9 deletions

View File

@ -24,6 +24,7 @@ serde = ["dep:serde"]
[dependencies] [dependencies]
anyhow = "1.0.81" anyhow = "1.0.81"
chrono = { version = "0.4.37" }
clap = { version = "4.5.3", features = ["derive"], optional = true } clap = { version = "4.5.3", features = ["derive"], optional = true }
colored = { version = "2.1.0", optional = false } colored = { version = "2.1.0", optional = false }
libpt = "0.4.2" libpt = "0.4.2"
@ -31,6 +32,7 @@ rand = "0.8.5"
regex = "1.10.3" regex = "1.10.3"
serde = { version = "1.0.197", optional = true, features = ["serde_derive"] } serde = { version = "1.0.197", optional = true, features = ["serde_derive"] }
serde_json = { version = "1.0.114", optional = true } serde_json = { version = "1.0.114", optional = true }
# serde_with = "3.7.0"
thiserror = "1.0.58" thiserror = "1.0.58"
[[bin]] [[bin]]

View File

@ -11,8 +11,12 @@ pub struct BuiltinBenchmark<'wl, WL: WordList, SL: Solver<'wl, WL>> {
builder: GameBuilder<'wl, WL>, builder: GameBuilder<'wl, WL>,
} }
impl<'wl, WL: WordList, SL: Solver<'wl, WL>> Benchmark<'wl, WL, SL> impl<'wl, WL, SL> Benchmark<'wl, WL, SL> for BuiltinBenchmark<'wl, WL, SL>
for BuiltinBenchmark<'wl, WL, SL> where
WL: WordList,
WL: 'wl,
SL: Solver<'wl, WL>,
SL: 'wl,
{ {
fn build(wordlist: &'wl WL, solver: SL) -> crate::error::WResult<Self> { fn build(wordlist: &'wl WL, solver: SL) -> crate::error::WResult<Self> {
let builder: GameBuilder<_> = Game::builder(wordlist); let builder: GameBuilder<_> = Game::builder(wordlist);

View File

@ -19,6 +19,7 @@ where
WL: WordList, WL: WordList,
WL: 'wl, WL: 'wl,
SL: Solver<'wl, WL>, SL: Solver<'wl, WL>,
SL: 'wl,
{ {
fn build(wordlist: &'wl WL, solver: SL) -> WResult<Self>; fn build(wordlist: &'wl WL, solver: SL) -> WResult<Self>;
fn builder(&'wl self) -> &'wl GameBuilder<'wl, WL>; fn builder(&'wl self) -> &'wl GameBuilder<'wl, WL>;
@ -27,16 +28,12 @@ where
} }
fn solver(&'wl self) -> &'wl SL; fn solver(&'wl self) -> &'wl SL;
fn play(&'wl self) -> WResult<GuessResponse> { fn play(&'wl self) -> WResult<GuessResponse> {
// FIXME: wth why does the lifetime break here? self.solver().play(&mut self.make_game()?)
let mut game: Game<'wl, WL> = self.make_game()?;
todo!()
} }
fn bench(&'wl self, n: usize) -> WResult<Report> { fn bench(&'wl self, n: usize) -> WResult<Report> {
// PERF: it would be better to make this multithreaded // PERF: it would be better to make this multithreaded
let part = n / 20; let part = n / 20;
let mut report = Report::new(); let mut report = Report::new();
let start = std::time::Instant::now();
for i in 0..n { for i in 0..n {
// TODO: limit execution time for the following, perhaps async // TODO: limit execution time for the following, perhaps async
@ -46,6 +43,8 @@ where
} }
} }
report.finalize();
Ok(report) Ok(report)
} }
} }

View File

@ -1,3 +1,4 @@
use chrono::{self, NaiveDateTime, NaiveTime, TimeDelta};
use std::fmt::Display; use std::fmt::Display;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
@ -6,14 +7,24 @@ use serde::{Deserialize, Serialize};
use crate::game::response::GuessResponse; use crate::game::response::GuessResponse;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Report { pub struct Report {
data: Vec<GuessResponse>, data: Vec<GuessResponse>,
start: NaiveDateTime,
end: Option<NaiveDateTime>,
benchtime: Option<TimeDelta>,
/// is the benchmark finished?
finished: bool,
} }
impl Report { impl Report {
pub fn new() -> Self { pub fn new() -> Self {
Self { data: Vec::new() } Self {
data: Vec::new(),
start: chrono::Local::now().naive_local(),
benchtime: None,
end: None,
finished: false,
}
} }
pub fn add(&mut self, data: GuessResponse) { pub fn add(&mut self, data: GuessResponse) {
self.data.push(data) self.data.push(data)
@ -30,6 +41,23 @@ impl Report {
pub fn avg_score(&self) -> usize { pub fn avg_score(&self) -> usize {
todo!() todo!()
} }
/// finalize the record
///
/// Sets the [benchtime](Report::benchtime) and [over](Report::over). In future versions, this
/// method might be used to precompute statistical information from the data.
pub(crate) fn finalize(&mut self) {
self.end = Some(chrono::Local::now().naive_local());
self.benchtime = Some(self.end.unwrap() - self.start);
self.finished = true;
}
/// is the report finished?
///
/// Will be true after the [benchmark][super::Benchmark] is done.
pub fn finished(&self) -> bool {
self.finished
}
} }
impl Display for Report { impl Display for Report {