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" lto = "fat"
codegen-units = 1 codegen-units = 1
panic = "abort" panic = "abort"
strip = true
[[bench]] [[bench]]
name = "simulation" name = "simulation"

View File

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