rust-rocksdb-zaidoon1/tests/test_perf.rs
Evan Jones 4b9ba976d6 MemoryUsageBuilder: Prevent outliving the DBs/caches (#1061)
This type must not outlive the DBs and caches that are added to it,
otherwise the caller will access invalid memory and likely crash.
Use PhantomData to ensure it does not outlive its arguments. This is
technically a breaking API change, but anything that used this code
in this way was likely broken anyway.

Co-authored-by: Oleksandr Anyshchenko <aanischenko@gmail.com>
2026-01-06 22:13:21 -05:00

25 lines
757 B
Rust

use rust_rocksdb::{
DB,
perf::{MemoryUsageBuilder, get_memory_usage_stats},
};
#[test]
fn test_memory_usage_builder() {
let tempdir = tempfile::tempdir().unwrap();
let db = DB::open_default(tempdir.path()).unwrap();
let mut builder = MemoryUsageBuilder::new().unwrap();
builder.add_db(&db);
let memory_usage = builder.build().unwrap();
assert!(memory_usage.approximate_mem_table_total() > 0);
// alternative non-builder approach
let memory_usage = get_memory_usage_stats(Some(&[&db]), None).unwrap();
assert!(memory_usage.mem_table_total > 0);
}
#[test]
fn test_memory_usage_builder_outlive_db() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/fail/memory_usage_builder_outlive_db.rs");
}