|
| 1 | +mod util; |
| 2 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 3 | +use util::wasm_to_twasm; |
| 4 | + |
| 5 | +fn run_tinywasm(twasm: &[u8], params: (i32, i32, i32), name: &str) { |
| 6 | + let (mut store, instance) = util::tinywasm(twasm); |
| 7 | + let argon2 = instance.exported_func::<(i32, i32, i32), i32>(&store, name).expect("exported_func"); |
| 8 | + argon2.call(&mut store, params).expect("call"); |
| 9 | +} |
| 10 | + |
| 11 | +fn run_wasmi(wasm: &[u8], params: (i32, i32, i32), name: &str) { |
| 12 | + let (module, mut store, linker) = util::wasmi(wasm); |
| 13 | + let instance = linker.instantiate(&mut store, &module).expect("instantiate").start(&mut store).expect("start"); |
| 14 | + let argon2 = instance.get_typed_func::<(i32, i32, i32), i32>(&mut store, name).expect("get_typed_func"); |
| 15 | + argon2.call(&mut store, params).expect("call"); |
| 16 | +} |
| 17 | + |
| 18 | +fn run_wasmer(wasm: &[u8], params: (i32, i32, i32), name: &str) { |
| 19 | + use wasmer::Value; |
| 20 | + let (mut store, instance) = util::wasmer(wasm); |
| 21 | + let argon2 = instance.exports.get_function(name).expect("get_function"); |
| 22 | + argon2.call(&mut store, &[Value::I32(params.0), Value::I32(params.1), Value::I32(params.2)]).expect("call"); |
| 23 | +} |
| 24 | + |
| 25 | +fn run_native(params: (i32, i32, i32)) { |
| 26 | + fn run_native(m_cost: i32, t_cost: i32, p_cost: i32) { |
| 27 | + let password = b"password"; |
| 28 | + let salt = b"some random salt"; |
| 29 | + |
| 30 | + let params = argon2::Params::new(m_cost as u32, t_cost as u32, p_cost as u32, None).unwrap(); |
| 31 | + let argon = argon2::Argon2::new(argon2::Algorithm::Argon2id, argon2::Version::V0x13, params); |
| 32 | + |
| 33 | + let mut hash = [0u8; 32]; |
| 34 | + argon.hash_password_into(password, salt, &mut hash).unwrap(); |
| 35 | + } |
| 36 | + run_native(params.0, params.1, params.2) |
| 37 | +} |
| 38 | + |
| 39 | +const ARGON2ID: &[u8] = include_bytes!("../examples/rust/out/argon2id.wasm"); |
| 40 | +fn criterion_benchmark(c: &mut Criterion) { |
| 41 | + let twasm = wasm_to_twasm(ARGON2ID); |
| 42 | + let params = (1000, 2, 1); |
| 43 | + |
| 44 | + let mut group = c.benchmark_group("argon2id"); |
| 45 | + group.measurement_time(std::time::Duration::from_secs(7)); |
| 46 | + group.sample_size(10); |
| 47 | + |
| 48 | + group.bench_function("native", |b| b.iter(|| run_native(black_box(params)))); |
| 49 | + group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm, black_box(params), "argon2id"))); |
| 50 | + group.bench_function("wasmi", |b| b.iter(|| run_wasmi(&ARGON2ID, black_box(params), "argon2id"))); |
| 51 | + group.bench_function("wasmer", |b| b.iter(|| run_wasmer(&ARGON2ID, black_box(params), "argon2id"))); |
| 52 | +} |
| 53 | + |
| 54 | +criterion_group!( |
| 55 | + name = benches; |
| 56 | + config = Criterion::default().significance_level(0.1); |
| 57 | + targets = criterion_benchmark |
| 58 | +); |
| 59 | + |
| 60 | +criterion_main!(benches); |
0 commit comments