diff --git a/soroban-env-host/benches/common/measure.rs b/soroban-env-host/benches/common/measure.rs index 0248c7a13..3262ad9f3 100644 --- a/soroban-env-host/benches/common/measure.rs +++ b/soroban-env-host/benches/common/measure.rs @@ -451,10 +451,21 @@ pub fn measure_worst_case_costs( }) } +/// Measure the cost variation of a HCM. `sweep_input` specifies whether the input +/// is fixed or randomized. +/// if true - input is randomized, with `large_input` specifying the upperbound +/// of the input size +/// if false - input size is fixed at `large_input` +/// `iteration` specifies number of iterations to run the measurement +/// `include_best_case` specifies whether best case is included. Often the best case +/// is a trivial case that isn't too relevant (and never hit). So if one is more +/// interested in the worst/average analysis, it might be useful to throw it away. pub fn measure_cost_variation( large_input: u64, + iterations: u64, + sweep_input: bool, + include_best_case: bool, ) -> Result { - let count = 100; let mut i = 0; let mut rng = StdRng::from_seed([0xff; 32]); @@ -476,11 +487,19 @@ pub fn measure_cost_variation( let measurements = measure_costs_inner::( |host| { i += 1; + let input = if sweep_input { + rng.gen_range(1..=2 + large_input) + } else { + large_input + }; match i { - 1 => Some(HCM::new_best_case(host, &mut rng)), + 1 => if include_best_case { + Some(HCM::new_best_case(host, &mut rng)) + } else { + Some(HCM::new_random_case(host, &mut rng, input)) + } 2 => Some(HCM::new_worst_case(host, &mut rng, large_input)), - n if n < count => { - let input = rng.gen_range(1..=2 + large_input); + n if n < iterations => { Some(HCM::new_random_case(host, &mut rng, input)) } _ => None, diff --git a/soroban-env-host/benches/variation_histograms.rs b/soroban-env-host/benches/variation_histograms.rs index 7f57fbcaf..221013280 100644 --- a/soroban-env-host/benches/variation_histograms.rs +++ b/soroban-env-host/benches/variation_histograms.rs @@ -7,7 +7,7 @@ use soroban_env_host::cost_runner::CostRunner; struct LinearModelTables; impl Benchmark for LinearModelTables { fn bench() -> std::io::Result<(FPCostModel, FPCostModel)> { - let mut measurements = measure_cost_variation::(100)?; + let mut measurements = measure_cost_variation::(100, 1000, false, false)?; measurements.check_range_against_baseline(&HCM::Runner::COST_TYPE)?; measurements.preprocess(); measurements.report_histogram("cpu", |m| m.cpu_insns);