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 | |
| parent | 666c7f59301acc9fcef10ac2da5beb464e57d665 (diff) | |
chore: cleanup tests / devdeps, fix doom example
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'benches')
| -rw-r--r-- | benches/argon2id.rs | 46 | ||||
| -rw-r--r-- | benches/fibonacci.rs | 52 | ||||
| -rw-r--r-- | benches/memory_backends.rs | 152 | ||||
| -rw-r--r-- | benches/tinywasm.rs | 49 | ||||
| -rw-r--r-- | benches/tinywasm_modes.rs | 100 |
5 files changed, 399 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); 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); diff --git a/benches/memory_backends.rs b/benches/memory_backends.rs new file mode 100644 index 0000000..3a59f8c --- /dev/null +++ b/benches/memory_backends.rs @@ -0,0 +1,152 @@ +use criterion::measurement::WallTime; +use criterion::{BatchSize, BenchmarkGroup, BenchmarkId, Criterion, criterion_group, criterion_main}; +use std::hint::black_box; +use tinywasm::{LinearMemory, PagedMemory, VecMemory}; + +const PAGE_SIZE: usize = 64 * 1024; +const CHUNK_SIZE: usize = 4 * 1024; +const GROW_STEPS: usize = 32; +const BENCH_MEASUREMENT_TIME: std::time::Duration = std::time::Duration::from_secs(10); + +const MEMORY_LEN: usize = PAGE_SIZE * 4; +const CONTIGUOUS_OFFSET: usize = 1024; +const CONTIGUOUS_LEN: usize = 2048; +const CROSS_CHUNK_OFFSET: usize = CHUNK_SIZE - 512; +const CROSS_CHUNK_LEN: usize = CHUNK_SIZE * 2; + +fn bench_grow<M, F>(group: &mut BenchmarkGroup<'_, WallTime>, backend: &str, make_memory: F) +where + M: LinearMemory, + F: Fn() -> M + Copy, +{ + group.bench_function(BenchmarkId::new("grow", backend), |b| { + b.iter_batched( + make_memory, + |mut memory| { + for page_count in 2..=GROW_STEPS + 1 { + memory.grow_to(page_count * PAGE_SIZE).unwrap(); + } + black_box(memory.len()) + }, + BatchSize::SmallInput, + ) + }); +} + +fn bench_write_all<M: LinearMemory>( + group: &mut BenchmarkGroup<'_, WallTime>, + backend: &str, + workload: &str, + mut memory: M, + offset: usize, + len: usize, +) { + let src = vec![0xA5; len]; + group.bench_function(BenchmarkId::new(format!("write_all/{workload}"), backend), |b| { + b.iter(|| { + memory.write_all(offset, black_box(&src)).unwrap(); + black_box(memory.len()) + }) + }); +} + +fn bench_read_exact<M: LinearMemory>( + group: &mut BenchmarkGroup<'_, WallTime>, + backend: &str, + workload: &str, + mut memory: M, + offset: usize, + len: usize, +) { + let src = vec![0x5A; len]; + memory.write_all(offset, &src).unwrap(); + + let mut dst = vec![0; len]; + group.bench_function(BenchmarkId::new(format!("read_exact/{workload}"), backend), |b| { + b.iter(|| { + memory.read_exact(offset, black_box(&mut dst)).unwrap(); + black_box(&dst); + }) + }); +} + +fn criterion_benchmark(c: &mut Criterion) { + let mut group = c.benchmark_group("memory_backends"); + group.measurement_time(BENCH_MEASUREMENT_TIME); + + bench_grow(&mut group, "vec", || VecMemory::try_new(PAGE_SIZE).expect("bench memory should be constructible")); + bench_grow(&mut group, "paged", || { + PagedMemory::try_new(PAGE_SIZE, CHUNK_SIZE).expect("bench memory should be constructible") + }); + + bench_write_all( + &mut group, + "vec", + "contiguous", + VecMemory::try_new(MEMORY_LEN).expect("bench memory should be constructible"), + CONTIGUOUS_OFFSET, + CONTIGUOUS_LEN, + ); + bench_write_all( + &mut group, + "paged", + "contiguous", + PagedMemory::try_new(MEMORY_LEN, CHUNK_SIZE).expect("bench memory should be constructible"), + CONTIGUOUS_OFFSET, + CONTIGUOUS_LEN, + ); + bench_read_exact( + &mut group, + "vec", + "contiguous", + VecMemory::try_new(MEMORY_LEN).expect("bench memory should be constructible"), + CONTIGUOUS_OFFSET, + CONTIGUOUS_LEN, + ); + bench_read_exact( + &mut group, + "paged", + "contiguous", + PagedMemory::try_new(MEMORY_LEN, CHUNK_SIZE).expect("bench memory should be constructible"), + CONTIGUOUS_OFFSET, + CONTIGUOUS_LEN, + ); + + bench_write_all( + &mut group, + "vec", + "cross_chunk", + VecMemory::try_new(MEMORY_LEN).expect("bench memory should be constructible"), + CROSS_CHUNK_OFFSET, + CROSS_CHUNK_LEN, + ); + bench_write_all( + &mut group, + "paged", + "cross_chunk", + PagedMemory::try_new(MEMORY_LEN, CHUNK_SIZE).expect("bench memory should be constructible"), + CROSS_CHUNK_OFFSET, + CROSS_CHUNK_LEN, + ); + bench_read_exact( + &mut group, + "vec", + "cross_chunk", + VecMemory::try_new(MEMORY_LEN).expect("bench memory should be constructible"), + CROSS_CHUNK_OFFSET, + CROSS_CHUNK_LEN, + ); + bench_read_exact( + &mut group, + "paged", + "cross_chunk", + PagedMemory::try_new(MEMORY_LEN, CHUNK_SIZE).expect("bench memory should be constructible"), + CROSS_CHUNK_OFFSET, + CROSS_CHUNK_LEN, + ); + + group.finish(); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/benches/tinywasm.rs b/benches/tinywasm.rs new file mode 100644 index 0000000..aff8c70 --- /dev/null +++ b/benches/tinywasm.rs @@ -0,0 +1,49 @@ +use criterion::{Criterion, criterion_group, criterion_main}; +use tinywasm::engine::Config; +use tinywasm::{Engine, FuncContext, HostFunction, Imports, ModuleInstance, Result, Store, types}; +use types::Module; + +const WASM: &[u8] = include_bytes!("../examples/rust/out/tinywasm.wasm"); + +fn tinywasm_parse() -> Result<Module> { + let parser = tinywasm_parser::Parser::default(); + let data = parser.parse_module_bytes(WASM)?; + Ok(data) +} + +fn tinywasm_to_twasm(module: &Module) -> Result<Vec<u8>> { + let twasm = module.serialize_twasm()?; + Ok(twasm) +} + +fn tinywasm_from_twasm(twasm: &[u8]) -> Result<Module> { + let module = Module::try_from_twasm(twasm)?; + Ok(module) +} + +fn tinywasm_run(module: &Module) -> Result<()> { + let engine = Engine::new(Config::default()); + let mut store = Store::new(engine); + let mut imports = Imports::default(); + imports.define("env", "printi32", HostFunction::from(|_: FuncContext<'_>, _: i32| Ok(()))); + let instance = ModuleInstance::instantiate(&mut store, module, Some(&imports)).expect("instantiate"); + let hello = instance.func::<(), ()>(&store, "hello").expect("func_typed"); + hello.call(&mut store, ()).expect("call"); + Ok(()) +} + +fn criterion_benchmark(c: &mut Criterion) { + let module = tinywasm_parse().expect("tinywasm_parse"); + let twasm = tinywasm_to_twasm(&module).expect("tinywasm_to_twasm"); + let mut group = c.benchmark_group("tinywasm"); + + group.measurement_time(std::time::Duration::from_secs(2)); + group.bench_function("tinywasm_parse", |b| b.iter(tinywasm_parse)); + group.bench_function("tinywasm_to_twasm", |b| b.iter(|| tinywasm_to_twasm(&module))); + group.bench_function("tinywasm_from_twasm", |b| b.iter(|| tinywasm_from_twasm(&twasm))); + group.measurement_time(std::time::Duration::from_secs(10)); + group.bench_function("tinywasm", |b| b.iter(|| tinywasm_run(&module))); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); 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); |
