improve progress bar

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

26
Cargo.lock generated
View File

@ -298,6 +298,16 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "colored"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
dependencies = [
"lazy_static",
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.17" version = "0.2.17"
@ -759,6 +769,12 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.172" version = "0.2.172"
@ -1076,6 +1092,15 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "rich_progress_bar"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "892eca205ea9663141375f828ed5cfc1adbe70b25283ccec0b2f264d7059cb06"
dependencies = [
"colored",
]
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.24" version = "0.1.24"
@ -1623,6 +1648,7 @@ name = "xtask"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"rayon", "rayon",
"rich_progress_bar",
] ]
[[package]] [[package]]

View File

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

View File

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