Skip to content

Commit

Permalink
Implement benchmarks based on the Criterion crate (wasmi-labs#323)
Browse files Browse the repository at this point in the history
* apply fmt to benches

* add git submodule poiting to wasm_kernel

* clean up benchmark .wat files

* add v1::Module::engine method getter

* remove built-in wasm-kernel

We are now using this as a git submodule instead.

* add new benchmarks, remove old ones

* no longer deny warnings for cargo audit CI job

This is due to the fact that the tools warns for unmaintained crates such as serde_cbor which usually is not a big deal and happens way too often to be a practical guard.
  • Loading branch information
Robbepop authored Jan 11, 2022
1 parent f20a8a1 commit 0793ada
Show file tree
Hide file tree
Showing 23 changed files with 817 additions and 1,478 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: audit
args: --deny warnings
args: ''

clippy:
name: Clippy
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
[submodule "tests/spec/testsuite-v1"]
path = tests/spec/testsuite-v1
url = https://github.com/WebAssembly/testsuite.git
[submodule "benches/wasm_kernel"]
path = benches/wasm/wasm_kernel
url = https://github.com/robbepop/wasm_kernel.git
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ rand = "0.4.2"
wabt = "0.9"
wast = "39.0"
anyhow = "1.0"
criterion = "0.3.5"

[features]
default = ["std"]
Expand Down Expand Up @@ -56,4 +57,13 @@ reduced-stack-buffer = [ "parity-wasm/reduced-stack-buffer" ]

[workspace]
members = ["validation"]
exclude = ["benches"]
exclude = []

[[bench]]
name = "benches"
harness = false

[profile.bench]
debug = true
lto = "fat"
codegen-units = 1
3 changes: 0 additions & 3 deletions benches/.gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions benches/Cargo.toml

This file was deleted.

139 changes: 139 additions & 0 deletions benches/bench/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use super::{v0, v1};
use std::{fs::File, io::Read as _};

/// Returns the Wasm binary at the given `file_name` as `Vec<u8>`.
///
/// # Note
///
/// This includes validation and compilation to `wasmi` bytecode.
///
/// # Panics
///
/// If the benchmark Wasm file could not be opened, read or parsed.
pub fn load_wasm_from_file(file_name: &str) -> Vec<u8> {
let mut file = File::open(file_name)
.unwrap_or_else(|error| panic!("could not open benchmark file {}: {}", file_name, error));
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap_or_else(|error| {
panic!("could not read file at {} to buffer: {}", file_name, error)
});
buffer
}

/// Parses the Wasm binary at the given `file_name` into a `wasmi` module.
///
/// # Note
///
/// This includes validation and compilation to `wasmi` bytecode.
///
/// # Panics
///
/// If the benchmark Wasm file could not be opened, read or parsed.
pub fn load_module_from_file_v0(file_name: &str) -> v0::Module {
let wasm = load_wasm_from_file(file_name);
v0::Module::from_buffer(wasm).unwrap_or_else(|error| {
panic!(
"could not parse Wasm module from file {}: {}",
file_name, error
)
})
}

/// Parses the Wasm binary at the given `file_name` into a `wasmi` module.
///
/// # Note
///
/// This includes validation and compilation to `wasmi` bytecode.
///
/// # Panics
///
/// If the benchmark Wasm file could not be opened, read or parsed.
pub fn load_module_from_file_v1(file_name: &str) -> v1::Module {
let wasm = load_wasm_from_file(file_name);
let engine = v1::Engine::default();
v1::Module::new(&engine, wasm).unwrap_or_else(|error| {
panic!(
"could not parse Wasm module from file {}: {}",
file_name, error
)
})
}

/// Parses the Wasm binary from the given `file_name` into a `wasmi` `v0` module.
///
/// # Note
///
/// This includes validation and compilation to `wasmi` bytecode.
///
/// # Panics
///
/// If the benchmark Wasm file could not be opened, read or parsed.
pub fn load_instance_from_file_v0(file_name: &str) -> v0::ModuleRef {
let module = load_module_from_file_v0(file_name);
v0::ModuleInstance::new(&module, &v0::ImportsBuilder::default())
.expect("failed to instantiate wasm module")
.run_start(&mut v0::NopExternals)
.unwrap()
}

/// Parses the Wasm binary from the given `file_name` into a `wasmi` `v1` module.
///
/// # Note
///
/// This includes validation and compilation to `wasmi` bytecode.
///
/// # Panics
///
/// If the benchmark Wasm file could not be opened, read or parsed.
pub fn load_instance_from_file_v1(file_name: &str) -> (v1::Store<()>, v1::Instance) {
let module = load_module_from_file_v1(file_name);
let mut linker = <v1::Linker<()>>::default();
let mut store = v1::Store::new(module.engine(), ());
let instance = linker
.instantiate(&mut store, &module)
.unwrap()
.start(&mut store)
.unwrap();
(store, instance)
}

/// Parses the Wasm source from the given `.wat` bytes into a `wasmi` `v0` module.
///
/// # Note
///
/// This includes validation and compilation to `wasmi` bytecode.
///
/// # Panics
///
/// If the benchmark Wasm file could not be opened, read or parsed.
pub fn load_instance_from_wat_v0(wat_bytes: &[u8]) -> v0::ModuleRef {
let wasm = wabt::wat2wasm(wat_bytes).unwrap();
let module = v0::Module::from_buffer(&wasm).unwrap();
v0::ModuleInstance::new(&module, &v0::ImportsBuilder::default())
.expect("failed to instantiate wasm module")
.run_start(&mut v0::NopExternals)
.unwrap()
}

/// Parses the Wasm source from the given `.wat` bytes into a `wasmi` `v0` module.
///
/// # Note
///
/// This includes validation and compilation to `wasmi` bytecode.
///
/// # Panics
///
/// If the benchmark Wasm file could not be opened, read or parsed.
pub fn load_instance_from_wat_v1(wat_bytes: &[u8]) -> (v1::Store<()>, v1::Instance) {
let wasm = wabt::wat2wasm(wat_bytes).unwrap();
let engine = v1::Engine::default();
let module = v1::Module::new(&engine, &wasm).unwrap();
let mut linker = <v1::Linker<()>>::default();
let mut store = v1::Store::new(&engine, ());
let instance = linker
.instantiate(&mut store, &module)
.unwrap()
.start(&mut store)
.unwrap();
(store, instance)
}
Loading

0 comments on commit 0793ada

Please sign in to comment.