Skip to content

Instantly share code, notes, and snippets.

@acfoltzer
Created March 28, 2019 22:20
Show Gist options
  • Save acfoltzer/199e92236695c01b21f4764b4d456c6b to your computer and use it in GitHub Desktop.
Save acfoltzer/199e92236695c01b21f4764b4d456c6b to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate criterion;
use criterion::Criterion;
use lucet_runtime::{DlModule, InstanceHandle, Limits, MmapRegion, Module, Region};
use lucet_wasi_sdk::Link;
use lucetc::{Bindings, Lucetc};
use std::path::Path;
use std::sync::Arc;
use tempfile::TempDir;
fn load_mkregion_and_instantiate_body(so_file: &Path) -> InstanceHandle {
let module = DlModule::load(so_file).unwrap();
let region = MmapRegion::create(1, &Limits::default()).unwrap();
region.new_instance(module).unwrap()
}
fn load_mkregion_and_instantiate(c: &mut Criterion) {
lucet_runtime::lucet_internal_ensure_linked();
lucet_wasi::hostcalls::ensure_linked();
let workdir = TempDir::new().expect("create working directory");
let wasm_build = Link::new(&["hello.c"])
.cflag("-Wall")
.cflag("-Werror")
.print_output(true);
let wasm_file = workdir.path().join("out.wasm");
wasm_build.link(wasm_file.clone()).unwrap();
let bindings = Bindings::from_file("../lucet-wasi/bindings.json").unwrap();
let native_build = Lucetc::new(wasm_file).unwrap().bindings(bindings).unwrap();
let so_file = workdir.path().join("out.so");
native_build.shared_object_file(so_file.clone()).unwrap();
c.bench_function("load_mkregion_and_instantiate hello", move |b| {
b.iter(|| load_mkregion_and_instantiate_body(&so_file))
});
}
fn instantiate_body(module: Arc<dyn Module>, region: Arc<MmapRegion>) -> InstanceHandle {
region.new_instance(module).unwrap()
}
fn instantiate(c: &mut Criterion) {
lucet_runtime::lucet_internal_ensure_linked();
lucet_wasi::hostcalls::ensure_linked();
let workdir = TempDir::new().expect("create working directory");
let wasm_build = Link::new(&["hello.c"])
.cflag("-Wall")
.cflag("-Werror")
.print_output(true);
let wasm_file = workdir.path().join("out.wasm");
wasm_build.link(wasm_file.clone()).unwrap();
let bindings = Bindings::from_file("../lucet-wasi/bindings.json").unwrap();
let native_build = Lucetc::new(wasm_file).unwrap().bindings(bindings).unwrap();
let so_file = workdir.path().join("out.so");
native_build.shared_object_file(so_file.clone()).unwrap();
let module = DlModule::load(so_file).unwrap();
let region = MmapRegion::create(1, &Limits::default()).unwrap();
c.bench_function("instantiate hello", move |b| {
b.iter(|| instantiate_body(module.clone(), region.clone()))
});
}
criterion_group!(benches, load_mkregion_and_instantiate, instantiate);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment