chore: update ablation and clean configs

This commit is contained in:
2026-04-15 14:48:59 +08:00
parent eaf574cd4e
commit 365ceac3be
15 changed files with 879 additions and 324 deletions

View File

@@ -6,6 +6,7 @@ use std::io::Write;
use kvcache_simulator::config::*;
use kvcache_simulator::driver;
use kvcache_simulator::replay::ReplayEvictPolicy;
fn base_config(trace_path: &str, out_dir: &str, mode: RouterMode) -> Config {
Config {
@@ -36,7 +37,9 @@ fn base_config(trace_path: &str, out_dir: &str, mode: RouterMode) -> Config {
},
cluster: ClusterConfig {
num_instances: 4,
meta_store: MetaStoreConfig { ttl_seconds: 1000.0 },
meta_store: MetaStoreConfig {
ttl_seconds: 1000.0,
},
router: RouterConfig {
mode,
precise_probe_latency_us: 10.0,
@@ -94,9 +97,11 @@ fn write_synthetic_trace(path: &std::path::Path) {
}
}
fn run(mode: RouterMode, trace_path: &std::path::Path, out_root: &std::path::Path)
-> kvcache_simulator::metrics::Summary
{
fn run(
mode: RouterMode,
trace_path: &std::path::Path,
out_root: &std::path::Path,
) -> kvcache_simulator::metrics::Summary {
let cfg = base_config(
trace_path.to_str().unwrap(),
out_root.to_str().unwrap(),
@@ -119,9 +124,8 @@ fn ablation_hit_rate_ordering() {
let s_ttl = run(RouterMode::TtlAware, &trace_path, &tmp);
let s_prec = run(RouterMode::Precise, &trace_path, &tmp);
let total_hit = |s: &kvcache_simulator::metrics::Summary| {
s.hit_rate_l0 + s.hit_rate_l1 + s.hit_rate_remote
};
let total_hit =
|s: &kvcache_simulator::metrics::Summary| s.hit_rate_l0 + s.hit_rate_l1 + s.hit_rate_remote;
let h_rand = total_hit(&s_random);
let h_ll = total_hit(&s_ll);
@@ -135,23 +139,79 @@ fn ablation_hit_rate_ordering() {
eprintln!(
" remote+local hit ratio L0/L1/remote: \
random=({:.2},{:.2},{:.2}) precise=({:.2},{:.2},{:.2})",
s_random.hit_rate_l0, s_random.hit_rate_l1, s_random.hit_rate_remote,
s_prec.hit_rate_l0, s_prec.hit_rate_l1, s_prec.hit_rate_remote,
s_random.hit_rate_l0,
s_random.hit_rate_l1,
s_random.hit_rate_remote,
s_prec.hit_rate_l0,
s_prec.hit_rate_l1,
s_prec.hit_rate_remote,
);
// ttl_aware and precise should outperform random / least_loaded for
// a workload built entirely of shared-prefix conversations.
let eps = 1e-6;
assert!(
h_ttl + eps >= h_rand,
"ttl_aware should >= random hit rate"
);
assert!(
h_prec + eps >= h_rand,
"precise should >= random hit rate"
);
assert!(h_ttl + eps >= h_rand, "ttl_aware should >= random hit rate");
assert!(h_prec + eps >= h_rand, "precise should >= random hit rate");
assert!(
h_prec + eps >= h_ll,
"precise should >= least_loaded hit rate"
);
}
#[test]
fn ablation_lru_preserves_ttft_fields() {
let tmp = std::env::temp_dir().join("kvcache_sim_replay");
let _ = std::fs::remove_dir_all(&tmp);
std::fs::create_dir_all(&tmp).unwrap();
let trace_path = tmp.join("trace.jsonl");
write_synthetic_trace(&trace_path);
let cfg = base_config(
trace_path.to_str().unwrap(),
tmp.to_str().unwrap(),
RouterMode::Random,
);
let online = driver::run(&cfg, Some("online_lru")).expect("online lru run");
let out = driver::ablate_fixed_placement(&cfg, &[RouterMode::Random], &[ReplayEvictPolicy::Lru])
.expect("ablate lru");
assert_eq!(out.len(), 1);
let row = &out[0];
let online_hit = online.summary.hit_rate_l0 + online.summary.hit_rate_l1 + online.summary.hit_rate_remote;
let ablate_hit = row.hit_rate_l0 + row.hit_rate_l1 + row.hit_rate_remote;
assert!(
(ablate_hit - online_hit).abs() < 1e-9,
"ablation lru should match online lru hit rate: online={online_hit} ablate={ablate_hit}"
);
assert!((row.ttft_mean - online.summary.ttft_mean).abs() < 1e-9);
assert!((row.ttft_p50 - online.summary.ttft_p50).abs() < 1e-9);
assert!((row.ttft_p95 - online.summary.ttft_p95).abs() < 1e-9);
assert!((row.ttft_p99 - online.summary.ttft_p99).abs() < 1e-9);
}
#[test]
fn ablate_rejects_belady_until_exact_algorithm_exists() {
let tmp = std::env::temp_dir().join("kvcache_sim_ablate_evict");
let _ = std::fs::remove_dir_all(&tmp);
std::fs::create_dir_all(&tmp).unwrap();
let trace_path = tmp.join("trace.jsonl");
write_synthetic_trace(&trace_path);
let cfg = base_config(
trace_path.to_str().unwrap(),
tmp.to_str().unwrap(),
RouterMode::Random,
);
let err = driver::ablate_fixed_placement(
&cfg,
&[RouterMode::Random],
&[ReplayEvictPolicy::Belady],
)
.expect_err("belady should be rejected");
assert!(
err.to_string().contains("exact belady"),
"unexpected error: {err:#}"
);
}