forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistribution_statistic.rs
63 lines (57 loc) · 1.84 KB
/
distribution_statistic.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use rand::{
distributions::{Distribution, Uniform},
seq::SliceRandom,
};
use vector::{event::metric::Sample, sinks::util::statistic::DistributionStatistic};
fn generate_samples(mut size: u32, max_bin_count: u32) -> Vec<Sample> {
let mut rng = rand::thread_rng();
let range = Uniform::from(1..=max_bin_count);
let mut value = 1.0;
let mut samples = Vec::new();
while size > 0 {
let bin_count = u32::min(range.sample(&mut rng), size);
samples.push(Sample {
value,
rate: bin_count,
});
size -= bin_count;
value += 1.0;
}
samples.shuffle(&mut rng);
samples
}
fn bench_statistic(c: &mut Criterion) {
let mut group = c.benchmark_group("distribution_statistic");
let sizes = [5, 10, 50, 100, 200, 500, 1000];
for &size in &sizes {
group.bench_function(format!("small-bin-{}", size), |b| {
b.iter_batched(
move || generate_samples(size, 3),
|samples| {
DistributionStatistic::from_samples(&samples, &[0.5, 0.75, 0.9, 0.95, 0.99])
},
BatchSize::SmallInput,
);
});
}
let sizes = [50, 100, 200, 500, 1000];
for &size in &sizes {
group.bench_function(format!("large-bin-{}", size), |b| {
b.iter_batched(
move || generate_samples(size, 20),
|samples| {
DistributionStatistic::from_samples(&samples, &[0.5, 0.75, 0.9, 0.95, 0.99])
},
BatchSize::SmallInput,
);
});
}
group.finish();
}
criterion_group!(
name = benches;
config = Criterion::default().noise_threshold(0.1);
targets = bench_statistic
);
criterion_main!(benches);