From 0fc46dfa3f109ce72136ddea589cca0bdaa382a0 Mon Sep 17 00:00:00 2001 From: Milind Srivastava Date: Wed, 7 Oct 2026 12:47:54 -0400 Subject: [PATCH 1/3] rqe-optimizer: quantile_scenarios example sweeping lookback and interval Each scenario is 10 quantile_over_time RAQEs on distinct metrics sharing one (lookback, interval); the example solves each with the MILP and prints one row per scenario. Co-Authored-By: Claude Opus 5.5 --- rqe-optimizer/examples/quantile_scenarios.rs | 231 +++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 rqe-optimizer/examples/quantile_scenarios.rs diff --git a/rqe-optimizer/examples/quantile_scenarios.rs b/rqe-optimizer/examples/quantile_scenarios.rs new file mode 100644 index 00000000..7e4a1993 --- /dev/null +++ b/rqe-optimizer/examples/quantile_scenarios.rs @@ -0,0 +1,231 @@ +//! How the optimal deployment of a quantile workload moves with the query's +//! lookback and evaluation interval. +//! +//! Every scenario is one workload: [`QUERY_COUNT`] `quantile_over_time` +//! RAQEs, one per latency metric, all sharing the scenario's lookback and +//! interval. The metrics are distinct, so no deployment is shared across +//! queries. Each scenario is solved with the MILP and printed as one row. +//! +//! Run: +//! `cargo run --release -p rqe-optimizer --example quantile_scenarios -- +//! --cost-dir DIR --saturation-dir DIR [--w-cpu X] [--w-mem Y] +//! [--allow-undeployable-families]` + +use rqe_optimizer::candidates::build_all_candidates; +use rqe_optimizer::enumerate::unservable; +use rqe_optimizer::milp::{minimize, Objective}; +use rqe_optimizer::saturation::{DataShape, SaturationCurves}; +use rqe_optimizer::{ + validate_facts, AccuracyDirection, AtomicCostTable, Capability, Deployment, LabelSet, + MetricFacts, Raqe, WorkloadFacts, +}; + +const QUERY_COUNT: usize = 10; +const MINUTE_MS: u64 = 60_000; +const HOUR_MS: u64 = 60 * MINUTE_MS; +const DAY_MS: u64 = 24 * HOUR_MS; +const LOOKBACKS_MS: [u64; 5] = [5 * MINUTE_MS, HOUR_MS, 6 * HOUR_MS, DAY_MS, 7 * DAY_MS]; +const INTERVALS_MS: [u64; 3] = [15_000, MINUTE_MS, 5 * MINUTE_MS]; + +const RANK_ERROR_METRIC: &str = "mean_rank_err"; +const RANK_ERROR_SLA: f64 = 0.05; + +fn label_set(names: &[&str]) -> LabelSet { + names.iter().map(|name| name.to_string()).collect() +} + +fn metric_name(query_index: usize) -> String { + format!("service_{query_index}_request_duration_seconds") +} + +/// The flag's value, or `None` when the flag is absent. +fn flag_value(flag: &str) -> Option { + let args: Vec<_> = std::env::args().collect(); + let index = args.iter().position(|arg| arg == flag)?; + Some( + args.get(index + 1) + .unwrap_or_else(|| panic!("{flag} requires a value")) + .clone(), + ) +} + +fn weight_flag(flag: &str, default: f64) -> f64 { + flag_value(flag).map_or(default, |value| { + value + .parse::() + .ok() + .filter(|weight| *weight >= 0.0) + .unwrap_or_else(|| panic!("{flag} requires a non-negative number")) + }) +} + +fn reject_unknown_args() { + const SWITCHES: &[&str] = &["--allow-undeployable-families"]; + const WITH_VALUE: &[&str] = &["--cost-dir", "--saturation-dir", "--w-cpu", "--w-mem"]; + let mut args = std::env::args().skip(1); + while let Some(arg) = args.next() { + if WITH_VALUE.contains(&arg.as_str()) { + args.next(); + } else if !SWITCHES.contains(&arg.as_str()) { + panic!("unknown argument: {arg}"); + } + } +} + +fn load_cost_table() -> AtomicCostTable { + let cost_dir = flag_value("--cost-dir").expect("--cost-dir DIR is required"); + let path = std::path::Path::new(&cost_dir).join("rqe_atomic_costs.json"); + let raw = std::fs::read_to_string(&path) + .unwrap_or_else(|e| panic!("couldn't read {} ({e})", path.display())); + serde_json::from_str(&raw) + .unwrap_or_else(|e| panic!("{} isn't a valid AtomicCostTable: {e}", path.display())) +} + +fn load_saturation_curves(cost_table: &AtomicCostTable) -> SaturationCurves { + let dir = flag_value("--saturation-dir").expect("--saturation-dir DIR is required"); + let curves = SaturationCurves::load(std::path::Path::new(&dir)) + .unwrap_or_else(|e| panic!("couldn't load saturation curves: {e}")); + // ponytail: warn rather than panic, since a config with no curve is never + // eligible; panic like small_problem once the cost export matches the grid. + let check = curves.check_cost_table(cost_table); + for row in check.unchecked.iter().chain(&check.mismatched) { + eprintln!("cost table vs. saturation curves: {row}"); + } + curves +} + +/// Each metric: 5 services × 10 endpoints × 600 pods = 30,000 series scraped +/// every 15 s, with durations from 1 ms to 60 s, as in `small_problem`. +fn facts() -> WorkloadFacts { + (0..QUERY_COUNT) + .map(|query_index| { + let metric_facts = MetricFacts { + labels: label_set(&["service", "endpoint", "pod"]), + scrape_interval_ms: 15_000, + cardinality: [ + (label_set(&["service"]), 5), + (label_set(&["service", "endpoint"]), 50), + (label_set(&["service", "endpoint", "pod"]), 30_000), + ] + .into(), + value_range: Some((0.001, 60.0)), + data_shape: [( + label_set(&["service", "endpoint"]), + DataShape { + zipf_s: 1.1, + distinct_keys: 1_000.0, + tail_index: 1.5, + }, + )] + .into(), + }; + (metric_name(query_index), metric_facts) + }) + .collect() +} + +fn raqes(lookback_ms: u64, interval_ms: u64) -> Vec { + (0..QUERY_COUNT) + .map(|query_index| Raqe { + id: format!("p99_{query_index}"), + capability: Capability::Quantile, + lookback_ms, + interval_ms, + metric: metric_name(query_index), + spatial_filter: String::new(), + grouping_labels: label_set(&["service", "endpoint"]), + accuracy_metric: RANK_ERROR_METRIC.to_string(), + accuracy_sla: RANK_ERROR_SLA, + accuracy_direction: AccuracyDirection::LowerIsBetter, + latency_sla_ms: None, + }) + .collect() +} + +fn duration_label(ms: u64) -> String { + match ms { + _ if ms.is_multiple_of(DAY_MS) => format!("{}d", ms / DAY_MS), + _ if ms.is_multiple_of(HOUR_MS) => format!("{}h", ms / HOUR_MS), + _ if ms.is_multiple_of(MINUTE_MS) => format!("{}m", ms / MINUTE_MS), + _ => format!("{}s", ms / 1_000), + } +} + +fn describe(deployment: &Deployment) -> String { + format!( + "{} {} x={} y={}", + deployment.config.sketch, + deployment.config.sketch_config, + duration_label(deployment.window_ms), + duration_label(deployment.slide_ms), + ) +} + +fn main() { + reject_unknown_args(); + let cost_table = load_cost_table(); + let curves = load_saturation_curves(&cost_table); + let facts = facts(); + let objective = Objective::AUCCost { + w_cpu: weight_flag("--w-cpu", 1.0), + w_mem: weight_flag("--w-mem", 0.0), + }; + let allow_undeployable = std::env::args().any(|arg| arg == "--allow-undeployable-families"); + let accuracy = |raqe: &Raqe, deployment: &Deployment| curves.accuracy(raqe, deployment, &facts); + + println!("{QUERY_COUNT} quantile queries per scenario, {objective:?}"); + println!( + "{:<8} {:<8} {:>10} {:>10} {:>9} {:>8} {:>12} deployment(s)", + "lookback", "interval", "objective", "cpu", "mem_MB", "merged", "latency_ms" + ); + for lookback_ms in LOOKBACKS_MS { + for interval_ms in INTERVALS_MS { + let raqes = raqes(lookback_ms, interval_ms); + if let Err(problems) = validate_facts(&raqes, &facts) { + panic!("invalid workload facts: {problems:#?}"); + } + let deployments = + build_all_candidates(&raqes, &cost_table, &facts, allow_undeployable, &accuracy); + let scenario = format!( + "{:<8} {:<8}", + duration_label(lookback_ms), + duration_label(interval_ms) + ); + if !unservable(&raqes, &deployments, &facts, &accuracy).is_empty() { + println!("{scenario} unservable: no eligible deployment"); + continue; + } + let solution = match minimize(&raqes, &deployments, &facts, objective, &accuracy) { + Ok(solution) => solution, + Err(error) => { + println!("{scenario} infeasible: {error}"); + continue; + } + }; + // The queries are identical up to metric, so their choices + // normally agree; list every distinct one in case they don't. + let mut chosen: Vec = solution + .deployments + .iter() + .map(|planned| describe(&planned.deployment)) + .collect(); + chosen.sort(); + chosen.dedup(); + let plan_cost = &solution.plan_cost; + let max_latency_ms = plan_cost + .query_latency_ms + .iter() + .copied() + .fold(0.0, f64::max); + println!( + "{scenario} {:>10.3e} {:>10.3e} {:>9.1} {:>8} {:>12.3e} {}", + objective.value(plan_cost), + plan_cost.cpu_secs_per_sec(), + plan_cost.memory_bytes() / 1e6, + solution.raqes[0].merged_instance_count, + max_latency_ms, + chosen.join(" | "), + ); + } + } +} From 0a33779e94bbbe25376da06ee58d0d6b83d7f163 Mon Sep 17 00:00:00 2001 From: Milind Srivastava Date: Wed, 7 Oct 2026 12:50:12 -0400 Subject: [PATCH 2/3] rqe-optimizer: workload_scenarios example with grouping, type and SLA sweeps Renames quantile_scenarios to workload_scenarios and adds sweeps over mixed groupings, mixed query types, a latency SLA and an accuracy SLA. Each step prints the deployments the MILP runs and the queries each one serves. --sweep NAME runs one sweep. Co-Authored-By: Claude Opus 5.5 --- rqe-optimizer/examples/quantile_scenarios.rs | 231 --------- rqe-optimizer/examples/workload_scenarios.rs | 516 +++++++++++++++++++ 2 files changed, 516 insertions(+), 231 deletions(-) delete mode 100644 rqe-optimizer/examples/quantile_scenarios.rs create mode 100644 rqe-optimizer/examples/workload_scenarios.rs diff --git a/rqe-optimizer/examples/quantile_scenarios.rs b/rqe-optimizer/examples/quantile_scenarios.rs deleted file mode 100644 index 7e4a1993..00000000 --- a/rqe-optimizer/examples/quantile_scenarios.rs +++ /dev/null @@ -1,231 +0,0 @@ -//! How the optimal deployment of a quantile workload moves with the query's -//! lookback and evaluation interval. -//! -//! Every scenario is one workload: [`QUERY_COUNT`] `quantile_over_time` -//! RAQEs, one per latency metric, all sharing the scenario's lookback and -//! interval. The metrics are distinct, so no deployment is shared across -//! queries. Each scenario is solved with the MILP and printed as one row. -//! -//! Run: -//! `cargo run --release -p rqe-optimizer --example quantile_scenarios -- -//! --cost-dir DIR --saturation-dir DIR [--w-cpu X] [--w-mem Y] -//! [--allow-undeployable-families]` - -use rqe_optimizer::candidates::build_all_candidates; -use rqe_optimizer::enumerate::unservable; -use rqe_optimizer::milp::{minimize, Objective}; -use rqe_optimizer::saturation::{DataShape, SaturationCurves}; -use rqe_optimizer::{ - validate_facts, AccuracyDirection, AtomicCostTable, Capability, Deployment, LabelSet, - MetricFacts, Raqe, WorkloadFacts, -}; - -const QUERY_COUNT: usize = 10; -const MINUTE_MS: u64 = 60_000; -const HOUR_MS: u64 = 60 * MINUTE_MS; -const DAY_MS: u64 = 24 * HOUR_MS; -const LOOKBACKS_MS: [u64; 5] = [5 * MINUTE_MS, HOUR_MS, 6 * HOUR_MS, DAY_MS, 7 * DAY_MS]; -const INTERVALS_MS: [u64; 3] = [15_000, MINUTE_MS, 5 * MINUTE_MS]; - -const RANK_ERROR_METRIC: &str = "mean_rank_err"; -const RANK_ERROR_SLA: f64 = 0.05; - -fn label_set(names: &[&str]) -> LabelSet { - names.iter().map(|name| name.to_string()).collect() -} - -fn metric_name(query_index: usize) -> String { - format!("service_{query_index}_request_duration_seconds") -} - -/// The flag's value, or `None` when the flag is absent. -fn flag_value(flag: &str) -> Option { - let args: Vec<_> = std::env::args().collect(); - let index = args.iter().position(|arg| arg == flag)?; - Some( - args.get(index + 1) - .unwrap_or_else(|| panic!("{flag} requires a value")) - .clone(), - ) -} - -fn weight_flag(flag: &str, default: f64) -> f64 { - flag_value(flag).map_or(default, |value| { - value - .parse::() - .ok() - .filter(|weight| *weight >= 0.0) - .unwrap_or_else(|| panic!("{flag} requires a non-negative number")) - }) -} - -fn reject_unknown_args() { - const SWITCHES: &[&str] = &["--allow-undeployable-families"]; - const WITH_VALUE: &[&str] = &["--cost-dir", "--saturation-dir", "--w-cpu", "--w-mem"]; - let mut args = std::env::args().skip(1); - while let Some(arg) = args.next() { - if WITH_VALUE.contains(&arg.as_str()) { - args.next(); - } else if !SWITCHES.contains(&arg.as_str()) { - panic!("unknown argument: {arg}"); - } - } -} - -fn load_cost_table() -> AtomicCostTable { - let cost_dir = flag_value("--cost-dir").expect("--cost-dir DIR is required"); - let path = std::path::Path::new(&cost_dir).join("rqe_atomic_costs.json"); - let raw = std::fs::read_to_string(&path) - .unwrap_or_else(|e| panic!("couldn't read {} ({e})", path.display())); - serde_json::from_str(&raw) - .unwrap_or_else(|e| panic!("{} isn't a valid AtomicCostTable: {e}", path.display())) -} - -fn load_saturation_curves(cost_table: &AtomicCostTable) -> SaturationCurves { - let dir = flag_value("--saturation-dir").expect("--saturation-dir DIR is required"); - let curves = SaturationCurves::load(std::path::Path::new(&dir)) - .unwrap_or_else(|e| panic!("couldn't load saturation curves: {e}")); - // ponytail: warn rather than panic, since a config with no curve is never - // eligible; panic like small_problem once the cost export matches the grid. - let check = curves.check_cost_table(cost_table); - for row in check.unchecked.iter().chain(&check.mismatched) { - eprintln!("cost table vs. saturation curves: {row}"); - } - curves -} - -/// Each metric: 5 services × 10 endpoints × 600 pods = 30,000 series scraped -/// every 15 s, with durations from 1 ms to 60 s, as in `small_problem`. -fn facts() -> WorkloadFacts { - (0..QUERY_COUNT) - .map(|query_index| { - let metric_facts = MetricFacts { - labels: label_set(&["service", "endpoint", "pod"]), - scrape_interval_ms: 15_000, - cardinality: [ - (label_set(&["service"]), 5), - (label_set(&["service", "endpoint"]), 50), - (label_set(&["service", "endpoint", "pod"]), 30_000), - ] - .into(), - value_range: Some((0.001, 60.0)), - data_shape: [( - label_set(&["service", "endpoint"]), - DataShape { - zipf_s: 1.1, - distinct_keys: 1_000.0, - tail_index: 1.5, - }, - )] - .into(), - }; - (metric_name(query_index), metric_facts) - }) - .collect() -} - -fn raqes(lookback_ms: u64, interval_ms: u64) -> Vec { - (0..QUERY_COUNT) - .map(|query_index| Raqe { - id: format!("p99_{query_index}"), - capability: Capability::Quantile, - lookback_ms, - interval_ms, - metric: metric_name(query_index), - spatial_filter: String::new(), - grouping_labels: label_set(&["service", "endpoint"]), - accuracy_metric: RANK_ERROR_METRIC.to_string(), - accuracy_sla: RANK_ERROR_SLA, - accuracy_direction: AccuracyDirection::LowerIsBetter, - latency_sla_ms: None, - }) - .collect() -} - -fn duration_label(ms: u64) -> String { - match ms { - _ if ms.is_multiple_of(DAY_MS) => format!("{}d", ms / DAY_MS), - _ if ms.is_multiple_of(HOUR_MS) => format!("{}h", ms / HOUR_MS), - _ if ms.is_multiple_of(MINUTE_MS) => format!("{}m", ms / MINUTE_MS), - _ => format!("{}s", ms / 1_000), - } -} - -fn describe(deployment: &Deployment) -> String { - format!( - "{} {} x={} y={}", - deployment.config.sketch, - deployment.config.sketch_config, - duration_label(deployment.window_ms), - duration_label(deployment.slide_ms), - ) -} - -fn main() { - reject_unknown_args(); - let cost_table = load_cost_table(); - let curves = load_saturation_curves(&cost_table); - let facts = facts(); - let objective = Objective::AUCCost { - w_cpu: weight_flag("--w-cpu", 1.0), - w_mem: weight_flag("--w-mem", 0.0), - }; - let allow_undeployable = std::env::args().any(|arg| arg == "--allow-undeployable-families"); - let accuracy = |raqe: &Raqe, deployment: &Deployment| curves.accuracy(raqe, deployment, &facts); - - println!("{QUERY_COUNT} quantile queries per scenario, {objective:?}"); - println!( - "{:<8} {:<8} {:>10} {:>10} {:>9} {:>8} {:>12} deployment(s)", - "lookback", "interval", "objective", "cpu", "mem_MB", "merged", "latency_ms" - ); - for lookback_ms in LOOKBACKS_MS { - for interval_ms in INTERVALS_MS { - let raqes = raqes(lookback_ms, interval_ms); - if let Err(problems) = validate_facts(&raqes, &facts) { - panic!("invalid workload facts: {problems:#?}"); - } - let deployments = - build_all_candidates(&raqes, &cost_table, &facts, allow_undeployable, &accuracy); - let scenario = format!( - "{:<8} {:<8}", - duration_label(lookback_ms), - duration_label(interval_ms) - ); - if !unservable(&raqes, &deployments, &facts, &accuracy).is_empty() { - println!("{scenario} unservable: no eligible deployment"); - continue; - } - let solution = match minimize(&raqes, &deployments, &facts, objective, &accuracy) { - Ok(solution) => solution, - Err(error) => { - println!("{scenario} infeasible: {error}"); - continue; - } - }; - // The queries are identical up to metric, so their choices - // normally agree; list every distinct one in case they don't. - let mut chosen: Vec = solution - .deployments - .iter() - .map(|planned| describe(&planned.deployment)) - .collect(); - chosen.sort(); - chosen.dedup(); - let plan_cost = &solution.plan_cost; - let max_latency_ms = plan_cost - .query_latency_ms - .iter() - .copied() - .fold(0.0, f64::max); - println!( - "{scenario} {:>10.3e} {:>10.3e} {:>9.1} {:>8} {:>12.3e} {}", - objective.value(plan_cost), - plan_cost.cpu_secs_per_sec(), - plan_cost.memory_bytes() / 1e6, - solution.raqes[0].merged_instance_count, - max_latency_ms, - chosen.join(" | "), - ); - } - } -} diff --git a/rqe-optimizer/examples/workload_scenarios.rs b/rqe-optimizer/examples/workload_scenarios.rs new file mode 100644 index 00000000..720375e6 --- /dev/null +++ b/rqe-optimizer/examples/workload_scenarios.rs @@ -0,0 +1,516 @@ +//! How the optimal deployment moves as the query workload changes: query +//! types, groupings, lookbacks, repetition intervals, and accuracy and +//! latency SLAs. Each sweep steps one knob of a base workload; every step is +//! one workload solved with the MILP and printed as the deployments it runs +//! and the queries each one serves. +//! +//! A deployment serves only queries with its own capability, metric and +//! grouping (`candidates::is_eligible`), so the mixed-grouping and +//! mixed-type sweeps cost the same as solving each part alone. They are +//! baselines for when the model shares across groupings. +//! +//! Run: +//! `cargo run --release -p rqe-optimizer --example workload_scenarios -- +//! --cost-dir DIR --saturation-dir DIR [--w-cpu X] [--w-mem Y] +//! [--allow-undeployable-families] [--sweep NAME]` + +use rqe_optimizer::candidates::build_all_candidates; +use rqe_optimizer::enumerate::unservable; +use rqe_optimizer::milp::{minimize, Objective}; +use rqe_optimizer::saturation::{DataShape, SaturationCurves}; +use rqe_optimizer::{ + validate_facts, AccuracyDirection, AtomicCostTable, Capability, Deployment, LabelSet, + MetricFacts, Raqe, WorkloadFacts, +}; + +const MINUTE_MS: u64 = 60_000; +const HOUR_MS: u64 = 60 * MINUTE_MS; +const DAY_MS: u64 = 24 * HOUR_MS; + +const REQUESTS: &str = "http_requests_total"; +const DURATION: &str = "http_request_duration_seconds"; +/// The lookback × interval sweep's queries each read their own metric, so +/// none of them share a deployment. +const PER_SERVICE_METRIC_COUNT: usize = 10; + +fn per_service_metric(service_index: usize) -> String { + format!("service_{service_index}_request_duration_seconds") +} + +fn label_set(names: &[&str]) -> LabelSet { + names.iter().map(|name| name.to_string()).collect() +} + +fn by_service() -> LabelSet { + label_set(&["service"]) +} + +fn by_service_endpoint() -> LabelSet { + label_set(&["service", "endpoint"]) +} + +/// One query, with the accuracy metric, SLA and direction its capability +/// reports. Sweeps adjust the returned value. +fn query( + id: &str, + capability: Capability, + metric: &str, + grouping_labels: LabelSet, + lookback_ms: u64, + interval_ms: u64, +) -> Raqe { + let (accuracy_metric, accuracy_sla, accuracy_direction) = match capability { + Capability::Quantile => ("mean_rank_err", 0.05, AccuracyDirection::LowerIsBetter), + Capability::TopKByValue => ("precision_at_k", 0.9, AccuracyDirection::HigherIsBetter), + _ => ("relative_error", 0.1, AccuracyDirection::LowerIsBetter), + }; + Raqe { + id: id.to_string(), + capability, + lookback_ms, + interval_ms, + metric: metric.to_string(), + spatial_filter: String::new(), + grouping_labels, + accuracy_metric: accuracy_metric.to_string(), + accuracy_sla, + accuracy_direction, + latency_sla_ms: None, + } +} + +fn p99(id: &str, lookback_ms: u64) -> Raqe { + query( + id, + Capability::Quantile, + DURATION, + by_service_endpoint(), + lookback_ms, + MINUTE_MS, + ) +} + +fn distinct_endpoints(id: &str, lookback_ms: u64) -> Raqe { + query( + id, + Capability::Cardinality, + REQUESTS, + by_service(), + lookback_ms, + MINUTE_MS, + ) +} + +/// p99 latency over three lookbacks of one metric: candidates for sharing +/// one deployment. +fn shared_p99_workload() -> Vec { + vec![ + p99("p99_1h", HOUR_MS), + p99("p99_6h", 6 * HOUR_MS), + p99("p99_1d", DAY_MS), + ] +} + +/// Distinct endpoints per service over three lookbacks of one metric. +fn shared_distinct_workload() -> Vec { + vec![ + distinct_endpoints("distinct_1h", HOUR_MS), + distinct_endpoints("distinct_6h", 6 * HOUR_MS), + distinct_endpoints("distinct_1d", DAY_MS), + ] +} + +struct Scenario { + label: String, + raqes: Vec, +} + +struct Sweep { + name: &'static str, + description: &'static str, + scenarios: Vec, +} + +fn lookback_interval_sweep() -> Sweep { + let mut scenarios = Vec::new(); + for lookback_ms in [5 * MINUTE_MS, HOUR_MS, 6 * HOUR_MS, DAY_MS, 7 * DAY_MS] { + for interval_ms in [15_000, MINUTE_MS, 5 * MINUTE_MS] { + let raqes = (0..PER_SERVICE_METRIC_COUNT) + .map(|service_index| { + query( + &format!("p99_service_{service_index}"), + Capability::Quantile, + &per_service_metric(service_index), + by_service_endpoint(), + lookback_ms, + interval_ms, + ) + }) + .collect(); + scenarios.push(Scenario { + label: format!( + "lookback {} interval {}", + duration_label(lookback_ms), + duration_label(interval_ms) + ), + raqes, + }); + } + } + Sweep { + name: "lookback-interval", + description: "10 p99 queries on distinct metrics, all with one lookback and interval", + scenarios, + } +} + +fn mixed_grouping_sweep() -> Sweep { + let p99_by = |grouping_labels: LabelSet, id: &str| { + query( + id, + Capability::Quantile, + DURATION, + grouping_labels, + HOUR_MS, + MINUTE_MS, + ) + }; + let by_service_query = || p99_by(by_service(), "p99_1h_by_service"); + let by_service_endpoint_query = || p99_by(by_service_endpoint(), "p99_1h_by_endpoint"); + Sweep { + name: "mixed-grouping", + description: "1h p99 by (service), by (service, endpoint), and both together", + scenarios: vec![ + Scenario { + label: "by service".to_string(), + raqes: vec![by_service_query()], + }, + Scenario { + label: "by service, endpoint".to_string(), + raqes: vec![by_service_endpoint_query()], + }, + Scenario { + label: "both".to_string(), + raqes: vec![by_service_query(), by_service_endpoint_query()], + }, + ], + } +} + +fn mixed_type_sweep() -> Sweep { + let rate = || { + query( + "rate_1h", + Capability::RateOrIncrease, + REQUESTS, + by_service_endpoint(), + HOUR_MS, + MINUTE_MS, + ) + }; + let quantile = || p99("p99_1h", HOUR_MS); + let distinct = || distinct_endpoints("distinct_1h", HOUR_MS); + let top_endpoints = || { + query( + "topk_1h", + Capability::TopKByValue, + REQUESTS, + by_service_endpoint(), + HOUR_MS, + MINUTE_MS, + ) + }; + let alone = |raqe: Raqe| Scenario { + label: format!("{} alone", raqe.id), + raqes: vec![raqe], + }; + Sweep { + name: "mixed-type", + description: "1h rate, p99, distinct count and top-k, alone and together", + scenarios: vec![ + alone(rate()), + alone(quantile()), + alone(distinct()), + alone(top_endpoints()), + Scenario { + label: "rate + p99 + distinct".to_string(), + raqes: vec![rate(), quantile(), distinct()], + }, + Scenario { + label: "all four".to_string(), + raqes: vec![rate(), quantile(), distinct(), top_endpoints()], + }, + ], + } +} + +fn latency_sla_sweep() -> Sweep { + let scenarios = [None, Some(5.0), Some(2.0), Some(1.0), Some(0.5), Some(0.2)] + .into_iter() + .map(|latency_sla_ms| { + let mut raqes = shared_p99_workload(); + raqes[0].latency_sla_ms = latency_sla_ms; + Scenario { + label: match latency_sla_ms { + Some(limit) => format!("p99_1h latency <= {limit} ms"), + None => "no latency SLA".to_string(), + }, + raqes, + } + }) + .collect(); + Sweep { + name: "latency-sla", + description: "p99 over 1h/6h/1d of one metric; tighten the latency SLA of p99_1h", + scenarios, + } +} + +fn accuracy_sla_sweep() -> Sweep { + let tightened = |mut raqes: Vec, accuracy_sla: f64| { + raqes[0].accuracy_sla = accuracy_sla; + Scenario { + label: format!( + "{} {} <= {accuracy_sla}", + raqes[0].id, raqes[0].accuracy_metric + ), + raqes, + } + }; + let quantile_scenarios = [0.05, 0.01, 0.005, 0.002, 0.001] + .into_iter() + .map(|accuracy_sla| tightened(shared_p99_workload(), accuracy_sla)); + let distinct_scenarios = [0.1, 0.05, 0.02, 0.01] + .into_iter() + .map(|accuracy_sla| tightened(shared_distinct_workload(), accuracy_sla)); + Sweep { + name: "accuracy-sla", + description: "p99 and distinct count over 1h/6h/1d of one metric; tighten the 1h query's accuracy SLA", + scenarios: quantile_scenarios.chain(distinct_scenarios).collect(), + } +} + +/// Every metric: 5 services × 10 endpoints × 600 pods = 30,000 series scraped +/// every 15 s, as in `small_problem`. Durations range from 1 ms to 60 s. +fn facts() -> WorkloadFacts { + let metric_facts = |value_range| MetricFacts { + labels: label_set(&["service", "endpoint", "pod"]), + scrape_interval_ms: 15_000, + cardinality: [ + (by_service(), 5), + (by_service_endpoint(), 50), + (label_set(&["service", "endpoint", "pod"]), 30_000), + ] + .into(), + value_range, + data_shape: [ + (by_service(), data_shape(10_000.0)), + (by_service_endpoint(), data_shape(1_000.0)), + ] + .into(), + }; + let duration_range = Some((0.001, 60.0)); + let per_service_metrics = (0..PER_SERVICE_METRIC_COUNT).map(|service_index| { + ( + per_service_metric(service_index), + metric_facts(duration_range), + ) + }); + [ + (REQUESTS.to_string(), metric_facts(None)), + (DURATION.to_string(), metric_facts(duration_range)), + ] + .into_iter() + .chain(per_service_metrics) + .collect() +} + +fn data_shape(distinct_keys: f64) -> DataShape { + DataShape { + zipf_s: 1.1, + distinct_keys, + tail_index: 1.5, + } +} + +/// The flag's value, or `None` when the flag is absent. +fn flag_value(flag: &str) -> Option { + let args: Vec<_> = std::env::args().collect(); + let index = args.iter().position(|arg| arg == flag)?; + Some( + args.get(index + 1) + .unwrap_or_else(|| panic!("{flag} requires a value")) + .clone(), + ) +} + +fn weight_flag(flag: &str, default: f64) -> f64 { + flag_value(flag).map_or(default, |value| { + value + .parse::() + .ok() + .filter(|weight| *weight >= 0.0) + .unwrap_or_else(|| panic!("{flag} requires a non-negative number")) + }) +} + +fn reject_unknown_args() { + const SWITCHES: &[&str] = &["--allow-undeployable-families"]; + const WITH_VALUE: &[&str] = &[ + "--cost-dir", + "--saturation-dir", + "--sweep", + "--w-cpu", + "--w-mem", + ]; + let mut args = std::env::args().skip(1); + while let Some(arg) = args.next() { + if WITH_VALUE.contains(&arg.as_str()) { + args.next(); + } else if !SWITCHES.contains(&arg.as_str()) { + panic!("unknown argument: {arg}"); + } + } +} + +fn load_cost_table() -> AtomicCostTable { + let cost_dir = flag_value("--cost-dir").expect("--cost-dir DIR is required"); + let path = std::path::Path::new(&cost_dir).join("rqe_atomic_costs.json"); + let raw = std::fs::read_to_string(&path) + .unwrap_or_else(|e| panic!("couldn't read {} ({e})", path.display())); + serde_json::from_str(&raw) + .unwrap_or_else(|e| panic!("{} isn't a valid AtomicCostTable: {e}", path.display())) +} + +fn load_saturation_curves(cost_table: &AtomicCostTable) -> SaturationCurves { + let dir = flag_value("--saturation-dir").expect("--saturation-dir DIR is required"); + let curves = SaturationCurves::load(std::path::Path::new(&dir)) + .unwrap_or_else(|e| panic!("couldn't load saturation curves: {e}")); + // ponytail: warn rather than panic, since a config with no curve is never + // eligible; panic like small_problem once the cost export matches the grid. + let check = curves.check_cost_table(cost_table); + for row in check.unchecked.iter().chain(&check.mismatched) { + eprintln!("cost table vs. saturation curves: {row}"); + } + curves +} + +fn duration_label(ms: u64) -> String { + match ms { + _ if ms.is_multiple_of(DAY_MS) => format!("{}d", ms / DAY_MS), + _ if ms.is_multiple_of(HOUR_MS) => format!("{}h", ms / HOUR_MS), + _ if ms.is_multiple_of(MINUTE_MS) => format!("{}m", ms / MINUTE_MS), + _ => format!("{}s", ms / 1_000), + } +} + +fn describe(deployment: &Deployment) -> String { + format!( + "{} {} by {:?} x={} y={}", + deployment.config.sketch, + deployment.config.sketch_config, + deployment.grouping_labels, + duration_label(deployment.window_ms), + duration_label(deployment.slide_ms), + ) +} + +fn solve_and_print( + scenario: &Scenario, + cost_table: &AtomicCostTable, + curves: &SaturationCurves, + facts: &WorkloadFacts, + objective: Objective, + allow_undeployable: bool, +) { + let raqes = &scenario.raqes; + if let Err(problems) = validate_facts(raqes, facts) { + panic!("invalid workload facts: {problems:#?}"); + } + let accuracy = |raqe: &Raqe, deployment: &Deployment| curves.accuracy(raqe, deployment, facts); + let deployments = build_all_candidates(raqes, cost_table, facts, allow_undeployable, &accuracy); + let missing = unservable(raqes, &deployments, facts, &accuracy); + if !missing.is_empty() { + println!("[{}] unservable: {missing:?}", scenario.label); + return; + } + let solution = match minimize(raqes, &deployments, facts, objective, &accuracy) { + Ok(solution) => solution, + Err(error) => { + println!("[{}] infeasible: {error}", scenario.label); + return; + } + }; + let plan_cost = &solution.plan_cost; + println!( + "[{}] objective={:.3e} cpu={:.3e} cpu-sec/sec memory={:.1}MB deployments={}", + scenario.label, + objective.value(plan_cost), + plan_cost.cpu_secs_per_sec(), + plan_cost.memory_bytes() / 1e6, + solution.deployments.len(), + ); + for (deployment_index, planned) in solution.deployments.iter().enumerate() { + println!(" D{deployment_index}: {}", describe(&planned.deployment)); + for ((raqe, planned_raqe), latency_ms) in raqes + .iter() + .zip(&solution.raqes) + .zip(&plan_cost.query_latency_ms) + .filter(|((_, planned_raqe), _)| planned_raqe.deployment == deployment_index) + { + let achieved = accuracy(raqe, &planned.deployment) + .map_or("unknown".to_string(), |value| format!("{value:.4}")); + println!( + " {:<22} merged={:<4} latency={latency_ms:.3e} ms {}={achieved} (sla {})", + raqe.id, + planned_raqe.merged_instance_count, + raqe.accuracy_metric, + raqe.accuracy_sla, + ); + } + } +} + +fn main() { + reject_unknown_args(); + let cost_table = load_cost_table(); + let curves = load_saturation_curves(&cost_table); + let facts = facts(); + let objective = Objective::AUCCost { + w_cpu: weight_flag("--w-cpu", 1.0), + w_mem: weight_flag("--w-mem", 0.0), + }; + let allow_undeployable = std::env::args().any(|arg| arg == "--allow-undeployable-families"); + let sweeps = [ + lookback_interval_sweep(), + mixed_grouping_sweep(), + mixed_type_sweep(), + latency_sla_sweep(), + accuracy_sla_sweep(), + ]; + let selected = flag_value("--sweep"); + if let Some(name) = &selected { + assert!( + sweeps.iter().any(|sweep| sweep.name == name), + "unknown --sweep {name}; expected one of {:?}", + sweeps.iter().map(|sweep| sweep.name).collect::>() + ); + } + println!("{objective:?}"); + for sweep in sweeps + .iter() + .filter(|sweep| selected.as_ref().is_none_or(|name| sweep.name == name)) + { + println!("\n=== {}: {} ===", sweep.name, sweep.description); + for scenario in &sweep.scenarios { + solve_and_print( + scenario, + &cost_table, + &curves, + &facts, + objective, + allow_undeployable, + ); + } + } +} From c80789b29a679149087ecd38322c54776172c474 Mon Sep 17 00:00:00 2001 From: Milind Srivastava Date: Fri, 9 Oct 2026 11:20:39 -0400 Subject: [PATCH 3/3] rqe-optimizer: workload_scenarios follows per-family accuracy and the study's cost table Raqe no longer names an accuracy metric, so each query keeps only its SLA and the output prints the chosen family's metric. The cost table is read from the saturation directory, so --cost-dir is gone. Co-Authored-By: Claude Opus 5.5 --- rqe-optimizer/examples/workload_scenarios.rs | 53 ++++++++------------ 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/rqe-optimizer/examples/workload_scenarios.rs b/rqe-optimizer/examples/workload_scenarios.rs index 720375e6..8faa735e 100644 --- a/rqe-optimizer/examples/workload_scenarios.rs +++ b/rqe-optimizer/examples/workload_scenarios.rs @@ -11,16 +11,16 @@ //! //! Run: //! `cargo run --release -p rqe-optimizer --example workload_scenarios -- -//! --cost-dir DIR --saturation-dir DIR [--w-cpu X] [--w-mem Y] +//! --saturation-dir DIR [--w-cpu X] [--w-mem Y] //! [--allow-undeployable-families] [--sweep NAME]` use rqe_optimizer::candidates::build_all_candidates; use rqe_optimizer::enumerate::unservable; use rqe_optimizer::milp::{minimize, Objective}; -use rqe_optimizer::saturation::{DataShape, SaturationCurves}; +use rqe_optimizer::saturation::{DataShape, SaturationCurves, COST_TABLE}; use rqe_optimizer::{ - validate_facts, AccuracyDirection, AtomicCostTable, Capability, Deployment, LabelSet, - MetricFacts, Raqe, WorkloadFacts, + accuracy_key, validate_facts, AtomicCostTable, Capability, Deployment, LabelSet, MetricFacts, + Raqe, WorkloadFacts, }; const MINUTE_MS: u64 = 60_000; @@ -49,8 +49,9 @@ fn by_service_endpoint() -> LabelSet { label_set(&["service", "endpoint"]) } -/// One query, with the accuracy metric, SLA and direction its capability -/// reports. Sweeps adjust the returned value. +/// One query, with a default accuracy SLA for its capability. Each candidate +/// checks the SLA against its own family's accuracy metric +/// (`rqe_optimizer::accuracy_key`). Sweeps adjust the returned value. fn query( id: &str, capability: Capability, @@ -59,10 +60,10 @@ fn query( lookback_ms: u64, interval_ms: u64, ) -> Raqe { - let (accuracy_metric, accuracy_sla, accuracy_direction) = match capability { - Capability::Quantile => ("mean_rank_err", 0.05, AccuracyDirection::LowerIsBetter), - Capability::TopKByValue => ("precision_at_k", 0.9, AccuracyDirection::HigherIsBetter), - _ => ("relative_error", 0.1, AccuracyDirection::LowerIsBetter), + let accuracy_sla = match capability { + Capability::Quantile => 0.05, + Capability::TopKByValue => 0.9, + _ => 0.1, }; Raqe { id: id.to_string(), @@ -72,10 +73,9 @@ fn query( metric: metric.to_string(), spatial_filter: String::new(), grouping_labels, - accuracy_metric: accuracy_metric.to_string(), accuracy_sla, - accuracy_direction, latency_sla_ms: None, + topk_k: None, } } @@ -270,10 +270,7 @@ fn accuracy_sla_sweep() -> Sweep { let tightened = |mut raqes: Vec, accuracy_sla: f64| { raqes[0].accuracy_sla = accuracy_sla; Scenario { - label: format!( - "{} {} <= {accuracy_sla}", - raqes[0].id, raqes[0].accuracy_metric - ), + label: format!("{} accuracy SLA {accuracy_sla}", raqes[0].id), raqes, } }; @@ -356,13 +353,7 @@ fn weight_flag(flag: &str, default: f64) -> f64 { fn reject_unknown_args() { const SWITCHES: &[&str] = &["--allow-undeployable-families"]; - const WITH_VALUE: &[&str] = &[ - "--cost-dir", - "--saturation-dir", - "--sweep", - "--w-cpu", - "--w-mem", - ]; + const WITH_VALUE: &[&str] = &["--saturation-dir", "--sweep", "--w-cpu", "--w-mem"]; let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { if WITH_VALUE.contains(&arg.as_str()) { @@ -373,9 +364,12 @@ fn reject_unknown_args() { } } +fn saturation_dir() -> String { + flag_value("--saturation-dir").expect("--saturation-dir DIR is required") +} + fn load_cost_table() -> AtomicCostTable { - let cost_dir = flag_value("--cost-dir").expect("--cost-dir DIR is required"); - let path = std::path::Path::new(&cost_dir).join("rqe_atomic_costs.json"); + let path = std::path::Path::new(&saturation_dir()).join(COST_TABLE); let raw = std::fs::read_to_string(&path) .unwrap_or_else(|e| panic!("couldn't read {} ({e})", path.display())); serde_json::from_str(&raw) @@ -383,8 +377,7 @@ fn load_cost_table() -> AtomicCostTable { } fn load_saturation_curves(cost_table: &AtomicCostTable) -> SaturationCurves { - let dir = flag_value("--saturation-dir").expect("--saturation-dir DIR is required"); - let curves = SaturationCurves::load(std::path::Path::new(&dir)) + let curves = SaturationCurves::load(std::path::Path::new(&saturation_dir())) .unwrap_or_else(|e| panic!("couldn't load saturation curves: {e}")); // ponytail: warn rather than panic, since a config with no curve is never // eligible; panic like small_problem once the cost export matches the grid. @@ -458,14 +451,12 @@ fn solve_and_print( .zip(&plan_cost.query_latency_ms) .filter(|((_, planned_raqe), _)| planned_raqe.deployment == deployment_index) { + let (accuracy_metric, _) = accuracy_key(&planned.deployment.config.sketch); let achieved = accuracy(raqe, &planned.deployment) .map_or("unknown".to_string(), |value| format!("{value:.4}")); println!( " {:<22} merged={:<4} latency={latency_ms:.3e} ms {}={achieved} (sla {})", - raqe.id, - planned_raqe.merged_instance_count, - raqe.accuracy_metric, - raqe.accuracy_sla, + raqe.id, planned_raqe.merged_instance_count, accuracy_metric, raqe.accuracy_sla, ); } }