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/tinywasm_modes.rs | |
| parent | 666c7f59301acc9fcef10ac2da5beb464e57d665 (diff) | |
chore: cleanup tests / devdeps, fix doom example
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'benches/tinywasm_modes.rs')
| -rw-r--r-- | benches/tinywasm_modes.rs | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/benches/tinywasm_modes.rs b/benches/tinywasm_modes.rs new file mode 100644 index 0000000..7d1d993 --- /dev/null +++ b/benches/tinywasm_modes.rs @@ -0,0 +1,100 @@ +use criterion::{BatchSize, Criterion, criterion_group, criterion_main}; +use tinywasm::engine::{Config, FuelPolicy}; +use tinywasm::types::Module; +use tinywasm::{ + Engine, ExecProgress, FuncContext, FunctionTyped, HostFunction, Imports, ModuleInstance, Result, Store, +}; + +const WASM: &[u8] = include_bytes!("../examples/rust/out/tinywasm.wasm"); +const FUEL_PER_ROUND: u32 = 512; +const TIME_BUDGET_PER_ROUND: core::time::Duration = core::time::Duration::from_micros(50); +const BENCH_MEASUREMENT_TIME: core::time::Duration = core::time::Duration::from_secs(10); + +fn tinywasm_parse() -> Result<Module> { + let parser = tinywasm_parser::Parser::default(); + Ok(parser.parse_module_bytes(WASM)?) +} + +fn setup_typed_func(module: &Module, engine: Option<Engine>) -> Result<(Store, FunctionTyped<(), ()>)> { + let mut store = match engine { + Some(engine) => Store::new(engine), + None => Store::default(), + }; + + let mut imports = Imports::default(); + imports.define("env", "printi32", HostFunction::from(|_: FuncContext<'_>, _: i32| Ok(()))); + + let instance = ModuleInstance::instantiate(&mut store, module, Some(&imports))?; + let func = instance.func::<(), ()>(&store, "hello")?; + Ok((store, func)) +} + +fn run_call(store: &mut Store, func: &FunctionTyped<(), ()>) -> Result<()> { + func.call(store, ())?; + Ok(()) +} + +fn run_resume_with_fuel(store: &mut Store, func: &FunctionTyped<(), ()>) -> Result<()> { + let mut execution = func.call_resumable(store, ())?; + loop { + match execution.resume_with_fuel(FUEL_PER_ROUND)? { + ExecProgress::Completed(_) => return Ok(()), + ExecProgress::Suspended => {} + } + } +} + +fn run_resume_with_time_budget(store: &mut Store, func: &FunctionTyped<(), ()>) -> Result<()> { + let mut execution = func.call_resumable(store, ())?; + loop { + match execution.resume_with_time_budget(TIME_BUDGET_PER_ROUND)? { + ExecProgress::Completed(_) => return Ok(()), + ExecProgress::Suspended => {} + } + } +} + +fn criterion_benchmark(c: &mut Criterion) { + let module = tinywasm_parse().expect("tinywasm_parse"); + let mut group = c.benchmark_group("tinywasm_modes"); + group.measurement_time(BENCH_MEASUREMENT_TIME); + + let per_instruction_engine = Engine::new(Config::new().with_fuel_policy(FuelPolicy::PerInstruction)); + group.bench_function("resume_fuel_per_instruction", |b| { + b.iter_batched_ref( + || setup_typed_func(&module, Some(per_instruction_engine.clone())).expect("setup fuel per-instruction"), + |(store, func)| run_resume_with_fuel(store, func).expect("run fuel per-instruction"), + BatchSize::LargeInput, + ) + }); + + let weighted_engine = Engine::new(Config::new().with_fuel_policy(FuelPolicy::Weighted)); + group.bench_function("resume_fuel_weighted", |b| { + b.iter_batched_ref( + || setup_typed_func(&module, Some(weighted_engine.clone())).expect("setup fuel weighted"), + |(store, func)| run_resume_with_fuel(store, func).expect("run fuel weighted"), + BatchSize::LargeInput, + ) + }); + + group.bench_function("resume_time_budget", |b| { + b.iter_batched_ref( + || setup_typed_func(&module, None).expect("setup time budget"), + |(store, func)| run_resume_with_time_budget(store, func).expect("run time budget"), + BatchSize::LargeInput, + ) + }); + + group.bench_function("call", |b| { + b.iter_batched_ref( + || setup_typed_func(&module, None).expect("setup call"), + |(store, func)| run_call(store, func).expect("run call"), + BatchSize::LargeInput, + ) + }); + + group.finish(); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); |
