|
| 1 | +use bevy_app::{App, First, Startup}; |
| 2 | +use bevy_ecs::{ |
| 3 | + component::Component, |
| 4 | + entity::Entity, |
| 5 | + event::{Event, EventWriter}, |
| 6 | + observer::Trigger, |
| 7 | + query::{Or, With, Without}, |
| 8 | + system::{Commands, EntityCommands, Query}, |
| 9 | +}; |
| 10 | +use bevy_hierarchy::{BuildChildren, Children, Parent}; |
| 11 | +use bevy_internal::MinimalPlugins; |
| 12 | + |
| 13 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 14 | +use rand::{seq::IteratorRandom, Rng}; |
| 15 | + |
| 16 | +const DENSITY: usize = 20; // percent of nodes with listeners |
| 17 | +const ENTITY_DEPTH: usize = 64; |
| 18 | +const ENTITY_WIDTH: usize = 200; |
| 19 | +const N_EVENTS: usize = 500; |
| 20 | + |
| 21 | +pub fn event_propagation(criterion: &mut Criterion) { |
| 22 | + let mut group = criterion.benchmark_group("event_propagation"); |
| 23 | + group.warm_up_time(std::time::Duration::from_millis(500)); |
| 24 | + group.measurement_time(std::time::Duration::from_secs(4)); |
| 25 | + |
| 26 | + group.bench_function("baseline", |bencher| { |
| 27 | + let mut app = App::new(); |
| 28 | + app.add_plugins(MinimalPlugins) |
| 29 | + .add_systems(Startup, spawn_listener_hierarchy); |
| 30 | + app.update(); |
| 31 | + |
| 32 | + bencher.iter(|| { |
| 33 | + black_box(app.update()); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + group.bench_function("single_event_type", |bencher| { |
| 38 | + let mut app = App::new(); |
| 39 | + app.add_plugins(MinimalPlugins) |
| 40 | + .add_systems( |
| 41 | + Startup, |
| 42 | + ( |
| 43 | + spawn_listener_hierarchy, |
| 44 | + add_listeners_to_hierarchy::<DENSITY, 1>, |
| 45 | + ), |
| 46 | + ) |
| 47 | + .add_systems(First, send_events::<1, N_EVENTS>); |
| 48 | + app.update(); |
| 49 | + |
| 50 | + bencher.iter(|| { |
| 51 | + black_box(app.update()); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + group.bench_function("single_event_type_no_listeners", |bencher| { |
| 56 | + let mut app = App::new(); |
| 57 | + app.add_plugins(MinimalPlugins) |
| 58 | + .add_systems( |
| 59 | + Startup, |
| 60 | + ( |
| 61 | + spawn_listener_hierarchy, |
| 62 | + add_listeners_to_hierarchy::<DENSITY, 1>, |
| 63 | + ), |
| 64 | + ) |
| 65 | + .add_systems(First, send_events::<9, N_EVENTS>); |
| 66 | + app.update(); |
| 67 | + |
| 68 | + bencher.iter(|| { |
| 69 | + black_box(app.update()); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + group.bench_function("four_event_types", |bencher| { |
| 74 | + let mut app = App::new(); |
| 75 | + const FRAC_N_EVENTS_4: usize = N_EVENTS / 4; |
| 76 | + const FRAC_DENSITY_4: usize = DENSITY / 4; |
| 77 | + |
| 78 | + app.add_plugins(MinimalPlugins) |
| 79 | + .add_systems( |
| 80 | + Startup, |
| 81 | + ( |
| 82 | + spawn_listener_hierarchy, |
| 83 | + add_listeners_to_hierarchy::<FRAC_DENSITY_4, 1>, |
| 84 | + add_listeners_to_hierarchy::<FRAC_DENSITY_4, 2>, |
| 85 | + add_listeners_to_hierarchy::<FRAC_DENSITY_4, 3>, |
| 86 | + add_listeners_to_hierarchy::<FRAC_DENSITY_4, 4>, |
| 87 | + ), |
| 88 | + ) |
| 89 | + .add_systems(First, send_events::<1, FRAC_N_EVENTS_4>) |
| 90 | + .add_systems(First, send_events::<2, FRAC_N_EVENTS_4>) |
| 91 | + .add_systems(First, send_events::<3, FRAC_N_EVENTS_4>) |
| 92 | + .add_systems(First, send_events::<4, FRAC_N_EVENTS_4>); |
| 93 | + app.update(); |
| 94 | + |
| 95 | + bencher.iter(|| { |
| 96 | + black_box(app.update()); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + group.finish(); |
| 101 | +} |
| 102 | + |
| 103 | +#[derive(Clone, Component)] |
| 104 | +struct TestEvent<const N: usize> {} |
| 105 | + |
| 106 | +impl<const N: usize> Event for TestEvent<N> { |
| 107 | + type Traversal = Parent; |
| 108 | + const AUTO_PROPAGATE: bool = true; |
| 109 | +} |
| 110 | + |
| 111 | +fn send_events<const N: usize, const N_EVENTS: usize>( |
| 112 | + mut commands: Commands, |
| 113 | + entities: Query<Entity, Without<Children>>, |
| 114 | +) { |
| 115 | + let target = entities.iter().choose(&mut rand::thread_rng()).unwrap(); |
| 116 | + (0..N_EVENTS).for_each(|_| { |
| 117 | + commands.trigger_targets(TestEvent::<N> {}, target); |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +fn spawn_listener_hierarchy(mut commands: Commands) { |
| 122 | + for _ in 0..ENTITY_WIDTH { |
| 123 | + let mut parent = commands.spawn_empty().id(); |
| 124 | + for _ in 0..ENTITY_DEPTH { |
| 125 | + let child = commands.spawn_empty().id(); |
| 126 | + commands.entity(parent).add_child(child); |
| 127 | + parent = child; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +fn empty_listener<const N: usize>(_trigger: Trigger<TestEvent<N>>) {} |
| 133 | + |
| 134 | +fn add_listeners_to_hierarchy<const DENSITY: usize, const N: usize>( |
| 135 | + mut commands: Commands, |
| 136 | + roots_and_leaves: Query<Entity, Or<(Without<Parent>, Without<Children>)>>, |
| 137 | + nodes: Query<Entity, (With<Parent>, With<Children>)>, |
| 138 | +) { |
| 139 | + for entity in &roots_and_leaves { |
| 140 | + commands.entity(entity).observe(empty_listener::<N>); |
| 141 | + } |
| 142 | + for entity in &nodes { |
| 143 | + maybe_insert_listener::<DENSITY, N>(&mut commands.entity(entity)); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +fn maybe_insert_listener<const DENSITY: usize, const N: usize>(commands: &mut EntityCommands) { |
| 148 | + if rand::thread_rng().gen_bool(DENSITY as f64 / 100.0) { |
| 149 | + commands.observe(empty_listener::<N>); |
| 150 | + } |
| 151 | +} |
0 commit comments