|
| 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