Skip to content

Commit

Permalink
Commands benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSWard committed Jun 15, 2021
1 parent 71bf07f commit 1cc6a6e
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
6 changes: 6 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ edition = "2018"

[dev-dependencies]
criterion = "0.3"
rand = { version = "0.8.0", features = ["small_rng"] }
bevy = { path = "../" }

[[bench]]
name = "system_stage"
path = "benches/bevy_ecs/stages.rs"
harness = false

[[bench]]
name = "commands"
path = "benches/bevy_ecs/commands.rs"
harness = false

[[bench]]
name = "iter"
path = "benches/bevy_tasks/iter.rs"
Expand Down
121 changes: 121 additions & 0 deletions benches/benches/bevy_ecs/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use bevy::ecs::{
system::{Command, CommandQueue, Commands},
world::World,
};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::{rngs::SmallRng, RngCore, SeedableRng};

criterion_group!(benches, empty_commands, spawn_commands, fake_commands);
criterion_main!(benches);

struct A;
struct B;
struct C;

fn empty_commands(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("empty_commands");
group.warm_up_time(std::time::Duration::from_millis(500));
group.measurement_time(std::time::Duration::from_secs(4));

group.bench_function("0_entities", |bencher| {
let mut world = World::default();
let mut command_queue = CommandQueue::default();

bencher.iter(|| {
command_queue.apply(&mut world);
});
});

group.finish();
}

fn spawn_commands(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("spawn_commands");
group.warm_up_time(std::time::Duration::from_millis(500));
group.measurement_time(std::time::Duration::from_secs(4));

for entity_count in (1..5).map(|i| i * 2 * 1000) {
group.bench_function(format!("{}_entities", entity_count), |bencher| {
let mut world = World::default();
let mut command_queue = CommandQueue::default();

let mut rng = SmallRng::seed_from_u64(42);
let mut rng_bool = || rng.next_u32() % 2 == 0;

bencher.iter(|| {
let mut commands = Commands::new(&mut command_queue, &world);
for _ in 0..entity_count {
let mut entity = commands.spawn();

if rng_bool() {
entity.insert(A);
}

if rng_bool() {
entity.insert(B);
}

if rng_bool() {
entity.insert(C);
}

if rng_bool() {
entity.despawn();
}
}
drop(commands);
command_queue.apply(&mut world);
});
});
}

group.finish();
}

struct FakeCommandA;
struct FakeCommandB(u64);

impl Command for FakeCommandA {
fn write(self: Box<Self>, world: &mut World) {
black_box(self);
black_box(world);
}
}

impl Command for FakeCommandB {
fn write(self: Box<Self>, world: &mut World) {
black_box(self);
black_box(world);
}
}

fn fake_commands(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("fake_commands");
group.warm_up_time(std::time::Duration::from_millis(500));
group.measurement_time(std::time::Duration::from_secs(4));

for command_count in (1..5).map(|i| i * 2 * 1000) {
group.bench_function(format!("{}_commands", command_count), |bencher| {
let mut world = World::default();
let mut command_queue = CommandQueue::default();

let mut rng = SmallRng::seed_from_u64(42);
let mut rng_bool = || rng.next_u32() % 2 == 0;

bencher.iter(|| {
let mut commands = Commands::new(&mut command_queue, &world);
for _ in 0..command_count {
if rng_bool() {
commands.add(FakeCommandA);
} else {
commands.add(FakeCommandB(0));
}
}
drop(commands);
command_queue.apply(&mut world);
});
});
}

group.finish();
}

0 comments on commit 1cc6a6e

Please sign in to comment.