diff options
| author | Mica White <botahamec@outlook.com> | 2026-10-31 11:10:49 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-10-31 11:10:49 -0400 |
| commit | 264ea48cc8c6dd9c3c9e0fe1a559252863fc9962 (patch) | |
| tree | 84a4a58de14442f4fcfa06bd0d82480508f82759 /benches/argon2id.rs | |
| parent | 7877eca4e8be283e7c51168ee3a3b05d9000f39c (diff) | |
| parent | b45a98a85c776d956a797773acd2f198c06b40ed (diff) | |
Merge branch 'main' into bota-tortuise
Diffstat (limited to 'benches/argon2id.rs')
| -rw-r--r-- | benches/argon2id.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/benches/argon2id.rs b/benches/argon2id.rs new file mode 100644 index 0000000..3121062 --- /dev/null +++ b/benches/argon2id.rs @@ -0,0 +1,46 @@ +use criterion::{Criterion, criterion_group, criterion_main}; +use tinywasm::{ModuleInstance, Result, Store, types}; +use types::Module; + +const WASM: &[u8] = include_bytes!("../examples/rust/out/argon2id.wasm"); + +fn argon2id_parse() -> Result<Module> { + let parser = tinywasm_parser::Parser::default(); + let data = parser.parse_module_bytes(WASM)?; + Ok(data) +} + +fn argon2id_to_twasm(module: &Module) -> Result<Vec<u8>> { + let twasm = module.serialize_twasm()?; + Ok(twasm) +} + +fn argon2id_from_twasm(twasm: &[u8]) -> Result<Module> { + let module = Module::try_from_twasm(twasm)?; + Ok(module) +} + +fn argon2id_run(module: Module) -> Result<()> { + let mut store = Store::default(); + let instance = ModuleInstance::instantiate(&mut store, &module, None)?; + let argon2 = instance.func::<(i32, i32, i32), i32>(&store, "argon2id")?; + argon2.call(&mut store, (1000, 2, 1))?; + Ok(()) +} + +fn criterion_benchmark(c: &mut Criterion) { + let module = argon2id_parse().expect("argon2id_parse"); + let twasm = argon2id_to_twasm(&module).expect("argon2id_to_twasm"); + let mut group = c.benchmark_group("argon2id"); + + group.measurement_time(std::time::Duration::from_secs(2)); + group.bench_function("argon2id_parse", |b| b.iter(argon2id_parse)); + group.bench_function("argon2id_to_twasm", |b| b.iter(|| argon2id_to_twasm(&module))); + group.bench_function("argon2id_from_twasm", |b| b.iter(|| argon2id_from_twasm(&twasm))); + + group.measurement_time(std::time::Duration::from_secs(10)); + group.bench_function("argon2id", |b| b.iter(|| argon2id_run(module.clone()))); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); |
