Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schedule executor benchmark #12873

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 65 additions & 0 deletions benches/benches/bevy_ecs/scheduling/executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use bevy_app::{App, Update};
use bevy_ecs::prelude::*;
use bevy_tasks::{ComputeTaskPool, TaskPool};
use criterion::Criterion;
use rand::random;
use std::time::{Duration, Instant};

fn s_system<const N: usize>() {
let now = Instant::now();
while Instant::now() - now < Duration::from_nanos(1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use something like tokio::time::sleep?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial thoughts was I just needed a short-lived system,I want to measure the cost of each batch(with different system count) spawned by the schedule.why should we use something like tokio::time::sleep?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or just time::sleep() sorry; which seems to do the same as your function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, time::sleep is clearer here :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sleep yields to the OS, which can be nondeterministic and bound by the minimum quanta established by the OS, which can be tens to hundreds of microseconds off. This may not be what we want in a benchmark.

// spin, simulating work being done
}
}

macro_rules! chain_systems {
($schedule:ident;$($indent:tt),*) => {
$schedule.add_systems(($(s_system::<$indent>,)*).chain());
};
}

pub fn executor(c: &mut Criterion) {
let mut group = c.benchmark_group("executor");
group.warm_up_time(std::time::Duration::from_millis(500));
group.measurement_time(std::time::Duration::from_secs(4));
group.bench_function("single-thread", |b| {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is running 16 systems in a chain?
I think it would be worht having a small explaining what the benchmark is doing.
Would it be worth trying with more systems?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, it should have a comment ,but more systems usually seems do not affect the result of benchmark,adding more system only increase time linearly.

let mut world = World::new();
let mut schedule = Schedule::default();
schedule.set_executor_kind(bevy_ecs::schedule::ExecutorKind::SingleThreaded);

// spawn 16 systems in chain
chain_systems!(schedule;0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);

schedule.initialize(&mut world);
b.iter(move || {
schedule.run(&mut world);
});
});

ComputeTaskPool::get_or_init(TaskPool::default);
let thread_num = ComputeTaskPool::get().thread_num();

for system_count_per_batch in [1, 10, 50, 100] {
group.bench_function(
format!(
"multi-thread({})-{}-per-batch",
thread_num, system_count_per_batch
),
|b| {
let mut world = World::new();
let mut schedule = Schedule::default();
schedule.set_executor_kind(bevy_ecs::schedule::ExecutorKind::MultiThreaded);

// spawn 16 batches, each with `system_count_per_batch` systems per batch.
for i in 0..system_count_per_batch {
re0312 marked this conversation as resolved.
Show resolved Hide resolved
chain_systems!(schedule;0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
}

schedule.initialize(&mut world);
b.iter(move || {
schedule.run(&mut world);
});
},
);
}
}
3 changes: 3 additions & 0 deletions benches/benches/bevy_ecs/scheduling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use criterion::criterion_group;
mod run_condition;
mod running_systems;
mod schedule;
mod executor;

use run_condition::*;
use running_systems::*;
use schedule::*;
use executor::*;

criterion_group!(
scheduling_benches,
Expand All @@ -20,4 +22,5 @@ criterion_group!(
schedule,
build_schedule,
empty_schedule_run,
executor,
);