Skip to content

Commit

Permalink
Add pathing visibility graph update benchmark
Browse files Browse the repository at this point in the history
Relates to #12.
  • Loading branch information
Indy2222 committed Jun 5, 2022
1 parent 8912a0f commit a35e531
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/pathing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ futures-lite = "1.11.3"

[dev-dependencies]
ntest = "0.8.0"
criterion = "0.3"

[[bench]]
name = "pathing"
harness = false
75 changes: 75 additions & 0 deletions crates/pathing/benches/pathing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::{
fs::File,
io::{BufRead, BufReader},
path::PathBuf,
};

use bevy::prelude::GlobalTransform;
use criterion::{
criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
Throughput,
};
use de_index::Ichnography;
use de_map::size::MapBounds;
use de_pathing::create_finder;
use glam::Vec2;
use parry2d::{math::Point, shape::ConvexPolygon};

const MAP_SIZE: f32 = 8000.;

fn load_points(number: u32) -> Vec<Vec2> {
let mut points_path: PathBuf = env!("CARGO_MANIFEST_DIR").into();
points_path.push("test_data");
points_path.push(format!("{}-points.txt", number));
let reader = BufReader::new(File::open(points_path).unwrap());

let mut points = Vec::with_capacity(number as usize);
for line in reader.lines() {
let line = line.unwrap();
let mut numbers = line.split_whitespace();
let x: f32 = numbers.next().unwrap().parse().unwrap();
let y: f32 = numbers.next().unwrap().parse().unwrap();
points.push(MAP_SIZE * Vec2::new(x, y));
}
points
}

fn load_entities(number: u32) -> Vec<(GlobalTransform, Ichnography)> {
load_points(number)
.iter()
.map(|p| {
let ichnography = Ichnography::new(
ConvexPolygon::from_convex_hull(&[
Point::new(p.x - 10., p.y + 10.),
Point::new(p.x - 10., p.y - 10.),
Point::new(p.x + 10., p.y - 10.),
Point::new(p.x + 10., p.y + 10.),
])
.unwrap(),
);
(GlobalTransform::identity(), ichnography)
})
.collect()
}

fn create_finder_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("create_finder");
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
group.plot_config(plot_config);

for num_entities in [100, 1000, 10_000, 100_000] {
let entities = load_entities(num_entities);

let bounds = MapBounds::new(Vec2::splat(MAP_SIZE));

group.throughput(Throughput::Elements(1));
group.bench_function(BenchmarkId::from_parameter(num_entities), |b| {
b.iter(|| {
create_finder(bounds, entities.clone());
});
});
}
}

criterion_group!(benches, create_finder_benchmark);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion crates/pathing/src/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
};

/// A struct used for path finding.
pub(crate) struct PathFinder {
pub struct PathFinder {
/// Spatial index of triangles. It is used to find edges neighboring start
/// and end pints of a path to be found.
triangles: RTree<GraphTriangle>,
Expand Down
2 changes: 1 addition & 1 deletion crates/pathing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ mod systems;
mod triangulation;
mod utils;

pub use systems::{PathingPlugin, UpdateEntityPath};
pub use systems::{create_finder, PathingPlugin, UpdateEntityPath};
6 changes: 5 additions & 1 deletion crates/pathing/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ fn check_update_result(
}

/// Creates a new path finder by triangulating accessible area on the map.
fn create_finder(bounds: MapBounds, entities: Vec<(GlobalTransform, Ichnography)>) -> PathFinder {
// This function has to be public due to its benchmark.
pub fn create_finder(
bounds: MapBounds,
entities: Vec<(GlobalTransform, Ichnography)>,
) -> PathFinder {
debug!(
"Going to create a new path finder from {} entities",
entities.len()
Expand Down
1 change: 1 addition & 0 deletions crates/pathing/test_data/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*-points.txt filter=lfs diff=lfs merge=lfs -text
3 changes: 3 additions & 0 deletions crates/pathing/test_data/100-points.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions crates/pathing/test_data/1000-points.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions crates/pathing/test_data/10000-points.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions crates/pathing/test_data/100000-points.txt
Git LFS file not shown

0 comments on commit a35e531

Please sign in to comment.