improve progress bar

This commit is contained in:
2025-04-24 21:22:04 +02:00
parent 9333f6c6fb
commit caa6eed783
3 changed files with 46 additions and 4 deletions

View File

@ -5,3 +5,4 @@ edition = "2024"
[dependencies]
rayon = "1.10"
rich_progress_bar = "1.1"

View File

@ -4,11 +4,13 @@ use std::{
net::TcpStream,
path::{Path, PathBuf},
process::{Child, Command, Stdio},
sync::{Mutex, atomic::AtomicUsize},
thread::sleep,
time::Duration,
};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use rich_progress_bar::{Colors, RichProgressBar};
type DynError = Box<dyn std::error::Error>;
@ -232,6 +234,8 @@ fn regression() -> Result<(), DynError> {
}
};
sleep(Duration::from_secs(1));
let res = try_regression();
snake.kill().and(prod.kill())?;
@ -265,18 +269,25 @@ fn try_regression() -> Result<(usize, usize, usize), DynError> {
.build_global()
.unwrap();
let mut progress = RichProgressBar::new();
progress
.set_progress_character('=')
.set_color(Colors::BrightCyan)
.set_total(GAMES as u64);
let progress = Mutex::new(&mut progress);
let stats = (0..GAMES)
.into_par_iter()
.flat_map(|_| {
eprint!(".");
let game = Command::new("./battlesnake-cli")
.current_dir(project_root())
.args([
"play",
"-W",
"--width",
"11",
"-H",
"--height",
"11",
"--timeout",
"100",
"--name",
"local",
"--url",
@ -290,7 +301,7 @@ fn try_regression() -> Result<(usize, usize, usize), DynError> {
])
.output()
.ok()?;
if !game.status.success() {
let res = if !game.status.success() {
eprintln!("game output: {}", String::from_utf8(game.stderr).ok()?);
eprintln!("game status: {}", game.status);
None
@ -308,7 +319,11 @@ fn try_regression() -> Result<(usize, usize, usize), DynError> {
} else {
Some((0, 1, 0))
}
};
if let Ok(mut progress) = progress.lock() {
let _ = progress.inc();
}
res
})
.reduce(
|| (0, 0, 0),