#![cfg(feature = "symbolize")]
mod common;
use std::hint::black_box;
use stoolap_jemalloc::{Jemalloc, prof};
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc::new().with_profiling();
const INUSE_BYTES: usize = 3;
const BUFFERS: usize = 100_000;
#[inline(never)]
fn shrunk_buffers() -> Vec<Vec<u8>> {
let mut kept = Vec::with_capacity(BUFFERS);
let mut i = 0;
while i < BUFFERS {
let mut v: Vec<u8> = Vec::with_capacity(1024);
v.resize(1000, 1);
v.shrink_to_fit();
kept.push(black_box(v));
i += 1;
}
kept
}
#[test]
fn live_bytes_survive_reallocation() {
assert_eq!(prof::sample_interval(), prof::DEFAULT_INTERVAL);
let kept = shrunk_buffers();
let live = common::parse(&prof::dump_pprof().unwrap()).totals("shrunk_buffers")[INUSE_BYTES];
let want = (BUFFERS * 1024) as u64;
assert!(
live.abs_diff(want) * 100 <= want * 35,
"{live} live bytes for {want}"
);
drop(kept);
}