add more statistics to regression test

This commit is contained in:
Max Känner 2024-10-07 19:24:00 +02:00
parent f3d7c3160d
commit 8895f875b2

View File

@ -1,4 +1,5 @@
use std::{
cmp::Ordering,
env,
net::TcpStream,
path::{Path, PathBuf},
@ -156,15 +157,29 @@ fn regression() -> Result<(), DynError> {
let res = try_regression();
snake.kill().and(prod.kill())?;
let (won, games) = res?;
let (won, draw, loose) = res?;
let games = won + draw + loose;
println!(
"\nThe local snake has won {won}/{games} games ({}%)",
(won + games / 200) * 100 / games
);
println!(
"The local snake has drawn {draw}/{games} games ({}%)",
(draw + games / 200) * 100 / games
);
println!(
"The local snake has lost {loose}/{games} games ({}%)",
(loose + games / 200) * 100 / games
);
match won.cmp(&loose) {
Ordering::Less => println!("The local nake is worse than the production"),
Ordering::Equal => println!("The local snake is equivalent to the production"),
Ordering::Greater => println!("The local snake is better than the production"),
}
Ok(())
}
fn try_regression() -> Result<(usize, usize), DynError> {
fn try_regression() -> Result<(usize, usize, usize), DynError> {
const GAMES: usize = 100;
// limit the parallelism
rayon::ThreadPoolBuilder::new()
@ -172,9 +187,9 @@ fn try_regression() -> Result<(usize, usize), DynError> {
.build_global()
.unwrap();
let won_count = (0..GAMES)
let stats = (0..GAMES)
.into_par_iter()
.map(|_| -> Option<usize> {
.flat_map(|_| {
eprint!(".");
let game = Command::new("./battlesnake-cli")
.current_dir(project_root())
@ -203,15 +218,28 @@ fn try_regression() -> Result<(usize, usize), DynError> {
None
} else {
const WIN_PHRASE: &[u8] = b"local was the winner.\n";
let won = &game.stderr[(game.stderr.len() - WIN_PHRASE.len())..game.stderr.len()]
== WIN_PHRASE;
Some(usize::from(won))
const LOOSE_PHRASE: &[u8] = b"production was the winner.\n";
if &game.stderr[(game.stderr.len() - WIN_PHRASE.len())..game.stderr.len()]
== WIN_PHRASE
{
Some((1, 0, 0))
} else if &game.stderr[(game.stderr.len() - LOOSE_PHRASE.len())..game.stderr.len()]
== LOOSE_PHRASE
{
Some((0, 0, 1))
} else {
Some((0, 1, 0))
}
}
})
.flatten()
.sum();
.reduce(
|| (0, 0, 0),
|(sum_win, sum_draw, sum_loose), (win, draw, loose)| {
(sum_win + win, sum_draw + draw, sum_loose + loose)
},
);
Ok((won_count, GAMES))
Ok(stats)
}
fn run_snake(port: u16, log: Option<&str>) -> Result<Child, DynError> {