add more statistics to regression test
This commit is contained in:
parent
f3d7c3160d
commit
8895f875b2
@ -1,4 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
cmp::Ordering,
|
||||||
env,
|
env,
|
||||||
net::TcpStream,
|
net::TcpStream,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
@ -156,15 +157,29 @@ fn regression() -> Result<(), DynError> {
|
|||||||
let res = try_regression();
|
let res = try_regression();
|
||||||
|
|
||||||
snake.kill().and(prod.kill())?;
|
snake.kill().and(prod.kill())?;
|
||||||
let (won, games) = res?;
|
let (won, draw, loose) = res?;
|
||||||
|
let games = won + draw + loose;
|
||||||
println!(
|
println!(
|
||||||
"\nThe local snake has won {won}/{games} games ({}%)",
|
"\nThe local snake has won {won}/{games} games ({}%)",
|
||||||
(won + games / 200) * 100 / 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_regression() -> Result<(usize, usize), DynError> {
|
fn try_regression() -> Result<(usize, usize, usize), DynError> {
|
||||||
const GAMES: usize = 100;
|
const GAMES: usize = 100;
|
||||||
// limit the parallelism
|
// limit the parallelism
|
||||||
rayon::ThreadPoolBuilder::new()
|
rayon::ThreadPoolBuilder::new()
|
||||||
@ -172,9 +187,9 @@ fn try_regression() -> Result<(usize, usize), DynError> {
|
|||||||
.build_global()
|
.build_global()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let won_count = (0..GAMES)
|
let stats = (0..GAMES)
|
||||||
.into_par_iter()
|
.into_par_iter()
|
||||||
.map(|_| -> Option<usize> {
|
.flat_map(|_| {
|
||||||
eprint!(".");
|
eprint!(".");
|
||||||
let game = Command::new("./battlesnake-cli")
|
let game = Command::new("./battlesnake-cli")
|
||||||
.current_dir(project_root())
|
.current_dir(project_root())
|
||||||
@ -203,15 +218,28 @@ fn try_regression() -> Result<(usize, usize), DynError> {
|
|||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
const WIN_PHRASE: &[u8] = b"local was the winner.\n";
|
const WIN_PHRASE: &[u8] = b"local was the winner.\n";
|
||||||
let won = &game.stderr[(game.stderr.len() - WIN_PHRASE.len())..game.stderr.len()]
|
const LOOSE_PHRASE: &[u8] = b"production was the winner.\n";
|
||||||
== WIN_PHRASE;
|
if &game.stderr[(game.stderr.len() - WIN_PHRASE.len())..game.stderr.len()]
|
||||||
Some(usize::from(won))
|
== 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()
|
.reduce(
|
||||||
.sum();
|
|| (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> {
|
fn run_snake(port: u16, log: Option<&str>) -> Result<Child, DynError> {
|
||||||
|
Loading…
Reference in New Issue
Block a user