|
| 1 | +use bevy::ecs::{ |
| 2 | + system::{CommandQueue, Commands}, |
| 3 | + world::World, |
| 4 | +}; |
| 5 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 6 | +use rand::{rngs::StdRng, RngCore, SeedableRng}; |
| 7 | + |
| 8 | +criterion_group!( |
| 9 | + benches, |
| 10 | + empty_commands, |
| 11 | + spawning_commands, |
| 12 | + rng_spawning_commands |
| 13 | +); |
| 14 | +criterion_main!(benches); |
| 15 | + |
| 16 | +struct A; |
| 17 | +struct B; |
| 18 | +struct C; |
| 19 | + |
| 20 | +fn empty_commands(criterion: &mut Criterion) { |
| 21 | + let mut group = criterion.benchmark_group("empty_commands"); |
| 22 | + group.warm_up_time(std::time::Duration::from_millis(500)); |
| 23 | + group.measurement_time(std::time::Duration::from_secs(3)); |
| 24 | + |
| 25 | + group.bench_function("empty_commands", |bencher| { |
| 26 | + let mut world = World::default(); |
| 27 | + let mut command_queue = CommandQueue::default(); |
| 28 | + |
| 29 | + bencher.iter(|| { |
| 30 | + command_queue.apply(&mut world); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + group.finish(); |
| 35 | +} |
| 36 | + |
| 37 | +fn spawning_commands(criterion: &mut Criterion) { |
| 38 | + let mut group = criterion.benchmark_group("spawning_commands"); |
| 39 | + group.warm_up_time(std::time::Duration::from_millis(500)); |
| 40 | + group.measurement_time(std::time::Duration::from_secs(3)); |
| 41 | + |
| 42 | + for i in (1..5).map(|i| i * 2) { |
| 43 | + let entity_count = i * 1000; |
| 44 | + |
| 45 | + group.bench_function(format!("spawn_{}_entities", entity_count), |bencher| { |
| 46 | + let mut world = World::default(); |
| 47 | + let mut command_queue = CommandQueue::default(); |
| 48 | + |
| 49 | + bencher.iter(|| { |
| 50 | + let mut commands = Commands::new(&mut command_queue, &world); |
| 51 | + for _ in 0..entity_count { |
| 52 | + commands.spawn().insert(A).insert(B).insert(C).despawn(); |
| 53 | + } |
| 54 | + drop(commands); |
| 55 | + command_queue.apply(&mut world); |
| 56 | + }); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + group.finish(); |
| 61 | +} |
| 62 | + |
| 63 | +fn rng_spawning_commands(criterion: &mut Criterion) { |
| 64 | + let mut group = criterion.benchmark_group("spawning_commands"); |
| 65 | + group.warm_up_time(std::time::Duration::from_millis(500)); |
| 66 | + group.measurement_time(std::time::Duration::from_secs(3)); |
| 67 | + |
| 68 | + for i in (1..5).map(|i| i * 2) { |
| 69 | + let entity_count = i * 1000; |
| 70 | + |
| 71 | + group.bench_function(format!("spawn_{}_entities_rng", entity_count), |bencher| { |
| 72 | + let mut world = World::default(); |
| 73 | + let mut command_queue = CommandQueue::default(); |
| 74 | + |
| 75 | + let mut rng = StdRng::seed_from_u64(42); |
| 76 | + let mut rng_bool = || rng.next_u32() % 2 == 0; |
| 77 | + |
| 78 | + bencher.iter(|| { |
| 79 | + let mut commands = Commands::new(&mut command_queue, &world); |
| 80 | + for _ in 0..entity_count { |
| 81 | + let mut entity = commands.spawn(); |
| 82 | + |
| 83 | + if rng_bool() { |
| 84 | + entity.insert(A); |
| 85 | + } |
| 86 | + |
| 87 | + if rng_bool() { |
| 88 | + entity.insert(B); |
| 89 | + } |
| 90 | + |
| 91 | + if rng_bool() { |
| 92 | + entity.insert(C); |
| 93 | + } |
| 94 | + |
| 95 | + if rng_bool() { |
| 96 | + entity.despawn(); |
| 97 | + } |
| 98 | + } |
| 99 | + drop(commands); |
| 100 | + command_queue.apply(&mut world); |
| 101 | + }); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + group.finish(); |
| 106 | +} |
0 commit comments