test(memory): prefix serve shed/restore and a churn test under random shed directives (exact resumes, store inside its budget)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
713235d8ed
commit
b3f2e85ea9
@@ -764,6 +764,96 @@ mod tests {
|
||||
assert_eq!(hit.tokens, last);
|
||||
}
|
||||
|
||||
fn governor() -> leleka_memory::Governor {
|
||||
let mut cfg = leleka_memory::GovernorConfig::new(leleka_memory::Policy::for_host(121 << 30, false));
|
||||
cfg.log = false;
|
||||
leleka_memory::Governor::new(cfg, std::sync::Arc::new(leleka_memory::FakeProc::default()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_memory_shed_evicts_to_the_target_and_the_configured_budget_comes_back() {
|
||||
let g = governor();
|
||||
let mut s = PrefixServe::new(cfg(), 1024, 1 << 30, "test");
|
||||
let mut d = Fake::new();
|
||||
let prompts: Vec<Vec<i32>> = (1..=4).map(|i| session(i, 2, 6, 10)).collect();
|
||||
for p in &prompts {
|
||||
request(&mut s, &mut d, p, None);
|
||||
}
|
||||
let full = s.bytes();
|
||||
assert!(full > 0 && s.node_count() > 0);
|
||||
// shed to a quarter: evicted down to the target, bookkeeping intact
|
||||
let freed = s.apply_shed(Some(full as u64 / 4), &g);
|
||||
assert!(freed > 0 && s.bytes() <= full / 4, "{} > {}", s.bytes(), full / 4);
|
||||
s.store().check_invariants();
|
||||
// a request after the shed is still exact: whatever was evicted is read again
|
||||
let (_, digest) = request(&mut s, &mut d, &prompts[0], None);
|
||||
assert_eq!(digest, full_digest(&prompts[0]));
|
||||
assert!(s.bytes() <= s.budget_bytes(), "the shed budget bounds the store");
|
||||
// everything goes at reject; the next request reads from 0 and is exact
|
||||
s.apply_shed(Some(0), &g);
|
||||
assert_eq!((s.bytes(), s.node_count(), s.block_count()), (0, 0, 0));
|
||||
let (start, digest) = request(&mut s, &mut d, &prompts[1], None);
|
||||
assert_eq!(start, None);
|
||||
assert_eq!(digest, full_digest(&prompts[1]));
|
||||
assert_eq!(s.bytes(), 0, "nothing is admitted while the budget is zero");
|
||||
// pressure over: the configured budget is restored and the cache refills
|
||||
assert_eq!(s.apply_shed(None, &g), 0);
|
||||
assert_eq!(s.budget_bytes(), 1 << 30);
|
||||
request(&mut s, &mut d, &prompts[2], None);
|
||||
assert!(s.bytes() > 0);
|
||||
// a directive that asks for more than the configured budget never raises it
|
||||
s.apply_shed(Some(u64::MAX), &g);
|
||||
assert_eq!(s.budget_bytes(), 1 << 30);
|
||||
s.store().check_invariants();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn churn_under_random_shed_directives_keeps_every_resume_exact_and_the_store_inside_its_budget() {
|
||||
let g = governor();
|
||||
let budget = 2 * 100 + 12 * 1024;
|
||||
let mut s = PrefixServe::new(cfg(), 1024, budget as u64, "test");
|
||||
let mut d = Fake::new();
|
||||
let mut prompts: Vec<Vec<i32>> = (1..=5).map(|i| session(i, 2, 4, 10)).collect();
|
||||
let mut x = 0x2545_f491_4f6c_dd1du64;
|
||||
let mut next = move || {
|
||||
x ^= x << 13;
|
||||
x ^= x >> 7;
|
||||
x ^= x << 17;
|
||||
x
|
||||
};
|
||||
let mut shed_events = 0;
|
||||
for turn in 0..300 {
|
||||
let who = (next() % 5) as usize;
|
||||
match next() % 8 {
|
||||
0 => {
|
||||
s.apply_shed(Some(next() % (budget as u64 + 1)), &g);
|
||||
shed_events += 1;
|
||||
}
|
||||
1 => {
|
||||
s.apply_shed(None, &g);
|
||||
}
|
||||
2 if s.bytes() > 0 => {
|
||||
s.apply_shed(Some(0), &g);
|
||||
shed_events += 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let p = &mut prompts[who];
|
||||
if p.len() < 40 * G {
|
||||
p.extend(doc(turn + 7000, 1, 3));
|
||||
}
|
||||
let (_, digest) = request(&mut s, &mut d, p, if next() % 9 == 0 { Some(1) } else { None });
|
||||
// a cancelled request ends mid-prefill: its digest is a partial read, only complete ones are compared
|
||||
if digest == full_digest(p) {
|
||||
assert!(s.bytes() <= s.budget_bytes(), "turn {turn}: {} > {}", s.bytes(), s.budget_bytes());
|
||||
}
|
||||
s.store().check_invariants();
|
||||
assert!(s.bytes() <= budget, "turn {turn}: the store outgrew its configured budget");
|
||||
}
|
||||
assert!(shed_events > 20, "the schedule exercised shedding");
|
||||
assert!(s.stats().evicted > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn low_host_memory_skips_the_checkpoint_instead_of_building_it() {
|
||||
let mut s = serve();
|
||||
|
||||
Reference in New Issue
Block a user