From 4919d8c2f86f69f669a1afba2a28d5c344fe7197 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Mon, 29 Jan 2024 23:12:20 +0100 Subject: perf: improve benchmarks Signed-off-by: Henry Gressmann --- benches/fibonacci.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'benches/fibonacci.rs') diff --git a/benches/fibonacci.rs b/benches/fibonacci.rs index 7c77ebf..ca83869 100644 --- a/benches/fibonacci.rs +++ b/benches/fibonacci.rs @@ -1,41 +1,41 @@ mod util; use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use tinywasm::types::TinyWasmModule; -use util::tinywasm_module; +use util::wasm_to_twasm; -fn run_tinywasm(module: TinyWasmModule, iterations: i32) { - use tinywasm::*; - let module = Module::from(module); - let mut store = Store::default(); - let imports = Imports::default(); - let instance = ModuleInstance::instantiate(&mut store, module, Some(imports)).expect("instantiate"); - let hello = instance.exported_func::(&store, "fibonacci").expect("exported_func"); +fn run_tinywasm(twasm: &[u8], iterations: i32, name: &str) { + let (mut store, instance) = util::tinywasm(twasm); + let hello = instance.exported_func::(&store, name).expect("exported_func"); hello.call(&mut store, iterations).expect("call"); } -fn run_wasmi(iterations: i32) { - use wasmi::*; - let engine = Engine::default(); - let module = wasmi::Module::new(&engine, FIBONACCI).expect("wasmi::Module::new"); - let mut store = Store::new(&engine, ()); - let linker = >::new(&engine); +fn run_wasmi(wasm: &[u8], iterations: i32, name: &str) { + let (module, mut store, linker) = util::wasmi(wasm); let instance = linker.instantiate(&mut store, &module).expect("instantiate").start(&mut store).expect("start"); - let hello = instance.get_typed_func::(&mut store, "fibonacci").expect("get_typed_func"); + let hello = instance.get_typed_func::(&mut store, name).expect("get_typed_func"); hello.call(&mut store, iterations).expect("call"); } const FIBONACCI: &[u8] = include_bytes!("../examples/rust/out/fibonacci.wasm"); fn criterion_benchmark(c: &mut Criterion) { - let module = tinywasm_module(FIBONACCI); + let twasm = wasm_to_twasm(FIBONACCI); - let mut group = c.benchmark_group("fibonacci"); - group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(module.clone(), black_box(60)))); - group.bench_function("wasmi", |b| b.iter(|| run_wasmi(black_box(60)))); + { + let mut group = c.benchmark_group("fibonacci"); + group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm, black_box(60), "fibonacci"))); + group.bench_function("wasmi", |b| b.iter(|| run_wasmi(&FIBONACCI, black_box(60), "fibonacci"))); + } + + { + let mut group = c.benchmark_group("fibonacci-recursive"); + group.measurement_time(std::time::Duration::from_secs(5)); + group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm, black_box(26), "fibonacci_recursive"))); + group.bench_function("wasmi", |b| b.iter(|| run_wasmi(&FIBONACCI, black_box(26), "fibonacci_recursive"))); + } } criterion_group!( name = benches; - config = Criterion::default().sample_size(50).measurement_time(std::time::Duration::from_secs(5)).significance_level(0.1); + config = Criterion::default().significance_level(0.1); targets = criterion_benchmark ); -- cgit v1.3.1