summaryrefslogtreecommitdiff
path: root/benches
diff options
context:
space:
mode:
Diffstat (limited to 'benches')
-rw-r--r--benches/argon2id.rs46
-rw-r--r--benches/fibonacci.rs52
-rw-r--r--benches/tinywasm.rs49
-rw-r--r--benches/tinywasm_modes.rs100
4 files changed, 247 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..699dc79
--- /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_100", |b| b.iter(|| fibonacci_run(module.clone(), false, 100)));
+ 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/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);