add simulation benchmark

This commit is contained in:
Max Känner 2024-10-05 20:19:17 +02:00
parent a4dc6eb6da
commit 16c78c81ae
3 changed files with 242 additions and 0 deletions

114
Cargo.lock generated
View File

@ -26,6 +26,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "anes"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "735d4f398ca57cfa2880225c2bf81c3b9af3be5bb22e44ae70118dad38713e84"
[[package]]
name = "anstream"
version = "0.6.15"
@ -148,6 +154,7 @@ dependencies = [
name = "battlesnake"
version = "1.0.0"
dependencies = [
"criterion2",
"enum-iterator",
"env_logger",
"iter_tools",
@ -171,6 +178,12 @@ version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "bpaf"
version = "0.9.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d4e5ca9929037866947af4b8b7418124f2ec7c411a8b9ee24e46ad2b8470497"
[[package]]
name = "bytemuck"
version = "1.17.1"
@ -189,6 +202,12 @@ version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "cc"
version = "1.1.15"
@ -204,6 +223,33 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "ciborium"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
dependencies = [
"ciborium-io",
"ciborium-ll",
"serde",
]
[[package]]
name = "ciborium-io"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
[[package]]
name = "ciborium-ll"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
dependencies = [
"ciborium-io",
"half",
]
[[package]]
name = "clone_dyn_types"
version = "0.23.0"
@ -227,6 +273,24 @@ dependencies = [
"version_check",
]
[[package]]
name = "criterion2"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6c2be2af5816b76f45745ccfb9959a9efd1322ae4c88fc3c9b2d6bcdabe5554"
dependencies = [
"anes",
"bpaf",
"cast",
"ciborium",
"num-traits",
"oorandom",
"rayon",
"serde",
"serde_json",
"walkdir",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
@ -252,6 +316,12 @@ version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "deranged"
version = "0.3.11"
@ -514,6 +584,16 @@ dependencies = [
"tracing",
]
[[package]]
name = "half"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888"
dependencies = [
"cfg-if",
"crunchy",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
@ -826,6 +906,12 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "oorandom"
version = "11.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9"
[[package]]
name = "ordered-float"
version = "4.3.0"
@ -1187,6 +1273,15 @@ version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scoped-tls"
version = "1.0.1"
@ -1586,6 +1681,16 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "want"
version = "0.3.1"
@ -1617,6 +1722,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -26,8 +26,16 @@ enum-iterator = "2.1"
iter_tools = "0.21"
ordered-float = "4.3.0"
[dev-dependencies]
# criterion = { version = "0.5.1", features = ["html_reports"] }
criterion2 = "1.1.1"
[profile.release]
lto = "fat"
codegen-units = 1
panic = "abort"
strip = true
[[bench]]
name = "simulation"
harness = false

View File

@ -0,0 +1,120 @@
use battlesnake::{
simulation::{self, SnakeToken},
Coord,
};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::{rngs::StdRng, SeedableRng};
fn random_moves(board: &battlesnake::Board) -> Option<SnakeToken> {
let token_map = SnakeToken::from_board(board);
let mut board = simulation::Board::from_game_board(board, &token_map, 0, 15, 1, false);
let mut rng = StdRng::seed_from_u64(0);
board.simulate_until(&mut rng, |board| board.alive_snakes() <= 1);
let winner = board.snakes().next();
winner
}
fn bench_duel_random_moves(c: &mut Criterion) {
c.bench_function("duel random moves", |b| {
b.iter(|| {
random_moves(black_box(&battlesnake::Board {
height: 11,
width: 11,
food: vec![Coord { x: 5, y: 5 }],
snakes: vec![
battlesnake::Battlesnake {
id: "1".to_owned(),
name: "1".to_owned(),
health: 100,
body: vec![Coord { x: 5, y: 1 }; 3],
head: Coord { x: 5, y: 1 },
length: 3,
latency: "0".to_owned(),
shout: None,
squad: String::new(),
},
battlesnake::Battlesnake {
id: "2".to_owned(),
name: "2".to_owned(),
health: 100,
body: vec![Coord { x: 5, y: 9 }; 3],
head: Coord { x: 5, y: 9 },
length: 3,
latency: "0".to_owned(),
shout: None,
squad: String::new(),
},
],
hazards: vec![],
}))
});
});
}
fn bench_standard_random_moves(c: &mut Criterion) {
c.bench_function("standard random moves", |b| {
b.iter(|| {
random_moves(black_box(&battlesnake::Board {
height: 11,
width: 11,
food: vec![Coord { x: 5, y: 5 }],
snakes: vec![
battlesnake::Battlesnake {
id: "1".to_owned(),
name: "1".to_owned(),
health: 100,
body: vec![Coord { x: 5, y: 1 }; 3],
head: Coord { x: 5, y: 1 },
length: 3,
latency: "0".to_owned(),
shout: None,
squad: String::new(),
},
battlesnake::Battlesnake {
id: "2".to_owned(),
name: "2".to_owned(),
health: 100,
body: vec![Coord { x: 5, y: 9 }; 3],
head: Coord { x: 5, y: 9 },
length: 3,
latency: "0".to_owned(),
shout: None,
squad: String::new(),
},
battlesnake::Battlesnake {
id: "3".to_owned(),
name: "3".to_owned(),
health: 100,
body: vec![Coord { x: 1, y: 5 }; 3],
head: Coord { x: 1, y: 5 },
length: 3,
latency: "0".to_owned(),
shout: None,
squad: String::new(),
},
battlesnake::Battlesnake {
id: "4".to_owned(),
name: "4".to_owned(),
health: 100,
body: vec![Coord { x: 9, y: 5 }; 3],
head: Coord { x: 9, y: 5 },
length: 3,
latency: "0".to_owned(),
shout: None,
squad: String::new(),
},
],
hazards: vec![],
}))
});
});
}
criterion_group!(
benches,
bench_duel_random_moves,
bench_standard_random_moves
);
criterion_main!(benches);