From 274a6026b4676e33bb8591390cd36f504d0aac08 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 17 Apr 2024 15:01:32 +0200 Subject: [PATCH] its a mess --- src/bench/builtin.rs | 7 ++++++- src/bench/mod.rs | 3 ++- src/bin/bench/cli.rs | 37 ++++++++++++------------------------- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/bench/builtin.rs b/src/bench/builtin.rs index 5afd7a5..50a7fc3 100644 --- a/src/bench/builtin.rs +++ b/src/bench/builtin.rs @@ -8,11 +8,12 @@ use crate::wlist::WordList; use super::{Benchmark, Report}; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct BuiltinBenchmark<'wl, WL: WordList, SL: Solver<'wl, WL>> { solver: SL, builder: GameBuilder<'wl, WL>, report: Arc>, + finished: bool } impl<'wl, WL, SL> Benchmark<'wl, WL, SL> for BuiltinBenchmark<'wl, WL, SL> @@ -38,6 +39,7 @@ where solver, report: Arc::new(RwLock::new(Report::new(builder.build()?))), builder, + finished: false }) } fn solver(&self) -> SL { @@ -60,4 +62,7 @@ where fn report(&'wl self) -> super::Report { self.report.read().expect("lock is poisoned").clone() } + fn is_finished(&self) -> bool { + self.finished + } } diff --git a/src/bench/mod.rs b/src/bench/mod.rs index 489a83c..d8644ab 100644 --- a/src/bench/mod.rs +++ b/src/bench/mod.rs @@ -18,7 +18,7 @@ pub mod builtin; /// Default amount of games to play for a [Benchmark] pub const DEFAULT_N: usize = 50; -pub trait Benchmark<'wl, WL, SL>: Sized + Debug + Sync +pub trait Benchmark<'wl, WL, SL>: Sized + Debug + Sync + Clone where WL: WordList, WL: 'wl, @@ -68,4 +68,5 @@ where // PERF: Somehow returning &Report would be better as we don't need to clone then fn report(&'wl self) -> Report; fn report_shared(&'wl self) -> Arc>; + fn is_finished(&self) -> bool; } diff --git a/src/bin/bench/cli.rs b/src/bin/bench/cli.rs index 7dd32c1..877b2d8 100644 --- a/src/bin/bench/cli.rs +++ b/src/bin/bench/cli.rs @@ -6,6 +6,7 @@ use std::sync::Arc; use clap::Parser; use libpt::log::*; +use tokio; use wordle_analyzer::bench::builtin::BuiltinBenchmark; use wordle_analyzer::bench::report::Report; @@ -64,31 +65,17 @@ async fn main() -> anyhow::Result<()> { let bench = BuiltinBenchmark::build(&wl, solver, builder, cli.threads)?; trace!("{bench:#?}"); - loop { - match tokio::time::timeout( - tokio::time::Duration::from_millis(10), - bench.bench(cli.n), - ) - .await - { - Ok(result) => { - match result { - Ok(final_report) => { - println!("{}", final_report); - } - Err(err) => { - error!("error while benchmarking: {err:#?}"); - } - } - break; - } - Err(_timeout) => { - println!("{}", bench.report()); - } - } - } + let n = cli.n; + let in_progress_bench = tokio::spawn( async move { + bench.bench(n) + }); + + while !bench.is_finished() { + tokio::time::sleep(tokio::time::Duration::from_millis(5000)).await; + println!("{}", bench.report()); + } + in_progress_bench.await; + dbg!(bench.report()); - // FIXME: Rustc thinks wl is borrowed at this point, but it is not!!!!! Or at least it should - // not be Ok(()) }