Skip to content

Instantly share code, notes, and snippets.

@bkolobara
Last active November 17, 2020 16:04
Show Gist options
  • Save bkolobara/5f0c946115900fb3eec0ba6cf28eff45 to your computer and use it in GitHub Desktop.
Save bkolobara/5f0c946115900fb3eec0ba6cf28eff45 to your computer and use it in GitHub Desktop.
use criterion::{criterion_group, criterion_main, Criterion};
use std::rc::Rc;
use wasmtime::*;
use rayon::prelude::*;
pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("with imports parallel", |b| {
// Use an engine with default settings, and create a module with one
// noop function.
let engine = Engine::default();
let module = Module::new(
&engine,
r#"
(module
(memory 1)
(func (export "foo")))
"#,
)
.unwrap();
b.iter_custom(|iters| {
let start = std::time::Instant::now();
(0..iters).into_par_iter().for_each(|_i| {
// Simulation application-specific state
let state = Rc::new(3);
// Set up some wasmtime types
let store = Store::new(&engine);
let mut linker = Linker::new(&store);
// Simulate defining a bunch of host functions
let state2 = state.clone();
linker
.func("env", "func1", move |_: i32| drop(&state2))
.unwrap();
let state2 = state.clone();
linker.func("env", "func2", move || drop(&state2)).unwrap();
let state2 = state.clone();
linker
.func("env", "func3", move |_: i64| drop(&state2))
.unwrap();
let state2 = state.clone();
linker
.func("env", "func4", move || -> i32 {
drop(&state2);
3
})
.unwrap();
let state2 = state.clone();
linker
.func("env", "func5", move || -> i64 {
drop(&state2);
9
})
.unwrap();
let state2 = state.clone();
linker
.func("env", "func6", move |_: Caller<'_>| drop(&state2))
.unwrap();
let state2 = state.clone();
linker.func("env", "func7", move || drop(&state2)).unwrap();
let state2 = state.clone();
linker
.func("env", "func8", move |_: i32, _: i64| drop(&state2))
.unwrap();
let state2 = state.clone();
linker
.func("env", "func9", move |_: i64, _: i32| drop(&state2))
.unwrap();
let state2 = state.clone();
linker
.func("env", "func10", move |_: i32, _: i32| drop(&state2))
.unwrap();
let state2 = state.clone();
linker
.func("env", "func11", move |_: i64, _: i64| drop(&state2))
.unwrap();
// Instantiate our module and call its noop function which does as
// little as possible in wasm.
let instance = linker.instantiate(&module).unwrap();
instance.get_func("foo").unwrap().call(&[]).unwrap();
});
start.elapsed()
})
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment