Skip to content

Commit 8e82c88

Browse files
Basic event benchmarks (#8251)
# Objective Fix #7731. Add basic Event sending and iteration benchmarks to bevy_ecs's benchmark suite. ## Solution Add said benchmarks scaling from 100 to 50,000 events. Not sure if I want to include a randomization of the events going in, the current implementation might be too easy for the compiler to optimize. --------- Co-authored-by: JoJoJet <[email protected]>
1 parent a954f3e commit 8e82c88

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

benches/benches/bevy_ecs/benches.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use criterion::criterion_main;
22

33
mod components;
4+
mod events;
45
mod iteration;
56
mod scheduling;
67
mod world;
78

89
criterion_main!(
9-
iteration::iterations_benches,
1010
components::components_benches,
11+
events::event_benches,
12+
iteration::iterations_benches,
1113
scheduling::scheduling_benches,
1214
world::world_benches,
1315
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use bevy_ecs::prelude::*;
2+
3+
pub struct Benchmark<const SIZE: usize>(Events<[u8; SIZE]>);
4+
5+
impl<const SIZE: usize> Benchmark<SIZE> {
6+
pub fn new(count: usize) -> Self {
7+
let mut events = Events::default();
8+
9+
for _ in 0..count {
10+
events.send([0u8; SIZE]);
11+
}
12+
13+
Self(events)
14+
}
15+
16+
pub fn run(&mut self) {
17+
let mut reader = self.0.get_reader();
18+
for evt in reader.iter(&self.0) {
19+
std::hint::black_box(evt);
20+
}
21+
}
22+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use criterion::*;
2+
3+
mod iter;
4+
mod send;
5+
6+
criterion_group!(event_benches, send, iter,);
7+
8+
fn send(c: &mut Criterion) {
9+
let mut group = c.benchmark_group("events_send");
10+
group.warm_up_time(std::time::Duration::from_millis(500));
11+
group.measurement_time(std::time::Duration::from_secs(4));
12+
for count in [100, 1000, 10000, 50000] {
13+
group.bench_function(format!("size_4_events_{}", count), |b| {
14+
let mut bench = send::Benchmark::<4>::new(count);
15+
b.iter(move || bench.run());
16+
});
17+
}
18+
for count in [100, 1000, 10000, 50000] {
19+
group.bench_function(format!("size_16_events_{}", count), |b| {
20+
let mut bench = send::Benchmark::<16>::new(count);
21+
b.iter(move || bench.run());
22+
});
23+
}
24+
for count in [100, 1000, 10000, 50000] {
25+
group.bench_function(format!("size_512_events_{}", count), |b| {
26+
let mut bench = send::Benchmark::<512>::new(count);
27+
b.iter(move || bench.run());
28+
});
29+
}
30+
group.finish();
31+
}
32+
33+
fn iter(c: &mut Criterion) {
34+
let mut group = c.benchmark_group("events_iter");
35+
group.warm_up_time(std::time::Duration::from_millis(500));
36+
group.measurement_time(std::time::Duration::from_secs(4));
37+
for count in [100, 1000, 10000, 50000] {
38+
group.bench_function(format!("size_4_events_{}", count), |b| {
39+
let mut bench = iter::Benchmark::<4>::new(count);
40+
b.iter(move || bench.run());
41+
});
42+
}
43+
for count in [100, 1000, 10000, 50000] {
44+
group.bench_function(format!("size_16_events_{}", count), |b| {
45+
let mut bench = iter::Benchmark::<4>::new(count);
46+
b.iter(move || bench.run());
47+
});
48+
}
49+
for count in [100, 1000, 10000, 50000] {
50+
group.bench_function(format!("size_512_events_{}", count), |b| {
51+
let mut bench = iter::Benchmark::<512>::new(count);
52+
b.iter(move || bench.run());
53+
});
54+
}
55+
group.finish();
56+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use bevy_ecs::prelude::*;
2+
3+
pub struct Benchmark<const SIZE: usize> {
4+
events: Events<[u8; SIZE]>,
5+
count: usize,
6+
}
7+
8+
impl<const SIZE: usize> Benchmark<SIZE> {
9+
pub fn new(count: usize) -> Self {
10+
let mut events = Events::default();
11+
12+
// Force both internal buffers to be allocated.
13+
for _ in 0..2 {
14+
for _ in 0..count {
15+
events.send([0u8; SIZE]);
16+
}
17+
events.update();
18+
}
19+
20+
Self { events, count }
21+
}
22+
23+
pub fn run(&mut self) {
24+
for _ in 0..self.count {
25+
self.events.send(std::hint::black_box([0u8; SIZE]));
26+
}
27+
self.events.update();
28+
}
29+
}

0 commit comments

Comments
 (0)