Skip to content

Commit

Permalink
fix: remove flaky, unbounded test and move it to a benchmark (#6102)
Browse files Browse the repository at this point in the history
Closes #6100 (which was a bit unnecessary and more for testing).

The benchmark runs quantile estimations of .5, .9, .95, and .99 on a normal distribution.
  • Loading branch information
zeeshanlakhani authored Jul 17, 2024
1 parent bc457a9 commit 78d3f30
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions oximeter/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ uuid.workspace = true

[dev-dependencies]
approx.workspace = true
# For benchmark
criterion.workspace = true
rand = { workspace = true, features = ["std_rng"] }
rand_distr.workspace = true
rstest.workspace = true
serde_json.workspace = true
trybuild.workspace = true

[[bench]]
name = "quantile"
harness = false
42 changes: 42 additions & 0 deletions oximeter/impl/benches/quantile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Benchmarks for the implementation of the P² algorithm with
//! quantile estimation.
// Copyright 2024 Oxide Computer Company

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use oximeter_impl::Quantile;
use rand_distr::{Distribution, Normal};

/// Emulates baseline code in a Python implementation of the P²
/// algorithm:
/// <https://github.com/rfrenoy/psquare/blob/master/tests/test_psquare.py#L47>.
fn normal_distribution_quantile(size: i32, p: f64) -> f64 {
let mu = 500.;
let sigma = 100.;
let mut q = Quantile::new(p).unwrap();
let normal = Normal::new(mu, sigma).unwrap();
for _ in 0..size {
q.append(normal.sample(&mut rand::thread_rng())).unwrap();
}
q.estimate().unwrap()
}

fn baseline_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Quantile");
let size = 1000;
for p in [0.5, 0.9, 0.95, 0.99].iter() {
group.bench_with_input(
BenchmarkId::new("Estimate on Normal Distribution", p),
p,
|b, p| b.iter(|| normal_distribution_quantile(size, *p)),
);
}
group.finish();
}

criterion_group!(benches, baseline_benchmark);
criterion_main!(benches);
34 changes: 0 additions & 34 deletions oximeter/impl/src/quantile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ mod tests {
use super::*;
use approx::assert_relative_eq;
use rand::{Rng, SeedableRng};
use rand_distr::{Distribution, Normal};

fn test_quantile_impl(
p: f64,
Expand Down Expand Up @@ -568,37 +567,4 @@ mod tests {
assert_eq!(q.find_cell(4.), Some(3));
assert_eq!(q.find_cell(3.5), Some(2));
}

/// Emulates baseline test in a basic Python implementation of the P²
/// algorithm:
/// <https://github.com/rfrenoy/psquare/blob/master/tests/test_psquare.py#L47>.
#[test]
fn test_against_baseline_normal_distribution() {
let mu = 500.;
let sigma = 100.;
let size = 1000;
let p = 0.9;

let normal = Normal::new(mu, sigma);
let mut observations = (0..size)
.map(|_| normal.unwrap().sample(&mut rand::thread_rng()))
.collect::<Vec<f64>>();
float_ord::sort(&mut observations);
let idx = ((f64::from(size) - 1.) * p) as usize;

let base_p_est = observations[idx];

let mut q = Quantile::new(p).unwrap();
for o in observations.iter() {
q.append(*o).unwrap();
}
let p_est = q.estimate().unwrap();

println!("Base: {}, Est: {}", base_p_est, p_est);
assert!(
(base_p_est - p_est).abs() < 10.0,
"Difference {} is not less than 10",
(base_p_est - p_est).abs()
);
}
}

0 comments on commit 78d3f30

Please sign in to comment.