Skip to content

Commit 0ff926d

Browse files
Trashtalk217chescockBD103
committed
Added stress test for large ecs worlds (bevyengine#16591)
# Objective We currently have no benchmarks for large worlds with many entities, components and systems. Having a benchmark for a world with many components is especially useful for the performance improvements needed for relations. This is also a response to this [comment from cart](bevyengine#14385 (comment)). > I'd like both a small bevy_ecs-scoped executor benchmark that generates thousands of components used by hundreds of systems. ## Solution I use dynamic components and components to construct a benchmark with 2000 components, 4000 systems, and 10000 entities. ## Some notes - ~I use a lot of random entities, which creates unpredictable performance, I should use a seeded PRNG.~ - Not entirely sure if everything is ran concurrently currently. And there are many conflicts, meaning there's probably a lot of first-come-first-serve going on. Not entirely sure if these benchmarks are very reproducible. - Maybe add some more safety comments - Also component_reads_and_writes() is about to be deprecated bevyengine#16339, but there's no other way to currently do what I'm trying to do. --------- Co-authored-by: Chris Russell <[email protected]> Co-authored-by: BD103 <[email protected]>
1 parent 674ac27 commit 0ff926d

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,17 @@ description = "Test rendering of many cameras and lights"
27502750
category = "Stress Tests"
27512751
wasm = true
27522752

2753+
[[example]]
2754+
name = "many_components"
2755+
path = "examples/stress_tests/many_components.rs"
2756+
doc-scrape-examples = true
2757+
2758+
[package.metadata.example.many_components]
2759+
name = "Many Components (and Entities and Systems)"
2760+
description = "Test large ECS systems"
2761+
category = "Stress Tests"
2762+
wasm = false
2763+
27532764
[[example]]
27542765
name = "many_cubes"
27552766
path = "examples/stress_tests/many_cubes.rs"

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ Example | Description
463463
[Many Animated Sprites](../examples/stress_tests/many_animated_sprites.rs) | Displays many animated sprites in a grid arrangement with slight offsets to their animation timers. Used for performance testing.
464464
[Many Buttons](../examples/stress_tests/many_buttons.rs) | Test rendering of many UI elements
465465
[Many Cameras & Lights](../examples/stress_tests/many_cameras_lights.rs) | Test rendering of many cameras and lights
466+
[Many Components (and Entities and Systems)](../examples/stress_tests/many_components.rs) | Test large ECS systems
466467
[Many Cubes](../examples/stress_tests/many_cubes.rs) | Simple benchmark to test per-entity draw overhead. Run with the `sphere` argument to test frustum culling
467468
[Many Foxes](../examples/stress_tests/many_foxes.rs) | Loads an animated fox model and spawns lots of them. Good for testing skinned mesh performance. Takes an unsigned integer argument for the number of foxes to spawn. Defaults to 1000
468469
[Many Gizmos](../examples/stress_tests/many_gizmos.rs) | Test rendering of many gizmos
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
//! Stress test for large ECS worlds.
2+
//!
3+
//! Running this example:
4+
//!
5+
//! ```
6+
//! cargo run --profile stress-test --example many_components [<num_entities>] [<num_components>] [<num_systems>]
7+
//! ```
8+
//!
9+
//! `num_entities`: The number of entities in the world (must be nonnegative)
10+
//! `num_components`: the number of components in the world (must be at least 10)
11+
//! `num_systems`: the number of systems in the world (must be nonnegative)
12+
//!
13+
//! If no valid number is provided, for each argument there's a reasonable default.
14+
15+
use bevy::{
16+
diagnostic::{
17+
DiagnosticPath, DiagnosticsPlugin, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin,
18+
},
19+
ecs::{
20+
component::{ComponentDescriptor, ComponentId, StorageType},
21+
system::QueryParamBuilder,
22+
world::FilteredEntityMut,
23+
},
24+
log::LogPlugin,
25+
prelude::{App, In, IntoSystem, Query, Schedule, SystemParamBuilder, Update},
26+
ptr::OwningPtr,
27+
MinimalPlugins,
28+
};
29+
30+
use rand::prelude::{Rng, SeedableRng, SliceRandom};
31+
use rand_chacha::ChaCha8Rng;
32+
use std::{alloc::Layout, num::Wrapping};
33+
34+
// A simple system that matches against several components and does some menial calculation to create
35+
// some non-trivial load.
36+
fn base_system(access_components: In<Vec<ComponentId>>, mut query: Query<FilteredEntityMut>) {
37+
for mut filtered_entity in &mut query {
38+
// We calculate Faulhaber's formula mod 256 with n = value and p = exponent.
39+
// See https://en.wikipedia.org/wiki/Faulhaber%27s_formula
40+
// The time is takes to compute this depends on the number of entities and the values in
41+
// each entity. This is to ensure that each system takes a different amount of time.
42+
let mut total: Wrapping<u8> = Wrapping(0);
43+
let mut exponent: u32 = 1;
44+
for component_id in &access_components.0 {
45+
// find the value of the component
46+
let ptr = filtered_entity.get_by_id(*component_id).unwrap();
47+
48+
#[expect(unsafe_code)]
49+
// SAFETY: All components have a u8 layout
50+
let value: u8 = unsafe { *ptr.deref::<u8>() };
51+
52+
for i in 0..=value {
53+
let mut product = Wrapping(1);
54+
for _ in 1..=exponent {
55+
product *= Wrapping(i);
56+
}
57+
total += product;
58+
}
59+
exponent += 1;
60+
}
61+
62+
// we assign this value to all the components we can write to
63+
for component_id in &access_components.0 {
64+
if let Some(ptr) = filtered_entity.get_mut_by_id(*component_id) {
65+
#[expect(unsafe_code)]
66+
// SAFETY: All components have a u8 layout
67+
unsafe {
68+
let mut value = ptr.with_type::<u8>();
69+
*value = total.0;
70+
}
71+
}
72+
}
73+
}
74+
}
75+
76+
fn stress_test(num_entities: u32, num_components: u32, num_systems: u32) {
77+
let mut rng = ChaCha8Rng::seed_from_u64(42);
78+
let mut app = App::default();
79+
let world = app.world_mut();
80+
81+
// register a bunch of components
82+
let component_ids: Vec<ComponentId> = (1..=num_components)
83+
.map(|i| {
84+
world.register_component_with_descriptor(
85+
#[allow(unsafe_code)]
86+
// SAFETY:
87+
// we don't implement a drop function
88+
// u8 is Sync and Send
89+
unsafe {
90+
ComponentDescriptor::new_with_layout(
91+
format!("Component{}", i).to_string(),
92+
StorageType::Table,
93+
Layout::new::<u8>(),
94+
None,
95+
true, // is mutable
96+
)
97+
},
98+
)
99+
})
100+
.collect();
101+
102+
// fill the schedule with systems
103+
let mut schedule = Schedule::new(Update);
104+
for _ in 1..=num_systems {
105+
let num_access_components = rng.gen_range(1..10);
106+
let access_components: Vec<ComponentId> = component_ids
107+
.choose_multiple(&mut rng, num_access_components)
108+
.copied()
109+
.collect();
110+
let system = (QueryParamBuilder::new(|builder| {
111+
for &access_component in &access_components {
112+
if rand::random::<bool>() {
113+
builder.mut_id(access_component);
114+
} else {
115+
builder.ref_id(access_component);
116+
}
117+
}
118+
}),)
119+
.build_state(world)
120+
.build_any_system(base_system);
121+
schedule.add_systems((move || access_components.clone()).pipe(system));
122+
}
123+
124+
// spawn a bunch of entities
125+
for _ in 1..=num_entities {
126+
let num_components = rng.gen_range(1..10);
127+
let components = component_ids.choose_multiple(&mut rng, num_components);
128+
129+
let mut entity = world.spawn_empty();
130+
for &component_id in components {
131+
let value: u8 = rng.gen_range(0..255);
132+
OwningPtr::make(value, |ptr| {
133+
#[allow(unsafe_code)]
134+
// SAFETY:
135+
// component_id is from the same world
136+
// value is u8, so ptr is a valid reference for component_id
137+
unsafe {
138+
entity.insert_by_id(component_id, ptr);
139+
}
140+
});
141+
}
142+
}
143+
144+
// overwrite Update schedule in the app
145+
app.add_schedule(schedule);
146+
app.add_plugins(MinimalPlugins)
147+
.add_plugins(DiagnosticsPlugin)
148+
.add_plugins(LogPlugin::default())
149+
.add_plugins(FrameTimeDiagnosticsPlugin)
150+
.add_plugins(LogDiagnosticsPlugin::filtered(vec![DiagnosticPath::new(
151+
"fps",
152+
)]));
153+
app.run();
154+
}
155+
156+
#[expect(missing_docs)]
157+
pub fn main() {
158+
const DEFAULT_NUM_ENTITIES: u32 = 50000;
159+
const DEFAULT_NUM_COMPONENTS: u32 = 1000;
160+
const DEFAULT_NUM_SYSTEMS: u32 = 800;
161+
162+
// take input
163+
let num_entities = std::env::args()
164+
.nth(1)
165+
.and_then(|string| string.parse::<u32>().ok())
166+
.unwrap_or_else(|| {
167+
println!(
168+
"No valid number of entities provided, using default {}",
169+
DEFAULT_NUM_ENTITIES
170+
);
171+
DEFAULT_NUM_ENTITIES
172+
});
173+
let num_components = std::env::args()
174+
.nth(2)
175+
.and_then(|string| string.parse::<u32>().ok())
176+
.and_then(|n| if n >= 10 { Some(n) } else { None })
177+
.unwrap_or_else(|| {
178+
println!(
179+
"No valid number of components provided (>= 10), using default {}",
180+
DEFAULT_NUM_COMPONENTS
181+
);
182+
DEFAULT_NUM_COMPONENTS
183+
});
184+
let num_systems = std::env::args()
185+
.nth(3)
186+
.and_then(|string| string.parse::<u32>().ok())
187+
.unwrap_or_else(|| {
188+
println!(
189+
"No valid number of systems provided, using default {}",
190+
DEFAULT_NUM_SYSTEMS
191+
);
192+
DEFAULT_NUM_SYSTEMS
193+
});
194+
195+
stress_test(num_entities, num_components, num_systems);
196+
}

0 commit comments

Comments
 (0)