Browse Source

Add benchmark

main
Adrian Heine 3 years ago
parent
commit
25b0004822
  1. 7
      Cargo.toml
  2. 48
      benches/benchmark.rs
  3. 2
      src/main.rs
  4. 18
      src/world.rs

7
Cargo.toml

@ -7,3 +7,10 @@ authors = ["Adrian Heine <mail@adrianheine.de>"]
[dependencies] [dependencies]
tui = "0.15" tui = "0.15"
termion = "1.5" termion = "1.5"
[dev-dependencies]
criterion = "0.3"
[[bench]]
name = "benchmark"
harness = false

48
benches/benchmark.rs

@ -0,0 +1,48 @@
use criterion::{BenchmarkId, measurement::Measurement};
use criterion::{criterion_group, criterion_main, Criterion, Throughput, BenchmarkGroup};
use gntag::agent::{Agent, SimpleAgent};
use gntag::world::ActualWorld;
fn world<M: Measurement>(mut group: BenchmarkGroup<M>, validate: bool) {
let width: isize = 1000;
for spacing in (50..100).step_by(10) {
group.throughput(Throughput::Elements((width / spacing).pow(2) as u64));
group.bench_with_input(
BenchmarkId::from_parameter(spacing),
&spacing,
|b, &spacing| {
let mut agents: Vec<(_, Box<dyn Agent>)> = vec![];
for x in (0..width).step_by(spacing as usize) {
for y in (0..width).step_by(spacing as usize) {
agents.push(((x, y).into(), Box::new(SimpleAgent)));
}
}
let mut world = ActualWorld::new((width, width).into(), agents, validate);
b.iter(|| world.do_step());
},
);
}
group.finish();
}
fn world_validating(c: &mut Criterion) {
let group = c.benchmark_group("world_validating");
world(group, true)
}
fn world_nonvalidating(c: &mut Criterion) {
let group = c.benchmark_group("world_nonvalidating");
world(group, false)
}
criterion_group!(benches, world_validating, world_nonvalidating);
criterion_main!(benches);
// 5000/1000 -> 9,993
// 5/01 -> 23,252
// 50/10 -> 14,000
// 100/20 -> 14,000
// 100/10 -> 96,000
// 100/05 -> 917,767
// 200/10 -> 916,172
// 1000/10 -> 443,482,526

2
src/main.rs

@ -42,7 +42,7 @@ fn run_simulation(view: &Arc<Mutex<Option<TerminalView<impl Backend>>>>) {
agents.push(((x, y).into(), Box::new(SimpleAgent))); agents.push(((x, y).into(), Box::new(SimpleAgent)));
} }
} }
let mut world = ActualWorld::new((width, height).into(), agents);
let mut world = ActualWorld::new((width, height).into(), agents, true);
let mut gen = 0; let mut gen = 0;
loop { loop {

18
src/world.rs

@ -5,6 +5,7 @@ pub struct ActualWorld {
size: Position, size: Position,
pub agents: HashMap<AgentId, Box<dyn Agent>>, pub agents: HashMap<AgentId, Box<dyn Agent>>,
pub state: WorldState, pub state: WorldState,
validating: bool,
} }
pub struct WorldState { pub struct WorldState {
@ -16,7 +17,11 @@ pub struct WorldState {
impl ActualWorld { impl ActualWorld {
/// Agents receive incrementing ids starting with 0 /// Agents receive incrementing ids starting with 0
/// The last agent is 'It' /// The last agent is 'It'
pub fn new(size: Position, input_agents: Vec<(Position, Box<dyn Agent>)>) -> Self {
pub fn new(
size: Position,
input_agents: Vec<(Position, Box<dyn Agent>)>,
validating: bool,
) -> Self {
let agent_count = input_agents.len(); let agent_count = input_agents.len();
assert!(agent_count > 0); assert!(agent_count > 0);
let mut id = 0; let mut id = 0;
@ -36,6 +41,7 @@ impl ActualWorld {
size, size,
agents, agents,
state, state,
validating,
} }
} }
@ -77,8 +83,9 @@ impl ActualWorld {
tagged_by: self.state.tagged_by, tagged_by: self.state.tagged_by,
}; };
for (id, mv) in moves { for (id, mv) in moves {
self.check_move(id, &mv);
//println!("{} {:?}", id, mv);
if self.validating {
self.check_move(id, &mv);
}
let mut new_pos = None; let mut new_pos = None;
match mv { match mv {
Move::Noop => {} Move::Noop => {}
@ -135,7 +142,7 @@ mod test {
#[test] #[test]
#[should_panic] #[should_panic]
fn empty() { fn empty() {
ActualWorld::new(Position { x: 0, y: 0 }, vec![]);
ActualWorld::new(Position { x: 0, y: 0 }, vec![], true);
} }
#[test] #[test]
@ -143,6 +150,7 @@ mod test {
let mut world = ActualWorld::new( let mut world = ActualWorld::new(
Position { x: 0, y: 0 }, Position { x: 0, y: 0 },
vec![(Position { x: 0, y: 0 }, Box::new(NullAgent))], vec![(Position { x: 0, y: 0 }, Box::new(NullAgent))],
true,
); );
world.do_step(); world.do_step();
assert_eq!(world.state.tagged, 0); assert_eq!(world.state.tagged, 0);
@ -163,6 +171,7 @@ mod test {
Box::new(ScriptedAgent::new(vec![Move::TryTag(0)])), Box::new(ScriptedAgent::new(vec![Move::TryTag(0)])),
), ),
], ],
true,
); );
world.do_step(); world.do_step();
assert_eq!(world.state.tagged, 0); assert_eq!(world.state.tagged, 0);
@ -178,6 +187,7 @@ mod test {
Position { x: 0, y: 0 }, Position { x: 0, y: 0 },
Box::new(ScriptedAgent::new(vec![Move::TryMove((-1, -1).into())])), Box::new(ScriptedAgent::new(vec![Move::TryMove((-1, -1).into())])),
)], )],
true,
); );
world.do_step(); world.do_step();
} }

Loading…
Cancel
Save