|
| 1 | +use criterion::{BatchSize, Criterion, criterion_group, criterion_main}; |
| 2 | +use eyre::Result; |
| 3 | +use tinywasm::engine::{Config, FuelPolicy}; |
| 4 | +use tinywasm::types::TinyWasmModule; |
| 5 | +use tinywasm::{Engine, ExecProgress, Extern, FuncContext, FuncHandleTyped, Imports, ModuleInstance, Store}; |
| 6 | + |
| 7 | +const WASM: &[u8] = include_bytes!("../../../examples/rust/out/tinywasm.wasm"); |
| 8 | +const FUEL_PER_ROUND: u32 = 512; |
| 9 | +const TIME_BUDGET_PER_ROUND: core::time::Duration = core::time::Duration::from_micros(50); |
| 10 | +const BENCH_MEASUREMENT_TIME: core::time::Duration = core::time::Duration::from_secs(10); |
| 11 | + |
| 12 | +fn tinywasm_parse() -> Result<TinyWasmModule> { |
| 13 | + let parser = tinywasm_parser::Parser::new(); |
| 14 | + Ok(parser.parse_module_bytes(WASM)?) |
| 15 | +} |
| 16 | + |
| 17 | +fn setup_typed_func(module: TinyWasmModule, engine: Option<Engine>) -> Result<(Store, FuncHandleTyped<(), ()>)> { |
| 18 | + let mut store = match engine { |
| 19 | + Some(engine) => Store::new(engine), |
| 20 | + None => Store::default(), |
| 21 | + }; |
| 22 | + |
| 23 | + let mut imports = Imports::default(); |
| 24 | + imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _: i32| Ok(())))?; |
| 25 | + |
| 26 | + let instance = ModuleInstance::instantiate(&mut store, module.into(), Some(imports))?; |
| 27 | + let func = instance.exported_func::<(), ()>(&store, "hello")?; |
| 28 | + Ok((store, func)) |
| 29 | +} |
| 30 | + |
| 31 | +fn run_call(store: &mut Store, func: &FuncHandleTyped<(), ()>) -> Result<()> { |
| 32 | + func.call(store, ())?; |
| 33 | + Ok(()) |
| 34 | +} |
| 35 | + |
| 36 | +fn run_resume_with_fuel(store: &mut Store, func: &FuncHandleTyped<(), ()>) -> Result<()> { |
| 37 | + let mut execution = func.call_resumable(store, ())?; |
| 38 | + loop { |
| 39 | + match execution.resume_with_fuel(FUEL_PER_ROUND)? { |
| 40 | + ExecProgress::Completed(_) => return Ok(()), |
| 41 | + ExecProgress::Suspended => {} |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn run_resume_with_time_budget(store: &mut Store, func: &FuncHandleTyped<(), ()>) -> Result<()> { |
| 47 | + let mut execution = func.call_resumable(store, ())?; |
| 48 | + loop { |
| 49 | + match execution.resume_with_time_budget(TIME_BUDGET_PER_ROUND)? { |
| 50 | + ExecProgress::Completed(_) => return Ok(()), |
| 51 | + ExecProgress::Suspended => {} |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn criterion_benchmark(c: &mut Criterion) { |
| 57 | + let module = tinywasm_parse().expect("tinywasm_parse"); |
| 58 | + let mut group = c.benchmark_group("tinywasm_modes"); |
| 59 | + group.measurement_time(BENCH_MEASUREMENT_TIME); |
| 60 | + |
| 61 | + group.bench_function("call", |b| { |
| 62 | + b.iter_batched_ref( |
| 63 | + || setup_typed_func(module.clone(), None).expect("setup call"), |
| 64 | + |(store, func)| run_call(store, func).expect("run call"), |
| 65 | + BatchSize::LargeInput, |
| 66 | + ) |
| 67 | + }); |
| 68 | + |
| 69 | + let per_instruction_engine = Engine::new(Config::new().fuel_policy(FuelPolicy::PerInstruction)); |
| 70 | + group.bench_function("resume_fuel_per_instruction", |b| { |
| 71 | + b.iter_batched_ref( |
| 72 | + || { |
| 73 | + setup_typed_func(module.clone(), Some(per_instruction_engine.clone())) |
| 74 | + .expect("setup fuel per-instruction") |
| 75 | + }, |
| 76 | + |(store, func)| run_resume_with_fuel(store, func).expect("run fuel per-instruction"), |
| 77 | + BatchSize::LargeInput, |
| 78 | + ) |
| 79 | + }); |
| 80 | + |
| 81 | + let weighted_engine = Engine::new(Config::new().fuel_policy(FuelPolicy::Weighted)); |
| 82 | + group.bench_function("resume_fuel_weighted", |b| { |
| 83 | + b.iter_batched_ref( |
| 84 | + || setup_typed_func(module.clone(), Some(weighted_engine.clone())).expect("setup fuel weighted"), |
| 85 | + |(store, func)| run_resume_with_fuel(store, func).expect("run fuel weighted"), |
| 86 | + BatchSize::LargeInput, |
| 87 | + ) |
| 88 | + }); |
| 89 | + |
| 90 | + group.bench_function("resume_time_budget", |b| { |
| 91 | + b.iter_batched_ref( |
| 92 | + || setup_typed_func(module.clone(), None).expect("setup time budget"), |
| 93 | + |(store, func)| run_resume_with_time_budget(store, func).expect("run time budget"), |
| 94 | + BatchSize::LargeInput, |
| 95 | + ) |
| 96 | + }); |
| 97 | + |
| 98 | + group.finish(); |
| 99 | +} |
| 100 | + |
| 101 | +criterion_group!(benches, criterion_benchmark); |
| 102 | +criterion_main!(benches); |
0 commit comments