Skip to content

Commit 31cecde

Browse files
benches: expand argon2 benchmarks varying p_cost
Additionally, use a set instead of trying to avoid repeating a particular set of params by hand.
1 parent 7a72cae commit 31cecde

File tree

1 file changed

+21
-41
lines changed

1 file changed

+21
-41
lines changed

benches/src/argon2.rs

+21-41
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashSet;
2+
13
use argon2::*;
24
use criterion::{black_box, criterion_group, criterion_main, Criterion};
35
use pprof::criterion::{Output, PProfProfiler};
@@ -26,46 +28,26 @@ fn bench_default_params(c: &mut Criterion) {
2628
}
2729
}
2830

29-
fn bench_vary_m(c: &mut Criterion) {
30-
let t_cost = 4;
31-
let p_cost = 4;
32-
for m_cost in [2 * 1024, 16 * 1024, 64 * 1024, 256 * 1024] {
33-
let test_name = format!("argon2id V0x13 m={m_cost} t={t_cost} p={p_cost}");
34-
c.bench_function(&test_name, |b| {
35-
let mut out = [0u8; 32];
36-
let params = Params::new(m_cost, t_cost, p_cost, Some(32)).unwrap();
37-
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
38-
b.iter(|| {
39-
argon2
40-
.hash_password_into(black_box(BENCH_PASSWORD), black_box(BENCH_SALT), &mut out)
41-
.unwrap()
42-
})
43-
});
31+
fn bench_vary_params(c: &mut Criterion) {
32+
let mut tests = HashSet::new();
33+
// Vary `m_cost`.
34+
for m_cost in [2 * 1024, 16 * 1024, 32 * 1024, 64 * 1024, 256 * 1024] {
35+
tests.insert((m_cost, 4, 4));
4436
}
45-
}
46-
47-
fn bench_vary_t(c: &mut Criterion) {
48-
let m_cost = 32 * 1024;
49-
let p_cost = 4;
50-
for t_cost in [2, 8, 16, 24] {
51-
let test_name = format!("argon2id V0x13 m={m_cost} t={t_cost} p={p_cost}");
52-
c.bench_function(&test_name, |b| {
53-
let mut out = [0u8; 32];
54-
let params = Params::new(m_cost, t_cost, p_cost, Some(32)).unwrap();
55-
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
56-
b.iter(|| {
57-
argon2
58-
.hash_password_into(black_box(BENCH_PASSWORD), black_box(BENCH_SALT), &mut out)
59-
.unwrap()
60-
})
61-
});
37+
// Vary `t_cost`.
38+
for t_cost in [1, 2, 4, 8, 16] {
39+
tests.insert((32 * 1024, t_cost, 4));
6240
}
63-
}
64-
65-
fn bench_vary_p(c: &mut Criterion) {
66-
let m_cost = 32 * 1024;
67-
let t_cost = 4;
68-
for p_cost in [2, 8, 16, 64] {
41+
// Vary `p_cost`.
42+
for p_cost in [1, 2, 4, 8, 16] {
43+
for m_mib in [256 * 1024, 1024 * 1024] {
44+
tests.insert((m_mib, 1, p_cost));
45+
}
46+
for t_cost in [1, 2, 4] {
47+
tests.insert((32 * 1024, t_cost, p_cost));
48+
}
49+
}
50+
for (m_cost, t_cost, p_cost) in tests {
6951
let test_name = format!("argon2id V0x13 m={m_cost} t={t_cost} p={p_cost}");
7052
c.bench_function(&test_name, |b| {
7153
let mut out = [0u8; 32];
@@ -85,8 +67,6 @@ criterion_group!(
8567
config = Criterion::default().with_profiler(PProfProfiler::new(300, Output::Flamegraph(None)));
8668
targets =
8769
bench_default_params,
88-
bench_vary_m,
89-
bench_vary_t,
90-
bench_vary_p,
70+
bench_vary_params,
9171
);
9272
criterion_main!(benches);

0 commit comments

Comments
 (0)