use std::{alloc::System, time::Duration};
use criterion::{criterion_group, criterion_main, Criterion};
use tracking_allocator::{AllocationGroupId, AllocationRegistry, AllocationTracker, Allocator};
#[global_allocator]
static ALLOCATOR: Allocator<System> = Allocator::system();
struct NoopTracker;
impl AllocationTracker for NoopTracker {
fn allocated(
&self,
_addr: usize,
_object_size: usize,
_wrapped_size: usize,
_group_id: AllocationGroupId,
) {
}
fn deallocated(
&self,
_addr: usize,
_object_size: usize,
_wrapped_size: usize,
_source_group_id: AllocationGroupId,
_current_group_id: AllocationGroupId,
) {
}
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("disabled/no tracker", |b| {
AllocationRegistry::disable_tracking();
unsafe {
AllocationRegistry::clear_global_tracker();
}
b.iter(|| Vec::<String>::with_capacity(128));
});
c.bench_function("disabled/noop tracker", |b| {
AllocationRegistry::disable_tracking();
unsafe {
AllocationRegistry::clear_global_tracker();
}
let _ = AllocationRegistry::set_global_tracker(NoopTracker)
.expect("no other global tracker should be set");
b.iter(|| Vec::<String>::with_capacity(128));
});
c.bench_function("enabled/noop tracker", |b| {
unsafe {
AllocationRegistry::clear_global_tracker();
}
let _ = AllocationRegistry::set_global_tracker(NoopTracker)
.expect("no other global tracker should be set");
AllocationRegistry::enable_tracking();
b.iter(|| Vec::<String>::with_capacity(128));
});
}
criterion_group!(
name = benches;
config = Criterion::default()
.significance_level(0.02)
.noise_threshold(0.05)
.measurement_time(Duration::from_secs(30))
.warm_up_time(Duration::from_secs(10));
targets = criterion_benchmark
);
criterion_main!(benches);