diff options
| author | Henry <mail@henrygressmann.de> | 2026-08-10 16:21:47 +0200 |
|---|---|---|
| committer | Henry <mail@henrygressmann.de> | 2026-08-10 16:21:47 +0200 |
| commit | 70c5bb2e4816f7ee458a6180845af3bd6b1d2d1d (patch) | |
| tree | efaff316f2fb7a84d641faf70c20d49ead3c0cbc /benches/fibonacci.rs | |
| parent | 666c7f59301acc9fcef10ac2da5beb464e57d665 (diff) | |
chore: cleanup tests / devdeps, fix doom example
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'benches/fibonacci.rs')
| -rw-r--r-- | benches/fibonacci.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/benches/fibonacci.rs b/benches/fibonacci.rs new file mode 100644 index 0000000..51fd2d3 --- /dev/null +++ b/benches/fibonacci.rs @@ -0,0 +1,52 @@ +use criterion::{Criterion, criterion_group, criterion_main}; +use tinywasm::{ModuleInstance, Result, Store, types}; +use types::Module; + +const WASM: &[u8] = include_bytes!("../examples/rust/out/fibonacci.wasm"); + +fn fibonacci_parse() -> Result<Module> { + let parser = tinywasm_parser::Parser::default(); + let data = parser.parse_module_bytes(WASM)?; + Ok(data) +} + +fn fibonacci_to_twasm(module: &Module) -> Result<Vec<u8>> { + let twasm = module.serialize_twasm()?; + Ok(twasm) +} + +fn fibonacci_from_twasm(twasm: &[u8]) -> Result<Module> { + Ok(Module::try_from_twasm(twasm)?) +} + +fn fibonacci_run(module: Module, recursive: bool, n: i32) -> Result<()> { + let mut store = Store::default(); + let instance = ModuleInstance::instantiate(&mut store, &module, None)?; + let argon2 = instance.func::<i32, i32>( + &store, + match recursive { + true => "fibonacci_recursive", + false => "fibonacci", + }, + )?; + argon2.call(&mut store, n)?; + Ok(()) +} + +fn criterion_benchmark(c: &mut Criterion) { + let module = fibonacci_parse().expect("fibonacci_parse"); + let twasm = fibonacci_to_twasm(&module).expect("fibonacci_to_twasm"); + let mut group = c.benchmark_group("fibonacci"); + + group.measurement_time(std::time::Duration::from_secs(2)); + group.bench_function("fibonacci_parse", |b| b.iter(fibonacci_parse)); + group.bench_function("fibonacci_to_twasm", |b| b.iter(|| fibonacci_to_twasm(&module))); + group.bench_function("fibonacci_from_twasm", |b| b.iter(|| fibonacci_from_twasm(&twasm))); + + group.measurement_time(std::time::Duration::from_secs(10)); + group.bench_function("fibonacci_iterative_60", |b| b.iter(|| fibonacci_run(module.clone(), false, 60))); + group.bench_function("fibonacci_recursive_26", |b| b.iter(|| fibonacci_run(module.clone(), true, 26))); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); |
