-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
base: main
Are you sure you want to change the base?
Schedule executor benchmark #12873
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
// 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| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so this is running 16 systems in a chain? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
}, | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.