Skip to content

Commit

Permalink
added initial benchmarks, similar to bevy_xpbd
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Jul 2, 2024
1 parent 56f9870 commit 6e61318
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
9 changes: 9 additions & 0 deletions benches_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "benches_common"
version = "0.1.0"
edition = "2021"

[dependencies]
bevy = { version = "0.14.0-rc.3", default-features = false }
bevy_rapier3d = { path = "../bevy_rapier3d", default-features = false }
criterion = "0.5"
63 changes: 63 additions & 0 deletions benches_common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use bevy::{
app::PluginsState,
prelude::*,
render::{
settings::{RenderCreation, WgpuSettings},
RenderPlugin,
},
scene::ScenePlugin,
time::TimeUpdateStrategy,
};
use bevy_rapier3d::prelude::*;
use criterion::{measurement::Measurement, BatchSize, Bencher};

pub fn bench_app<M: Measurement>(
bencher: &mut Bencher<'_, M>,
steps: u32,
setup: impl Fn(&mut App),
) {
bencher.iter_batched_ref(
move || {
let mut app = App::new();

app.add_plugins((
WindowPlugin::default(),
MinimalPlugins,
AssetPlugin::default(),
ScenePlugin,
RenderPlugin {
render_creation: RenderCreation::Automatic(WgpuSettings {
backends: None,
..Default::default()
}),
..Default::default()
},
ImagePlugin::default(),
HierarchyPlugin,
TransformPlugin,
RapierPhysicsPlugin::<()>::default(),
));

// 60 physics
app.insert_resource(TimeUpdateStrategy::ManualDuration(
std::time::Duration::from_secs_f32(1f32 / 60f32),
));

setup(&mut app);

while app.plugins_state() != PluginsState::Ready {
bevy::tasks::tick_global_task_pools_on_main_thread();
}

app.finish();
app.cleanup();
app
},
move |app| {
for _ in 0..steps {
app.update();
}
},
BatchSize::PerIteration,
);
}
6 changes: 6 additions & 0 deletions bevy_rapier3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ log = "0.4"
serde = { version = "1", features = ["derive"], optional = true }

[dev-dependencies]
benches_common = { path = "../benches_common" }
bevy = { version = "0.14.0-rc.3", default-features = false, features = [
"x11",
"tonemapping_luts",
"bevy_state",
] }
approx = "0.5.1"
glam = { version = "0.27", features = ["approx"] }
criterion = { version = "0.5", features = ["html_reports"] }

[package.metadata.docs.rs]
# Enable all the features when building the docs on docs.rs
features = ["debug-render-3d", "serde-serialize"]

[[bench]]
name = "cubes"
harness = false
47 changes: 47 additions & 0 deletions bevy_rapier3d/benches/cubes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::time::Duration;

use benches_common::bench_app;
use bevy::prelude::*;
use bevy_rapier3d::math::*;
use bevy_rapier3d::prelude::*;
use criterion::{criterion_group, criterion_main, Criterion};

fn setup_cubes(app: &mut App, size: u32) {
app.add_systems(Startup, move |mut commands: Commands| {
commands.spawn((
RigidBody::Fixed,
Transform::from_translation(-2.0 * Vect::Z),
Collider::cuboid(100.0, 1.0, 100.0),
));
for x in 0..size {
for z in 0..size {
commands.spawn((
RigidBody::Dynamic,
Transform::from_translation(Vec3::new(x as f32, 2.0, z as f32)),
Collider::cuboid(1.0, 1.0, 1.0),
));
}
}
});
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("cubes 3x3, 30 steps", |b| {
bench_app(b, 30, |app| setup_cubes(app, 3))
});

c.bench_function("cubes 5x5, 30 steps", |b| {
bench_app(b, 30, |app| setup_cubes(app, 5))
});

c.bench_function("cubes 10x10, 30 steps", |b| {
bench_app(b, 30, |app| setup_cubes(app, 10))
});
}

criterion_group!(
name = benches;
config = Criterion::default().measurement_time(Duration::from_secs(20));
targets = criterion_benchmark
);
criterion_main!(benches);

0 comments on commit 6e61318

Please sign in to comment.