-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathperformance.rs
More file actions
65 lines (60 loc) · 2.2 KB
/
performance.rs
File metadata and controls
65 lines (60 loc) · 2.2 KB
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
64
65
use std::{
hash::{DefaultHasher, Hash, Hasher},
hint::black_box,
time::Duration,
};
use consistent_hashing::{ConsistentChooseKHasher, ConsistentHasher};
use criterion::{
criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
Throughput,
};
use rand::{rng, Rng};
fn throughput_benchmark(c: &mut Criterion) {
let keys: Vec<u64> = rng().random_iter().take(1000).collect();
let mut group = c.benchmark_group(format!("choose"));
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
for n in [1usize, 10, 100, 1000, 10000] {
group.throughput(Throughput::Elements(keys.len() as u64));
group.bench_with_input(BenchmarkId::new(format!("1"), n), &n, |b, n| {
b.iter_batched(
|| &keys,
|keys| {
for key in keys {
let mut h = DefaultHasher::default();
key.hash(&mut h);
black_box(ConsistentHasher::new(h).prev(*n + 1));
}
},
criterion::BatchSize::SmallInput,
)
});
for k in [1, 2, 3, 10, 100] {
group.bench_with_input(BenchmarkId::new(format!("k_{k}"), n), &n, |b, n| {
b.iter_batched(
|| &keys,
|keys| {
let mut res = Vec::with_capacity(k);
for key in keys {
let mut h = DefaultHasher::default();
key.hash(&mut h);
black_box(
ConsistentChooseKHasher::new(h, k).prev_with_vec(*n + k, &mut res),
);
}
},
criterion::BatchSize::SmallInput,
)
});
}
}
group.finish();
}
criterion_group!(
name = benches;
config = Criterion::default()
.warm_up_time(Duration::from_millis(500))
.measurement_time(Duration::from_millis(4000))
.nresamples(1000);
targets = throughput_benchmark,
);
criterion_main!(benches);