use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rustsim_core::prelude::*;
#[derive(Debug, Clone)]
struct Particle {
id: AgentId,
_x: f32,
}
impl Agent for Particle {
fn id(&self) -> AgentId {
self.id
}
}
fn bench_hashmap_insert(c: &mut Criterion) {
c.bench_function("HashMapStore insert 10k", |b| {
b.iter(|| {
let mut store = HashMapStore::new();
for i in 1..=10_000u64 {
store.insert(Particle { id: i, _x: 0.0 });
}
black_box(&store);
});
});
}
fn bench_vec_insert(c: &mut Criterion) {
c.bench_function("VecStore insert 10k", |b| {
b.iter(|| {
let mut store = VecStore::new();
for i in 1..=10_000u64 {
store.insert(Particle { id: i, _x: 0.0 });
}
black_box(&store);
});
});
}
fn bench_hashmap_get(c: &mut Criterion) {
let mut store = HashMapStore::new();
for i in 1..=10_000u64 {
store.insert(Particle {
id: i,
_x: i as f32,
});
}
c.bench_function("HashMapStore get 10k", |b| {
b.iter(|| {
for i in 1..=10_000u64 {
black_box(store.get(i));
}
});
});
}
fn bench_vec_get(c: &mut Criterion) {
let mut store = VecStore::new();
for i in 1..=10_000u64 {
store.insert(Particle {
id: i,
_x: i as f32,
});
}
c.bench_function("VecStore get 10k", |b| {
b.iter(|| {
for i in 1..=10_000u64 {
black_box(store.get(i));
}
});
});
}
fn bench_hashmap_iter_ids(c: &mut Criterion) {
let mut store = HashMapStore::new();
for i in 1..=100_000u64 {
store.insert(Particle { id: i, _x: 0.0 });
}
c.bench_function("HashMapStore iter_ids 100k", |b| {
b.iter(|| {
black_box(store.iter_ids());
});
});
}
fn bench_hashmap_remove(c: &mut Criterion) {
c.bench_function("HashMapStore remove 5k from 10k", |b| {
b.iter_batched(
|| {
let mut store = HashMapStore::new();
for i in 1..=10_000u64 {
store.insert(Particle { id: i, _x: 0.0 });
}
store
},
|mut store| {
for i in (2..=10_000u64).step_by(2) {
store.remove(i);
}
black_box(&store);
},
criterion::BatchSize::SmallInput,
);
});
}
criterion_group!(
benches,
bench_hashmap_insert,
bench_vec_insert,
bench_hashmap_get,
bench_vec_get,
bench_hashmap_iter_ids,
bench_hashmap_remove,
);
criterion_main!(benches);