Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created November 16, 2020 20:30
Show Gist options
  • Save alexcrichton/638502ab212acdbd8d7f9b8a5f9adcda to your computer and use it in GitHub Desktop.
Save alexcrichton/638502ab212acdbd8d7f9b8a5f9adcda to your computer and use it in GitHub Desktop.
use criterion::{criterion_group, criterion_main, Criterion};
use std::rc::Rc;
use wasmtime::*;
pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("with imports", |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(|| {
// 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();
})
});
}
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