wordle-analyzer/src/bin/bench/cli.rs

78 lines
2.4 KiB
Rust
Raw Normal View History

2024-03-26 00:16:26 +01:00
#![warn(clippy::all)]
// #![warn(missing_docs)]
#![warn(missing_debug_implementations)]
use std::sync::Arc;
2024-03-26 00:16:26 +01:00
use clap::Parser;
use libpt::log::*;
2024-04-04 22:46:25 +02:00
use wordle_analyzer::bench::builtin::BuiltinBenchmark;
use wordle_analyzer::bench::report::Report;
2024-04-04 22:46:25 +02:00
use wordle_analyzer::bench::{Benchmark, DEFAULT_N};
use wordle_analyzer::error::WResult;
2024-04-04 22:46:25 +02:00
use wordle_analyzer::solve::{BuiltinSolverNames, Solver};
2024-03-26 00:16:26 +01:00
use wordle_analyzer::wlist::builtin::BuiltinWList;
2024-04-04 20:14:20 +02:00
2024-03-26 00:16:26 +01:00
use wordle_analyzer::{self, game};
#[derive(Parser, Clone, Debug)]
#[command(version, about, long_about, author)]
struct Cli {
/// precompute all possibilities for better performance at runtime
#[arg(short, long)]
precompute: bool,
/// how long should the word be?
#[arg(short, long, default_value_t = wordle_analyzer::DEFAULT_WORD_LENGTH)]
length: usize,
/// how many times can we guess?
#[arg(short, long, default_value_t = wordle_analyzer::DEFAULT_MAX_STEPS)]
max_steps: usize,
/// more verbose logs
#[arg(short, long)]
verbose: bool,
/// which solver to use
#[arg(short, long, default_value_t = BuiltinSolverNames::default())]
solver: BuiltinSolverNames,
2024-04-04 22:46:25 +02:00
/// how many games to play for the benchmark
#[arg(short, long, default_value_t = DEFAULT_N)]
n: usize,
/// how many threads to use for benchmarking
///
/// Note that the application as the whole will use at least one more thread.
#[arg(short, long, default_value_t = num_cpus::get())]
threads: usize,
2024-03-26 00:16:26 +01:00
}
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
if cli.verbose {
2024-04-04 22:46:25 +02:00
Logger::build_mini(Some(Level::DEBUG))?;
2024-03-26 00:16:26 +01:00
} else {
Logger::build_mini(Some(Level::INFO))?;
}
2024-04-04 22:46:25 +02:00
trace!("dumping CLI: {:#?}", cli);
2024-03-26 00:16:26 +01:00
let wl = BuiltinWList::default();
2024-04-04 22:46:25 +02:00
let builder = game::Game::builder(&wl)
2024-03-26 00:16:26 +01:00
.length(cli.length)
.max_steps(cli.max_steps)
.precompute(cli.precompute);
2024-04-04 22:46:25 +02:00
let solver = cli.solver.to_solver(&wl);
let bench = Arc::new(BuiltinBenchmark::build(&wl, solver, builder, cli.threads)?);
let bench_running = bench.clone();
2024-04-04 22:46:25 +02:00
trace!("{bench:#?}");
let n = cli.n;
let bench_th: std::thread::JoinHandle<WResult<Report>> =
std::thread::spawn(move || bench_running.bench(n));
while !bench_th.is_finished() {
println!("{}", bench.report());
}
2024-03-26 00:16:26 +01:00
// finished report
println!("{}", bench_th.join().expect("thread go boom")?);
2024-03-26 00:16:26 +01:00
Ok(())
}