#![allow(clippy::unwrap_used)]
use dhat::{HeapStats, Profiler};
use osproxy_core::FieldName;
use osproxy_rewrite::{
construct_id, construct_id_bytes, inject_fields, inject_fields_bytes, map_logical_to_physical,
map_physical_to_logical, parse_bulk, parse_bulk_action, parse_mget, parse_msearch,
strip_fields, wrap_query,
};
use serde_json::{json, Value};
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
fn allocs(f: impl FnOnce()) -> u64 {
let before = HeapStats::get().total_blocks;
f();
HeapStats::get().total_blocks - before
}
#[test]
fn rewrite_hot_path_allocation_budgets() {
if std::env::var_os("LLVM_PROFILE_FILE").is_some() {
return;
}
let _profiler = Profiler::builder().testing().build();
document_transform_budgets();
bulk_path_budgets();
demux_parse_budgets();
streaming_invariant_budgets();
}
fn streaming_invariant_budgets() {
const SIZE_SLACK: u64 = 8;
let fields = vec![(FieldName::from("_tenant"), Value::from("acme"))];
let small = padded_doc(64);
let large = padded_doc(64 * 1024);
let inject_small = allocs(|| {
let _ = std::hint::black_box(inject_fields_bytes(&small, &fields).unwrap());
});
let inject_large = allocs(|| {
let _ = std::hint::black_box(inject_fields_bytes(&large, &fields).unwrap());
});
assert!(
inject_large <= inject_small + SIZE_SLACK,
"inject_fields_bytes allocations must not grow with body size (INV-MEM): \
small={inject_small} large={inject_large}"
);
let id_small = allocs(|| {
let _ = std::hint::black_box(construct_id_bytes("{partition}:{body.id}", "acme", &small));
});
let id_large = allocs(|| {
let _ = std::hint::black_box(construct_id_bytes("{partition}:{body.id}", "acme", &large));
});
assert!(
id_large <= id_small + SIZE_SLACK,
"construct_id_bytes allocations must not grow with body size (INV-MEM): \
small={id_small} large={id_large}"
);
}
fn padded_doc(size: usize) -> Vec<u8> {
let pad = size.saturating_sub(20).max(1);
format!(r#"{{"id":7,"data":"{}"}}"#, "x".repeat(pad)).into_bytes()
}
fn document_transform_budgets() {
let doc = json!({ "id": 7, "msg": "hi" });
let id_allocs = allocs(|| {
let _ = std::hint::black_box(construct_id("{partition}:{body.id}", "acme", &doc).unwrap());
});
assert!(
id_allocs <= 6,
"construct_id allocation budget: {id_allocs} > 6"
);
let mut target = json!({ "msg": "hi" });
let fields = vec![(FieldName::from("_tenant"), Value::from("acme"))];
let inj_allocs = allocs(|| {
inject_fields(&mut target, &fields).unwrap();
});
assert!(
inj_allocs <= 6,
"inject_fields allocation budget: {inj_allocs} > 6"
);
let mut hit = json!({ "_tenant": "acme", "msg": "hi" });
let names = vec![FieldName::from("_tenant")];
assert_eq!(
allocs(|| {
let _ = std::hint::black_box(strip_fields(&mut hit, &names));
}),
0,
"strip_fields must not allocate (NFR-P3)"
);
let body = br#"{"query":{"match":{"msg":"hi"}}}"#;
let filter = vec![(FieldName::from("_tenant"), Value::from("acme"))];
let n = allocs(|| {
let _ = std::hint::black_box(wrap_query(body, &filter).unwrap());
});
assert!(n <= 15, "wrap_query allocation budget: {n} > 15");
}
fn bulk_path_budgets() {
let l2p = allocs(|| {
let _ = std::hint::black_box(
map_logical_to_physical("{partition}:{body.id}", "acme", "7").unwrap(),
);
});
let p2l = allocs(|| {
let _ = std::hint::black_box(
map_physical_to_logical("{partition}:{body.id}", "acme", "acme:7").unwrap(),
);
});
let bulk_body =
b"{\"index\":{\"_id\":\"1\"}}\n{\"msg\":\"hi\"}\n{\"delete\":{\"_id\":\"2\"}}\n";
let bulk = allocs(|| {
let _ = std::hint::black_box(parse_bulk(bulk_body).unwrap());
});
assert!(
l2p <= 6,
"map_logical_to_physical allocation budget: {l2p} > 6"
);
assert!(
p2l <= 6,
"map_physical_to_logical allocation budget: {p2l} > 6"
);
assert!(bulk <= 20, "parse_bulk allocation budget: {bulk} > 20");
let action = br#"{"index":{"_index":"a","_id":"1"}}"#;
let source = br#"{"msg":"hi"}"#;
let stream_op = allocs(|| {
let parsed = parse_bulk_action(std::hint::black_box(action)).unwrap();
let _ = std::hint::black_box(parsed.into_item(Some(source)));
});
assert!(
stream_op <= 13,
"streaming bulk op parse allocation budget: {stream_op} > 13 \
(a double-parse of the action line would cost ~19)"
);
}
fn demux_parse_budgets() {
let mget_body = b"{\"docs\":[{\"_index\":\"a\",\"_id\":\"1\"}]}";
let mget = allocs(|| {
let _ = std::hint::black_box(parse_mget(mget_body).unwrap());
});
let msearch_body = b"{\"index\":\"a\"}\n{\"query\":{\"match_all\":{}}}\n";
let msearch = allocs(|| {
let _ = std::hint::black_box(parse_msearch(msearch_body).unwrap());
});
assert!(mget <= 16, "parse_mget allocation budget: {mget} > 16");
assert!(
msearch <= 16,
"parse_msearch allocation budget: {msearch} > 16"
);
}