Skip to content

Commit

Permalink
Add more options to measure_cost_variation
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 committed Oct 13, 2023
1 parent 57cbefb commit 361098a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 23 additions & 4 deletions soroban-env-host/benches/common/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,21 @@ pub fn measure_worst_case_costs<HCM: HostCostMeasurement>(
})
}

/// 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<HCM: HostCostMeasurement>(
large_input: u64,
iterations: u64,
sweep_input: bool,
include_best_case: bool,
) -> Result<Measurements, std::io::Error> {
let count = 100;
let mut i = 0;
let mut rng = StdRng::from_seed([0xff; 32]);

Expand All @@ -476,11 +487,19 @@ pub fn measure_cost_variation<HCM: HostCostMeasurement>(
let measurements = measure_costs_inner::<HCM, _, _>(
|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,
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-host/benches/variation_histograms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use soroban_env_host::cost_runner::CostRunner;
struct LinearModelTables;
impl Benchmark for LinearModelTables {
fn bench<HCM: HostCostMeasurement>() -> std::io::Result<(FPCostModel, FPCostModel)> {
let mut measurements = measure_cost_variation::<HCM>(100)?;
let mut measurements = measure_cost_variation::<HCM>(100, 1000, false, false)?;
measurements.check_range_against_baseline(&HCM::Runner::COST_TYPE)?;
measurements.preprocess();
measurements.report_histogram("cpu", |m| m.cpu_insns);
Expand Down

0 comments on commit 361098a

Please sign in to comment.