Compare commits

..

No commits in common. "55050d54519852a30ff2925520cb64ce875bbfbe" and "f3d7c3160dc735acccd0e4a2b69e0024345c03a5" have entirely different histories.

2 changed files with 11 additions and 38 deletions

View File

@ -34,6 +34,7 @@ criterion2 = "1.1.1"
lto = "fat"
codegen-units = 1
panic = "abort"
strip = true
[[bench]]
name = "simulation"

View File

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