Expand description
A C API for benchmarking Wasmtime’s WebAssembly compilation, instantiation, and execution.
The API expects calls that match the following state machine:
|
|
V
.---> wasm_bench_create
| | |
| | |
| | V
| | wasm_bench_compile
| | | |
| | | | .----.
| | | | | |
| | | V V |
| | | wasm_bench_instantiate <------.
| | | | | |
| | | | | |
| | | | | |
| | | .------' '-----> wasm_bench_execute
| | | | |
| | | | |
| V V V |
'------ wasm_bench_free <--------------------------'
|
|
V
All API calls must happen on the same thread.
Functions which return pointers use null as an error value. Function which
return int
use 0
as OK and non-zero as an error value.
§Example
use std::ptr;
use wasmtime_bench_api::*;
let working_dir = std::env::current_dir().unwrap().display().to_string();
let stdout_path = "./stdout.log";
let stderr_path = "./stderr.log";
// Functions to start/end timers for compilation.
//
// The `compilation_timer` pointer configured in the `WasmBenchConfig` is
// passed through.
extern "C" fn compilation_start(timer: *mut u8) {
// Start your compilation timer here.
}
extern "C" fn compilation_end(timer: *mut u8) {
// End your compilation timer here.
}
// Similar for instantiation.
extern "C" fn instantiation_start(timer: *mut u8) {
// Start your instantiation timer here.
}
extern "C" fn instantiation_end(timer: *mut u8) {
// End your instantiation timer here.
}
// Similar for execution.
extern "C" fn execution_start(timer: *mut u8) {
// Start your execution timer here.
}
extern "C" fn execution_end(timer: *mut u8) {
// End your execution timer here.
}
let config = WasmBenchConfig {
working_dir_ptr: working_dir.as_ptr(),
working_dir_len: working_dir.len(),
stdout_path_ptr: stdout_path.as_ptr(),
stdout_path_len: stdout_path.len(),
stderr_path_ptr: stderr_path.as_ptr(),
stderr_path_len: stderr_path.len(),
stdin_path_ptr: ptr::null(),
stdin_path_len: 0,
compilation_timer: ptr::null_mut(),
compilation_start,
compilation_end,
instantiation_timer: ptr::null_mut(),
instantiation_start,
instantiation_end,
execution_timer: ptr::null_mut(),
execution_start,
execution_end,
execution_flags_ptr: ptr::null(),
execution_flags_len: 0,
};
let mut bench_api = ptr::null_mut();
unsafe {
let code = wasm_bench_create(config, &mut bench_api);
assert_eq!(code, OK);
assert!(!bench_api.is_null());
};
let wasm = wat::parse_bytes(br#"
(module
(func $bench_start (import "bench" "start"))
(func $bench_end (import "bench" "end"))
(func $start (export "_start")
call $bench_start
i32.const 1
i32.const 2
i32.add
drop
call $bench_end
)
)
"#).unwrap();
// This will call the `compilation_{start,end}` timing functions on success.
let code = unsafe { wasm_bench_compile(bench_api, wasm.as_ptr(), wasm.len()) };
assert_eq!(code, OK);
// This will call the `instantiation_{start,end}` timing functions on success.
let code = unsafe { wasm_bench_instantiate(bench_api) };
assert_eq!(code, OK);
// This will call the `execution_{start,end}` timing functions on success.
let code = unsafe { wasm_bench_execute(bench_api) };
assert_eq!(code, OK);
unsafe {
wasm_bench_free(bench_api);
}
Structs§
- Wasm
Bench Config - Configuration options for the benchmark.
Constants§
Functions§
- wasm_
bench_ compile - Compile the Wasm benchmark module.
- wasm_
bench_ create - Exposes a C-compatible way of creating the engine from the bytes of a single Wasm module.
- wasm_
bench_ execute - Execute the Wasm benchmark module.
- wasm_
bench_ free - Free the engine state allocated by this library.
- wasm_
bench_ instantiate - Instantiate the Wasm benchmark module.