|
| 1 | +use bevy_ecs::{ |
| 2 | + component::Component, |
| 3 | + prelude::{ParallelSystemDescriptorCoercion, Res, RunCriteriaDescriptorCoercion}, |
| 4 | + schedule::{ShouldRun, Stage, SystemStage}, |
| 5 | + system::{IntoSystem, Query}, |
| 6 | + world::World, |
| 7 | +}; |
| 8 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 9 | + |
| 10 | +criterion_group!( |
| 11 | + benches, |
| 12 | + run_criteria_yes, |
| 13 | + run_criteria_no, |
| 14 | + run_criteria_yes_with_labels, |
| 15 | + run_criteria_no_with_labels, |
| 16 | + run_criteria_yes_with_query, |
| 17 | + run_criteria_yes_with_resource |
| 18 | +); |
| 19 | +criterion_main!(benches); |
| 20 | + |
| 21 | +fn run_stage(stage: &mut SystemStage, world: &mut World) { |
| 22 | + stage.run(world); |
| 23 | +} |
| 24 | + |
| 25 | +fn run_criteria_yes(criterion: &mut Criterion) { |
| 26 | + let mut world = World::new(); |
| 27 | + let mut group = criterion.benchmark_group("run_criteria/yes"); |
| 28 | + group.warm_up_time(std::time::Duration::from_millis(500)); |
| 29 | + group.measurement_time(std::time::Duration::from_secs(3)); |
| 30 | + fn empty() {} |
| 31 | + fn always_yes() -> ShouldRun { |
| 32 | + ShouldRun::Yes |
| 33 | + } |
| 34 | + for amount in 0..21 { |
| 35 | + let mut stage = SystemStage::parallel(); |
| 36 | + stage.add_system(empty.with_run_criteria(always_yes)); |
| 37 | + for _ in 0..amount { |
| 38 | + // TODO: should change this to use a label or have another bench that uses a label instead |
| 39 | + stage |
| 40 | + .add_system(empty.with_run_criteria(always_yes)) |
| 41 | + .add_system(empty.with_run_criteria(always_yes)) |
| 42 | + .add_system(empty.with_run_criteria(always_yes)) |
| 43 | + .add_system(empty.with_run_criteria(always_yes)) |
| 44 | + .add_system(empty.with_run_criteria(always_yes)); |
| 45 | + } |
| 46 | + // run once to initialize systems |
| 47 | + run_stage(&mut stage, &mut world); |
| 48 | + group.bench_function(&format!("{:03}_systems", 5 * amount + 1), |bencher| { |
| 49 | + bencher.iter(|| { |
| 50 | + run_stage(&mut stage, &mut world); |
| 51 | + }); |
| 52 | + }); |
| 53 | + } |
| 54 | + group.finish(); |
| 55 | +} |
| 56 | + |
| 57 | +fn run_criteria_no(criterion: &mut Criterion) { |
| 58 | + let mut world = World::new(); |
| 59 | + let mut group = criterion.benchmark_group("run_criteria/no"); |
| 60 | + group.warm_up_time(std::time::Duration::from_millis(500)); |
| 61 | + group.measurement_time(std::time::Duration::from_secs(3)); |
| 62 | + fn empty() {} |
| 63 | + fn always_no() -> ShouldRun { |
| 64 | + ShouldRun::No |
| 65 | + } |
| 66 | + for amount in 0..21 { |
| 67 | + let mut stage = SystemStage::parallel(); |
| 68 | + stage.add_system(empty.with_run_criteria(always_no)); |
| 69 | + for _ in 0..amount { |
| 70 | + stage |
| 71 | + .add_system(empty.with_run_criteria(always_no)) |
| 72 | + .add_system(empty.with_run_criteria(always_no)) |
| 73 | + .add_system(empty.with_run_criteria(always_no)) |
| 74 | + .add_system(empty.with_run_criteria(always_no)) |
| 75 | + .add_system(empty.with_run_criteria(always_no)); |
| 76 | + } |
| 77 | + // run once to initialize systems |
| 78 | + run_stage(&mut stage, &mut world); |
| 79 | + group.bench_function(&format!("{:03}_systems", 5 * amount + 1), |bencher| { |
| 80 | + bencher.iter(|| { |
| 81 | + run_stage(&mut stage, &mut world); |
| 82 | + }); |
| 83 | + }); |
| 84 | + } |
| 85 | + group.finish(); |
| 86 | +} |
| 87 | + |
| 88 | +fn run_criteria_yes_with_labels(criterion: &mut Criterion) { |
| 89 | + let mut world = World::new(); |
| 90 | + let mut group = criterion.benchmark_group("run_criteria/yes_with_labels"); |
| 91 | + group.warm_up_time(std::time::Duration::from_millis(500)); |
| 92 | + group.measurement_time(std::time::Duration::from_secs(3)); |
| 93 | + fn empty() {} |
| 94 | + fn always_yes() -> ShouldRun { |
| 95 | + ShouldRun::Yes |
| 96 | + } |
| 97 | + for amount in 0..21 { |
| 98 | + let mut stage = SystemStage::parallel(); |
| 99 | + stage.add_system(empty.with_run_criteria(always_yes.label("always yes"))); |
| 100 | + for _ in 0..amount { |
| 101 | + stage |
| 102 | + .add_system(empty.with_run_criteria("always yes")) |
| 103 | + .add_system(empty.with_run_criteria("always yes")) |
| 104 | + .add_system(empty.with_run_criteria("always yes")) |
| 105 | + .add_system(empty.with_run_criteria("always yes")) |
| 106 | + .add_system(empty.with_run_criteria("always yes")); |
| 107 | + } |
| 108 | + // run once to initialize systems |
| 109 | + run_stage(&mut stage, &mut world); |
| 110 | + group.bench_function(&format!("{:03}_systems", 5 * amount + 1), |bencher| { |
| 111 | + bencher.iter(|| { |
| 112 | + run_stage(&mut stage, &mut world); |
| 113 | + }); |
| 114 | + }); |
| 115 | + } |
| 116 | + group.finish(); |
| 117 | +} |
| 118 | + |
| 119 | +fn run_criteria_no_with_labels(criterion: &mut Criterion) { |
| 120 | + let mut world = World::new(); |
| 121 | + let mut group = criterion.benchmark_group("run_criteria/no_with_labels"); |
| 122 | + group.warm_up_time(std::time::Duration::from_millis(500)); |
| 123 | + group.measurement_time(std::time::Duration::from_secs(3)); |
| 124 | + fn empty() {} |
| 125 | + fn always_no() -> ShouldRun { |
| 126 | + ShouldRun::No |
| 127 | + } |
| 128 | + for amount in 0..21 { |
| 129 | + let mut stage = SystemStage::parallel(); |
| 130 | + stage.add_system(empty.with_run_criteria(always_no.label("always no"))); |
| 131 | + for _ in 0..amount { |
| 132 | + stage |
| 133 | + .add_system(empty.with_run_criteria("always no")) |
| 134 | + .add_system(empty.with_run_criteria("always no")) |
| 135 | + .add_system(empty.with_run_criteria("always no")) |
| 136 | + .add_system(empty.with_run_criteria("always no")) |
| 137 | + .add_system(empty.with_run_criteria("always no")); |
| 138 | + } |
| 139 | + // run once to initialize systems |
| 140 | + run_stage(&mut stage, &mut world); |
| 141 | + group.bench_function(&format!("{:03}_systems", 5 * amount + 1), |bencher| { |
| 142 | + bencher.iter(|| { |
| 143 | + run_stage(&mut stage, &mut world); |
| 144 | + }); |
| 145 | + }); |
| 146 | + } |
| 147 | + group.finish(); |
| 148 | +} |
| 149 | + |
| 150 | +#[derive(Component)] |
| 151 | +struct TestBool(pub bool); |
| 152 | + |
| 153 | +fn run_criteria_yes_with_query(criterion: &mut Criterion) { |
| 154 | + let mut world = World::new(); |
| 155 | + world.spawn().insert(TestBool(true)); |
| 156 | + let mut group = criterion.benchmark_group("run_criteria/yes_using_query"); |
| 157 | + group.warm_up_time(std::time::Duration::from_millis(500)); |
| 158 | + group.measurement_time(std::time::Duration::from_secs(3)); |
| 159 | + fn empty() {} |
| 160 | + fn yes_with_query(query: Query<&TestBool>) -> ShouldRun { |
| 161 | + let test_bool = query.single(); |
| 162 | + if test_bool.0 { |
| 163 | + ShouldRun::Yes |
| 164 | + } else { |
| 165 | + ShouldRun::No |
| 166 | + } |
| 167 | + } |
| 168 | + for amount in 0..21 { |
| 169 | + let mut stage = SystemStage::parallel(); |
| 170 | + stage.add_system(empty.with_run_criteria(yes_with_query)); |
| 171 | + for _ in 0..amount { |
| 172 | + stage |
| 173 | + .add_system(empty.with_run_criteria(yes_with_query)) |
| 174 | + .add_system(empty.with_run_criteria(yes_with_query)) |
| 175 | + .add_system(empty.with_run_criteria(yes_with_query)) |
| 176 | + .add_system(empty.with_run_criteria(yes_with_query)) |
| 177 | + .add_system(empty.with_run_criteria(yes_with_query)); |
| 178 | + } |
| 179 | + // run once to initialize systems |
| 180 | + run_stage(&mut stage, &mut world); |
| 181 | + group.bench_function(&format!("{:03}_systems", 5 * amount + 1), |bencher| { |
| 182 | + bencher.iter(|| { |
| 183 | + run_stage(&mut stage, &mut world); |
| 184 | + }); |
| 185 | + }); |
| 186 | + } |
| 187 | + group.finish(); |
| 188 | +} |
| 189 | + |
| 190 | +fn run_criteria_yes_with_resource(criterion: &mut Criterion) { |
| 191 | + let mut world = World::new(); |
| 192 | + world.insert_resource(TestBool(true)); |
| 193 | + let mut group = criterion.benchmark_group("run_criteria/yes_using_resource"); |
| 194 | + group.warm_up_time(std::time::Duration::from_millis(500)); |
| 195 | + group.measurement_time(std::time::Duration::from_secs(3)); |
| 196 | + fn empty() {} |
| 197 | + fn yes_with_resource(res: Res<TestBool>) -> ShouldRun { |
| 198 | + if res.0 { |
| 199 | + ShouldRun::Yes |
| 200 | + } else { |
| 201 | + ShouldRun::No |
| 202 | + } |
| 203 | + } |
| 204 | + for amount in 0..21 { |
| 205 | + let mut stage = SystemStage::parallel(); |
| 206 | + stage.add_system(empty.with_run_criteria(yes_with_resource)); |
| 207 | + for _ in 0..amount { |
| 208 | + stage |
| 209 | + .add_system(empty.with_run_criteria(yes_with_resource)) |
| 210 | + .add_system(empty.with_run_criteria(yes_with_resource)) |
| 211 | + .add_system(empty.with_run_criteria(yes_with_resource)) |
| 212 | + .add_system(empty.with_run_criteria(yes_with_resource)) |
| 213 | + .add_system(empty.with_run_criteria(yes_with_resource)); |
| 214 | + } |
| 215 | + // run once to initialize systems |
| 216 | + run_stage(&mut stage, &mut world); |
| 217 | + group.bench_function(&format!("{:03}_systems", 5 * amount + 1), |bencher| { |
| 218 | + bencher.iter(|| { |
| 219 | + run_stage(&mut stage, &mut world); |
| 220 | + }); |
| 221 | + }); |
| 222 | + } |
| 223 | + group.finish(); |
| 224 | +} |
0 commit comments