Skip to content

Commit

Permalink
benches: expand argon2 benchmarks varying p_cost
Browse files Browse the repository at this point in the history
Additionally, use a set instead of trying to avoid repeating a
particular set of params by hand.
  • Loading branch information
jonasmalacofilho committed Mar 8, 2025
1 parent 7a72cae commit 31cecde
Showing 1 changed file with 21 additions and 41 deletions.
62 changes: 21 additions & 41 deletions benches/src/argon2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use argon2::*;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use pprof::criterion::{Output, PProfProfiler};
Expand Down Expand Up @@ -26,46 +28,26 @@ fn bench_default_params(c: &mut Criterion) {
}
}

fn bench_vary_m(c: &mut Criterion) {
let t_cost = 4;
let p_cost = 4;
for m_cost in [2 * 1024, 16 * 1024, 64 * 1024, 256 * 1024] {
let test_name = format!("argon2id V0x13 m={m_cost} t={t_cost} p={p_cost}");
c.bench_function(&test_name, |b| {
let mut out = [0u8; 32];
let params = Params::new(m_cost, t_cost, p_cost, Some(32)).unwrap();
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
b.iter(|| {
argon2
.hash_password_into(black_box(BENCH_PASSWORD), black_box(BENCH_SALT), &mut out)
.unwrap()
})
});
fn bench_vary_params(c: &mut Criterion) {
let mut tests = HashSet::new();
// Vary `m_cost`.
for m_cost in [2 * 1024, 16 * 1024, 32 * 1024, 64 * 1024, 256 * 1024] {
tests.insert((m_cost, 4, 4));
}
}

fn bench_vary_t(c: &mut Criterion) {
let m_cost = 32 * 1024;
let p_cost = 4;
for t_cost in [2, 8, 16, 24] {
let test_name = format!("argon2id V0x13 m={m_cost} t={t_cost} p={p_cost}");
c.bench_function(&test_name, |b| {
let mut out = [0u8; 32];
let params = Params::new(m_cost, t_cost, p_cost, Some(32)).unwrap();
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
b.iter(|| {
argon2
.hash_password_into(black_box(BENCH_PASSWORD), black_box(BENCH_SALT), &mut out)
.unwrap()
})
});
// Vary `t_cost`.
for t_cost in [1, 2, 4, 8, 16] {
tests.insert((32 * 1024, t_cost, 4));
}
}

fn bench_vary_p(c: &mut Criterion) {
let m_cost = 32 * 1024;
let t_cost = 4;
for p_cost in [2, 8, 16, 64] {
// Vary `p_cost`.
for p_cost in [1, 2, 4, 8, 16] {
for m_mib in [256 * 1024, 1024 * 1024] {
tests.insert((m_mib, 1, p_cost));
}
for t_cost in [1, 2, 4] {
tests.insert((32 * 1024, t_cost, p_cost));
}
}
for (m_cost, t_cost, p_cost) in tests {
let test_name = format!("argon2id V0x13 m={m_cost} t={t_cost} p={p_cost}");
c.bench_function(&test_name, |b| {
let mut out = [0u8; 32];
Expand All @@ -85,8 +67,6 @@ criterion_group!(
config = Criterion::default().with_profiler(PProfProfiler::new(300, Output::Flamegraph(None)));
targets =
bench_default_params,
bench_vary_m,
bench_vary_t,
bench_vary_p,
bench_vary_params,
);
criterion_main!(benches);

0 comments on commit 31cecde

Please sign in to comment.