Skip to content

Commit

Permalink
fix compilation ; removed some duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Jul 12, 2024
1 parent 3bb2b1a commit 7bf425b
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 195 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["bevy_rapier2d", "bevy_rapier3d"]
members = ["bevy_rapier2d", "bevy_rapier3d", "bevy_rapier_benches3d"]
resolver = "2"

[profile.dev]
Expand Down
1 change: 1 addition & 0 deletions bevy_rapier3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ bevy = { version = "0.14", default-features = false, features = [
approx = "0.5.1"
glam = { version = "0.27", features = ["approx"] }
divan = "0.1"
bevy_rapier_benches3d = { version = "0.1", path = "../bevy_rapier_benches3d" }

[package.metadata.docs.rs]
# Enable all the features when building the docs on docs.rs
Expand Down
12 changes: 12 additions & 0 deletions bevy_rapier3d/benches/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use bevy::app::App;
use bevy_rapier_benches3d::common::{default_app, wait_app_start};

pub fn bench_app_updates(bencher: divan::Bencher, setup: impl Fn(&mut App)) {
let mut app = default_app();
setup(&mut app);
wait_app_start(&mut app);

bencher.bench_local(|| {
app.update();
});
}
72 changes: 4 additions & 68 deletions bevy_rapier3d/benches/many_pyramids3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,22 @@
mod common;

use bevy_rapier_benches3d::pyramids::setup_pyramids;
use common::bench_app_updates;

use benches_common::bench_app_updates;
use bevy::prelude::*;
use bevy_rapier3d::math::*;
use bevy_rapier3d::prelude::*;

pub fn create_pyramid(commands: &mut Commands, offset: Vect, stack_height: usize, rad: f32) {
let shift = rad * 2.0;

for i in 0usize..stack_height {
for j in i..stack_height {
let fj = j as f32;
let fi = i as f32;
let x = (fi * shift / 2.0) + (fj - fi) * shift;
let y = fi * shift;

// Build the rigid body.
commands.spawn((
RigidBody::Dynamic,
Transform::from_translation(Vec3::new(x, y, 0.0) + offset),
Collider::cuboid(1.0, 1.0, 1.0),
));
}
}
}

pub fn setup_cubes(app: &mut App, pyramid_count: usize, stack_height: usize) {
app.add_systems(Startup, move |mut commands: Commands| {
let rad = 0.5;
let spacing = 4.0;

/*
* Ground
*/
let ground_size = 50.0;
let ground_height = 0.1;

commands.spawn((
RigidBody::Fixed,
Transform::from_translation(Vect::new(0.0, -ground_height, 0.0)),
Collider::cuboid(
ground_size,
ground_height,
pyramid_count as f32 * spacing / 2.0 + ground_size,
),
));

/*
* Create the cubes
*/
for pyramid_index in 0..pyramid_count {
let bottomy = rad;
create_pyramid(
&mut commands,
Vect::new(
0.0,
bottomy,
(pyramid_index as f32 - pyramid_count as f32 / 2.0) * spacing,
),
stack_height,
rad,
);
}
});
}

#[divan::bench(sample_count = 60, sample_size = 1)]
fn pyramid_1_with_height_2(bencher: divan::Bencher) {
bench_app_updates(bencher, |app| setup_cubes(app, 1, 2));
bench_app_updates(bencher, |app| setup_pyramids(app, 1, 2));
}

#[divan::bench(sample_count = 60, sample_size = 1)]
fn pyramid_1_with_height_20(bencher: divan::Bencher) {
bench_app_updates(bencher, |app| setup_cubes(app, 1, 20));
bench_app_updates(bencher, |app| setup_pyramids(app, 1, 20));
}

#[divan::bench(sample_count = 60, sample_size = 1)]
fn pyramid_2_with_height_20(bencher: divan::Bencher) {
bench_app_updates(bencher, |app| setup_cubes(app, 2, 20));
bench_app_updates(bencher, |app| setup_pyramids(app, 2, 20));
}

fn main() {
Expand Down
1 change: 0 additions & 1 deletion bevy_rapier_benches3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ edition = "2021"
rapier3d = { features = ["profiler"], version = "0.21" }
bevy_rapier3d = { version = "0.27.0-rc.1", path = "../bevy_rapier3d" }
bevy = { version = "0.14.0-rc.3", default-features = false }
benches_common = { version = "0.1", path = "../benches_common" }
2 changes: 1 addition & 1 deletion bevy_rapier_benches3d/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ It is implemented as a standalone binary, running different scenes setup, gather
and outputs them at the end.

```sh
cargo run --release -p bevy_rapier_benches3d
cargo run --release --bin bench
```

## cargo bench
Expand Down
14 changes: 14 additions & 0 deletions bevy_rapier_benches3d/src/bin/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use bevy_rapier_benches3d::{custom_bencher, pyramids::setup_pyramids};

fn pyramid_1_with_height_2() {
custom_bencher(1000, |app| setup_pyramids(app, 1, 2));
}

fn pyramid_2_with_height_20() {
custom_bencher(100, |app| setup_pyramids(app, 3, 20));
}

fn main() {
pyramid_1_with_height_2();
pyramid_2_with_height_20();
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,3 @@ pub fn wait_app_start(app: &mut App) {
app.finish();
app.cleanup();
}

pub fn bench_app_updates(bencher: divan::Bencher, setup: impl Fn(&mut App)) {
let mut app = default_app();
setup(&mut app);
wait_app_start(&mut app);

bencher.bench_local(|| {
app.update();
});
}
42 changes: 42 additions & 0 deletions bevy_rapier_benches3d/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Translated from rapier benchmark.
//!
//! <https://github.com/dimforge/rapier/blob/87ada34008f4a1a313ccf8c3040040bab4f10e69/benchmarks3d/many_pyramids3.rs>
pub mod common;
pub mod pyramids;

use common::default_app;
use common::wait_app_start;

use bevy::prelude::*;
use bevy_rapier3d::plugin::RapierContext;

pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) {
let mut app = default_app();
setup(&mut app);
wait_app_start(&mut app);

let mut timer_total = rapier3d::counters::Timer::new();
let mut timer_full_update = rapier3d::counters::Timer::new();
let mut rapier_step_times = vec![];
let mut total_update_times = vec![];
timer_total.start();
for _ in 0..steps {
timer_full_update.start();
app.update();
timer_full_update.pause();
let elapsed_time = timer_full_update.time() as f32;
let rc = app.world().resource::<RapierContext>();
rapier_step_times.push(rc.pipeline.counters.step_time.time() as f32);
total_update_times.push(elapsed_time);
}
timer_total.pause();
let average_total = total_update_times.iter().sum::<f32>() / total_update_times.len() as f32;
println!("average total time: {} ms", average_total);
let average_rapier_step =
rapier_step_times.iter().sum::<f32>() / rapier_step_times.len() as f32;
println!("average rapier step time: {} ms", average_rapier_step);
let average_rapier_overhead = average_total - average_rapier_step;
println!("average bevy overhead: {} ms", average_rapier_overhead);
println!("total time: {} ms", timer_total.time());
}
114 changes: 0 additions & 114 deletions bevy_rapier_benches3d/src/main.rs

This file was deleted.

64 changes: 64 additions & 0 deletions bevy_rapier_benches3d/src/pyramids.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use bevy::prelude::*;
use bevy_rapier3d::dynamics::RigidBody;
use bevy_rapier3d::geometry::Collider;
use bevy_rapier3d::math::Vect;

pub fn create_pyramid(commands: &mut Commands, offset: Vect, stack_height: usize, rad: f32) {
let shift = rad * 2.0;

for i in 0usize..stack_height {
for j in i..stack_height {
let fj = j as f32;
let fi = i as f32;
let x = (fi * shift / 2.0) + (fj - fi) * shift;
let y = fi * shift;

// Build the rigid body.
commands.spawn((
RigidBody::Dynamic,
Transform::from_translation(Vec3::new(x, y, 0.0) + offset),
Collider::cuboid(1.0, 1.0, 1.0),
));
}
}
}

pub fn setup_pyramids(app: &mut App, pyramid_count: usize, stack_height: usize) {
app.add_systems(Startup, move |mut commands: Commands| {
let rad = 0.5;
let spacing = 4.0;

/*
* Ground
*/
let ground_size = 50.0;
let ground_height = 0.1;

commands.spawn((
RigidBody::Fixed,
Transform::from_translation(Vect::new(0.0, -ground_height, 0.0)),
Collider::cuboid(
ground_size,
ground_height,
pyramid_count as f32 * spacing / 2.0 + ground_size,
),
));

/*
* Create the pyramids
*/
for pyramid_index in 0..pyramid_count {
let bottomy = rad;
create_pyramid(
&mut commands,
Vect::new(
0.0,
bottomy,
(pyramid_index as f32 - pyramid_count as f32 / 2.0) * spacing,
),
stack_height,
rad,
);
}
});
}

0 comments on commit 7bf425b

Please sign in to comment.