use std::any::Any;
use peakmem_alloc::PeakMemAllocTrait;
use crate::{
bench_id::BenchId,
plugins::{EventListener, PerBenchData, PluginEvents},
};
pub struct PeakMemAllocPlugin {
alloc_per_bench: PerBenchData<Vec<usize>>,
alloc: &'static dyn PeakMemAllocTrait,
}
impl PeakMemAllocPlugin {
pub fn new(alloc: &'static dyn PeakMemAllocTrait) -> Self {
Self {
alloc_per_bench: PerBenchData::new(),
alloc,
}
}
pub fn get_by_bench_id(&self, bench_id: &BenchId) -> Option<&Vec<usize>> {
self.alloc_per_bench.get(bench_id)
}
}
pub static ALLOC_EVENT_LISTENER_NAME: &str = "_binggan_alloc";
impl EventListener for PeakMemAllocPlugin {
fn prio(&self) -> u32 {
u32::MAX - 2
}
fn as_any(&mut self) -> &mut dyn Any {
self
}
fn name(&self) -> &'static str {
ALLOC_EVENT_LISTENER_NAME
}
fn on_event(&mut self, event: PluginEvents) {
match event {
PluginEvents::BenchStart { bench_id } => {
self.alloc_per_bench.insert_if_absent(bench_id, Vec::new);
self.alloc.reset_peak_memory();
}
PluginEvents::BenchStop { bench_id, .. } => {
let perf = self.alloc_per_bench.get_mut(bench_id).unwrap();
perf.push(self.alloc.get_peak_memory());
}
_ => {}
}
}
}