Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add independent regex_redux benchmark test case #1109

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,

Check warning on line 20 in crates/wasmi/benches/rust/regex_redux/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/benches/rust/regex_redux/lib.rs#L16-L20

Added lines #L16 - L20 were not covered by tests
})
}

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

Check warning on line 25 in crates/wasmi/benches/rust/regex_redux/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/benches/rust/regex_redux/lib.rs#L25

Added line #L25 was not covered by tests

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

Check warning on line 29 in crates/wasmi/benches/rust/regex_redux/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/benches/rust/regex_redux/lib.rs#L28-L29

Added lines #L28 - L29 were not covered by tests
}

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

Check warning on line 36 in crates/wasmi/benches/rust/regex_redux/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/benches/rust/regex_redux/lib.rs#L33-L36

Added lines #L33 - L36 were not covered by tests
}
}

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

Check warning on line 43 in crates/wasmi/benches/rust/regex_redux/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/benches/rust/regex_redux/lib.rs#L41-L43

Added lines #L41 - L43 were not covered by tests
}
Loading