Skip to content

Commit

Permalink
Add independent regex_redux benchmark test case (#1109)
Browse files Browse the repository at this point in the history
* add independent regex_redux benchmark test case

* exclude regex_redux benchmark crate in CI where needed
  • Loading branch information
Robbepop authored Jul 5, 2024
1 parent 8acb2e8 commit 2497643
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
--exclude wasmi_fuzz
--exclude wasmi_benches_tiny_keccak
--exclude wasmi_benches_reverse_complement
--exclude wasmi_benches_regex_redux
- name: Build (wasm32)
run: >-
cargo build
Expand All @@ -56,6 +57,7 @@ jobs:
--exclude wasmi_fuzz
--exclude wasmi_benches_tiny_keccak
--exclude wasmi_benches_reverse_complement
--exclude wasmi_benches_regex_redux
test-asan:
name: Test (Address Sanitizer)
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"crates/wasi",
"crates/wasmi/benches/rust/tiny_keccak",
"crates/wasmi/benches/rust/reverse_complement",
"crates/wasmi/benches/rust/regex_redux",
"fuzz",
]
exclude = []
Expand Down
33 changes: 23 additions & 10 deletions crates/wasmi/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,20 +768,20 @@ fn bench_execute_rev_comp(c: &mut Criterion) {

fn bench_execute_regex_redux(c: &mut Criterion) {
c.bench_function("execute/regex_redux", |b| {
let (mut store, instance) = load_instance_from_file(WASM_KERNEL);
let (mut store, instance) = load_instance_from_file("benches/rust/regex_redux.wasm");

// Allocate buffers for the input and output.
let test_data_ptr = instance
.get_typed_func::<i32, i32>(&store, "prepare_regex_redux")
let data_ptr = instance
.get_typed_func::<i32, i32>(&store, "setup")
.unwrap()
.call(&mut store, REVCOMP_INPUT.len() as i32)
.unwrap();

// Get the pointer to the input buffer.
let input_data_mem_offset = instance
.get_typed_func::<i32, i32>(&store, "regex_redux_input_ptr")
.get_typed_func::<i32, i32>(&store, "input_ptr")
.unwrap()
.call(&mut store, test_data_ptr)
.call(&mut store, data_ptr)
.unwrap();

// Copy test data inside the wasm memory.
Expand All @@ -792,12 +792,25 @@ fn bench_execute_regex_redux(c: &mut Criterion) {
.unwrap();

// Actually run the benchmark:
let run = instance
.get_typed_func::<i32, ()>(&store, "bench_regex_redux")
.unwrap();
let run = instance.get_typed_func::<i32, ()>(&store, "run").unwrap();
b.iter(|| {
run.call(&mut store, test_data_ptr).unwrap();
})
run.call(&mut store, data_ptr).unwrap();
});

// Check the result of the regex find.
let result = instance
.get_typed_func::<i32, i32>(&store, "output")
.unwrap()
.call(&mut store, data_ptr)
.unwrap();
assert_eq!(result, 2);

// Teardown benchmark data.
instance
.get_typed_func::<i32, ()>(&store, "teardown")
.unwrap()
.call(&mut store, data_ptr)
.unwrap();
});
}

Expand Down
Binary file added crates/wasmi/benches/rust/regex_redux.wasm
Binary file not shown.
13 changes: 13 additions & 0 deletions crates/wasmi/benches/rust/regex_redux/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "wasmi_benches_regex_redux"
version = "0.0.0"
publish = false
authors.workspace = true
edition.workspace = true

[lib]
path = "lib.rs"
crate-type = ["cdylib"]

[dependencies]
regex = "1.10.5"
35 changes: 35 additions & 0 deletions crates/wasmi/benches/rust/regex_redux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Benchmark - Regex Redux

Rust based `regex_redux` benchmark test case.

Build with

```
cargo build \
--package wasmi_benches_regex_redux \
--profile wasm \
--target wasm32-unknown-unknown
```

Post-optimize with `wasm-opt` using:

```
wasm-opt \
-O3 \
target/wasm32-unknown-unknown/wasm/wasmi_benches_regex_redux.wasm \
-o target/wasm32-unknown-unknown/wasm/wasmi_benches_regex_redux.opt.wasm
```

Finally move the post-optimized Wasm binary into the proper place:

```
cp \
target/wasm32-unknown-unknown/wasm/wasmi_benches_regex_redux.opt.wasm \
crates/wasmi/benches/rust/regex_redux.wasm
```

Benchmark with:

```
cargo bench --bench benches execute/regex_redux
```
44 changes: 44 additions & 0 deletions crates/wasmi/benches/rust/regex_redux/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//! Initially it was supposed to be like [1]. However it turned out
//! that executing this code in Wasmi way too slow, unfortunately.
//!
//! [1]: https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/regexredux-rust-2.html

use regex::bytes::Regex;

#[repr(C)]
pub struct RegexReduxData {
regex: Regex,
input: Box<[u8]>,
output: Option<usize>,
}

#[no_mangle]
pub extern "C" fn setup(size: usize) -> Box<RegexReduxData> {
Box::new(RegexReduxData {
regex: Regex::new("agggtaa[cgt]|[acg]ttaccct").unwrap(),
input: vec![0; size].into(),
output: None,
})
}

#[no_mangle]
pub extern "C" fn teardown(_: Box<RegexReduxData>) {}

#[no_mangle]
pub extern "C" fn input_ptr(data: &mut RegexReduxData) -> *mut u8 {
data.input.as_mut_ptr()
}

#[no_mangle]
pub extern "C" fn output(data: &mut RegexReduxData) -> u32 {
match data.output {
Some(output) => output.try_into().unwrap(),
None => u32::MAX,
}
}

#[no_mangle]
pub extern "C" fn run(data: &mut RegexReduxData) {
let result: usize = data.regex.find_iter(&data.input[..]).count();
data.output = Some(result);
}

0 comments on commit 2497643

Please sign in to comment.