From 0cd46eb2debd947d50989024cc57ac45269cee17 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Thu, 18 Apr 2024 14:59:53 +0200 Subject: [PATCH] i think i have decided on the api --- Cargo.toml | 1 - src/bench/builtin.rs | 5 +++++ src/bench/mod.rs | 5 ++--- src/bin/bench/cli.rs | 21 +++++++++------------ 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f5a7ccf..4e6d0de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,6 @@ serde = { version = "1.0.197", optional = true, features = ["serde_derive"] } serde_json = { version = "1.0.114", optional = true } # serde_with = "3.7.0" thiserror = "1.0.58" -tokio = { version = "1.37.0", features = ["macros", "rt", "rt-multi-thread", "time"] } [[bin]] name = "wordlec" diff --git a/src/bench/builtin.rs b/src/bench/builtin.rs index 50a7fc3..2838e37 100644 --- a/src/bench/builtin.rs +++ b/src/bench/builtin.rs @@ -2,6 +2,7 @@ use std::sync::{Arc, RwLock}; use libpt::log::info; +use crate::error::WResult; use crate::game::{self, GameBuilder}; use crate::solve::Solver; use crate::wlist::WordList; @@ -65,4 +66,8 @@ where fn is_finished(&self) -> bool { self.finished } + fn start(&self) -> WResult<()> { + todo!(); + Ok(()) + } } diff --git a/src/bench/mod.rs b/src/bench/mod.rs index d8644ab..4e9e8de 100644 --- a/src/bench/mod.rs +++ b/src/bench/mod.rs @@ -44,9 +44,7 @@ where // TODO: add some interface to get reports while the benchmark runs // TODO: make the benchmark optionally multithreaded // NOTE: This is blocking, use start to let it run in another thread - // HACK: Shame on me, but I cannot find out how to share the data over threads, so that we can - // make this a start function and just poll records while rayon does the benching. - async fn bench(&'wl self, n: usize) -> WResult { + fn bench(&'wl self, n: usize) -> WResult { let report = self.report_shared(); let this = std::sync::Arc::new(self); @@ -68,5 +66,6 @@ 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 start(&self) -> WResult<()>; fn is_finished(&self) -> bool; } diff --git a/src/bin/bench/cli.rs b/src/bin/bench/cli.rs index 3c5f265..9dd234a 100644 --- a/src/bin/bench/cli.rs +++ b/src/bin/bench/cli.rs @@ -2,9 +2,10 @@ // #![warn(missing_docs)] #![warn(missing_debug_implementations)] +use std::thread::sleep_ms; + use clap::Parser; use libpt::log::*; -use tokio; use wordle_analyzer::bench::builtin::BuiltinBenchmark; use wordle_analyzer::bench::report::Report; @@ -44,8 +45,7 @@ struct Cli { threads: usize, } -#[tokio::main] -async fn main() -> anyhow::Result<()> { +fn main() -> anyhow::Result<()> { let cli = Cli::parse(); if cli.verbose { Logger::build_mini(Some(Level::DEBUG))?; @@ -63,18 +63,15 @@ async fn main() -> anyhow::Result<()> { let bench = BuiltinBenchmark::build(&wl, solver, builder, cli.threads)?; trace!("{bench:#?}"); - let n = cli.n; - let bench2 = bench.clone(); - let in_progress_bench = tokio::spawn(async move { - bench2.bench(n).await - }); + bench.start()?; - while !bench.is_finished() { - tokio::time::sleep(tokio::time::Duration::from_millis(5000)).await; + loop { + sleep_ms(1000); println!("{}", bench.report()); + if bench.is_finished() { + break; + } } - in_progress_bench.await; - dbg!(bench.report()); Ok(()) }