From 4e6b4107cbea4c323c19280aa3736cd205746d32 Mon Sep 17 00:00:00 2001 From: Corwin Date: Fri, 6 Jan 2023 18:23:42 +0000 Subject: [PATCH] and test that benchmarks the allocator --- agb/src/agb_alloc/mod.rs | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/agb/src/agb_alloc/mod.rs b/agb/src/agb_alloc/mod.rs index c18fbbb7..53e429b0 100644 --- a/agb/src/agb_alloc/mod.rs +++ b/agb/src/agb_alloc/mod.rs @@ -251,4 +251,45 @@ mod test { "address of allocation should be within iwram, instead at {p:?}" ); } + + #[test_case] + fn benchmark_allocation(_gba: &mut crate::Gba) { + let mut stored: Vec> = Vec::new(); + + let mut rng = crate::rng::RandomNumberGenerator::new(); + + const MAX_VEC_LENGTH: usize = 100; + + enum Action { + Add { size: usize }, + Remove { index: usize }, + } + + let next_action = |rng: &mut crate::rng::RandomNumberGenerator, stored: &[Vec]| { + if stored.len() >= MAX_VEC_LENGTH { + Action::Remove { + index: (rng.gen() as usize) % stored.len(), + } + } else if stored.is_empty() || rng.gen() as usize % 4 != 0 { + Action::Add { + size: rng.gen() as usize % 32, + } + } else { + Action::Remove { + index: (rng.gen() as usize) % stored.len(), + } + } + }; + + for _ in 0..10000 { + match next_action(&mut rng, &stored) { + Action::Add { size } => { + stored.push(Vec::with_capacity(size)); + } + Action::Remove { index } => { + stored.swap_remove(index); + } + } + } + } }