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