Skip to content

Commit 1c114aa

Browse files
committed
wire up the benchmarking
Signed-off-by: James Sturtevant <[email protected]>
1 parent 255369a commit 1c114aa

File tree

7 files changed

+81
-32
lines changed

7 files changed

+81
-32
lines changed

Justfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ build-wasm-runtime target=default-target:
2929
cd ./src/wasm_runtime && cargo build --verbose --profile={{ if target == "debug" {"dev"} else { target } }} && rm -R target
3030

3131
build-wasm-examples target=default-target:
32+
wasm-tools component wit ./src/wasmsamples/components/runcomponent.wit -w -o ./src/wasmsamples/components/runcomponent-world.wasm
3233
{{ build-wasm-examples-command }} {{target}}
3334

3435
build-rust-wasm-examples target=default-target: (mkdir-redist target)
@@ -100,10 +101,11 @@ examples-components target=default-target features="": (build-rust-component-exa
100101
{{ wit-world }} cargo run {{ if features =="" {''} else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example component_example
101102

102103
# warning, compares to and then OVERWRITES the given baseline
103-
bench-ci baseline target=default-target features="":
104+
bench-ci baseline target="release" features="":
104105
cd src/hyperlight_wasm && cargo bench --profile={{ if target == "debug" {"dev"} else { target } }} {{ if features =="" {''} else { "--features " + features } }} -- --verbose --save-baseline {{baseline}}
105-
bench target=default-target features="":
106-
cd src/hyperlight_wasm && cargo bench --profile={{ if target == "debug" {"dev"} else { target } }} {{ if features =="" {''} else { "--features " + features } }} -- --verbose
106+
bench target="release" features="":
107+
#cd src/hyperlight_wasm && cargo bench --profile={{ if target == "debug" {"dev"} else { target } }} {{ if features =="" {''} else { "--features " + features } }} --bench benchmarks -- --verbose
108+
cd src/hyperlight_wasm && {{wit-world}} cargo bench --profile={{ if target == "debug" {"dev"} else { target } }} {{ if features =="" {''} else { "--features " + features } }} --bench benchmarks_components -- --verbose
107109
bench-download os hypervisor tag="":
108110
gh release download {{ tag }} -D ./src/hyperlight_wasm/target/ -p benchmarks_{{ os }}_{{ hypervisor }}.tar.gz
109111
mkdir {{ mkdir-arg }} ./src/hyperlight_wasm/target/criterion

src/hyperlight_wasm/benches/benchmarks_components.rs

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,67 @@
1+
use std::sync::{Arc, Mutex};
2+
13
use criterion::{Bencher, Criterion, criterion_group, criterion_main};
24
use hyperlight_host::HyperlightError;
35
use hyperlight_wasm::{LoadedWasmSandbox, Result, SandboxBuilder};
6+
use crate::bindings::example::runcomponent::Guest;
7+
8+
extern crate alloc;
9+
mod bindings {
10+
hyperlight_component_macro::host_bindgen!("../../src/wasmsamples/components/runcomponent-world.wasm");
11+
}
12+
13+
pub struct State {}
14+
impl State {
15+
pub fn new() -> Self {
16+
State {}
17+
}
18+
}
19+
20+
impl Default for State {
21+
fn default() -> Self {
22+
Self::new()
23+
}
24+
}
25+
26+
impl bindings::example::runcomponent::Host for State {
27+
fn r#get_time_since_boot_microsecond(&mut self,) -> i64 {
28+
let res = std::time::SystemTime::now()
29+
.duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap()
30+
.as_micros();
31+
i64::try_from(res).unwrap()
32+
}
33+
}
34+
35+
impl bindings::example::runcomponent::RuncomponentImports for State {
36+
type Host = State;
37+
38+
fn r#host(&mut self) -> impl ::core::borrow::BorrowMut<Self::Host> {
39+
self
40+
}
41+
}
42+
443

544
fn wasm_component_guest_call_benchmark(c: &mut Criterion) {
645
let mut group = c.benchmark_group("wasm_component_guest_functions");
746

8-
// let bench_guest_function = |b: &mut Bencher<'_>, ext| {
9-
// let mut loaded_wasm_sandbox = get_loaded_wasm_sandbox(ext);
10-
11-
// b.iter(|| {
12-
// loaded_wasm_sandbox
13-
// .call_guest_function::<String>("Echo", "Hello World!".to_string())
14-
// .unwrap()
15-
// });
16-
// };
47+
let bench_guest_function = |b: &mut Bencher<'_>, ext| {
48+
let (sb, rt) = get_loaded_wasm_sandbox(ext);
49+
let mut wrapped = bindings::RuncomponentSandbox { sb, rt };
50+
let instance = bindings::example::runcomponent::RuncomponentExports::guest(&mut wrapped);
51+
52+
b.iter(|| {
53+
instance
54+
.echo("Hello World!".to_string());
55+
});
56+
};
1757

18-
// group.bench_function("wasm_guest_call", |b: &mut Bencher<'_>| {
19-
// bench_guest_function(b, "wasm");
20-
// });
58+
group.bench_function("wasm_guest_call", |b: &mut Bencher<'_>| {
59+
bench_guest_function(b, "wasm");
60+
});
2161

22-
// group.bench_function("wasm_guest_call_aot", |b: &mut Bencher<'_>| {
23-
// bench_guest_function(b, "aot");
24-
// });
62+
group.bench_function("wasm_guest_call_aot", |b: &mut Bencher<'_>| {
63+
bench_guest_function(b, "aot");
64+
});
2565

2666
group.finish();
2767
}
@@ -43,14 +83,17 @@ fn wasm_component_sandbox_benchmark(c: &mut Criterion) {
4383
group.finish();
4484
}
4585

46-
fn get_loaded_wasm_sandbox(ext: &str) -> LoadedWasmSandbox {
86+
fn get_loaded_wasm_sandbox(ext: &str) -> (LoadedWasmSandbox, Arc<Mutex<bindings::RuncomponentResources<State>>>) {
87+
let state = State::new();
4788
let mut sandbox = SandboxBuilder::new().build().unwrap();
89+
let rt = bindings::register_host_functions(&mut sandbox, state);
90+
4891

49-
let wasm_sandbox = sandbox.load_runtime().unwrap();
92+
let sb = sandbox.load_runtime().unwrap();
5093

51-
wasm_sandbox
52-
.load_module(format!("../../x64/release/component_sample.aot",))
53-
.unwrap()
94+
let sb = sb.load_module(format!("../../x64/release/runcomponent.aot",))
95+
.unwrap();
96+
(sb, rt)
5497
}
5598

5699
criterion_group! {

src/hyperlight_wasm/scripts/build-wasm-examples.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ else
4747

4848
echo Building component
4949
# Build the wasm file with wasi-libc for wasmtime
50+
wit-bindgen c ${PWD}/components/runcomponent.wit --out-dir ${PWD}/components
5051
docker run --rm -i -v "${PWD}:/tmp/host" -v "${OUTPUT_DIR}:/tmp/output/" wasm-clang-builder:latest /opt/wasi-sdk/bin/clang -flto -ffunction-sections -mexec-model=reactor -O3 -z stack-size=4096 -Wl,--initial-memory=65536 -Wl,--export=__data_end -Wl,--export=__heap_base,--export=malloc,--export=free -o /tmp/output/runcomponent-p1.wasm /tmp/host/components/component.c /tmp/host/components/runcomponent.c /tmp/host/components/runcomponent_component_type.o
5152

53+
# tooling currently builds a p1 wasm component so convert it
54+
wasm-tools component new ${OUTPUT_DIR}/runcomponent-p1.wasm -o ${OUTPUT_DIR}/runcomponent-p2.wasm
55+
5256
# Build AOT for Wasmtime; note that Wasmtime does not support
5357
# interpreting, so its wasm binary is secretly an AOT binary.
54-
cargo run -p hyperlight-wasm-aot compile ${OUTPUT_DIR}/runcomponent-p1.wasm ${OUTPUT_DIR}/runcomponent.aot
58+
cargo run -p hyperlight-wasm-aot compile --component ${OUTPUT_DIR}/runcomponent-p2.wasm ${OUTPUT_DIR}/runcomponent.aot
5559
cp ${OUTPUT_DIR}/runcomponent.aot ${OUTPUT_DIR}/runcomponent.wasm
5660
fi
5761

src/wasmsamples/components/runcomponent.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#include <stdlib.h>
44
#include <string.h>
55

6-
// Imported Functions from `example:runcomponent/host@0.1.0`
6+
// Imported Functions from `example:runcomponent/host`
77

8-
__attribute__((__import_module__("example:runcomponent/host@0.1.0"), __import_name__("get-time-since-boot-microsecond")))
8+
__attribute__((__import_module__("example:runcomponent/host"), __import_name__("get-time-since-boot-microsecond")))
99
extern int64_t __wasm_import_example_runcomponent_host_get_time_since_boot_microsecond(void);
1010

11-
// Exported Functions from `example:runcomponent/guest@0.1.0`
11+
// Exported Functions from `example:runcomponent/guest`
1212

13-
__attribute__((__weak__, __export_name__("cabi_post_example:runcomponent/guest@0.1.0#echo")))
13+
__attribute__((__weak__, __export_name__("cabi_post_example:runcomponent/guest#echo")))
1414
void __wasm_export_exports_example_runcomponent_guest_echo_post_return(uint8_t * arg0) {
1515
if ((*((size_t*) (arg0 + 4))) > 0) {
1616
free(*((uint8_t **) (arg0 + 0)));
@@ -59,7 +59,7 @@ int64_t example_runcomponent_host_get_time_since_boot_microsecond(void) {
5959
return ret;
6060
}
6161

62-
__attribute__((__export_name__("example:runcomponent/guest@0.1.0#echo")))
62+
__attribute__((__export_name__("example:runcomponent/guest#echo")))
6363
uint8_t * __wasm_export_exports_example_runcomponent_guest_echo(uint8_t * arg, size_t arg0) {
6464
runcomponent_string_t arg1 = (runcomponent_string_t) { (uint8_t*)(arg), (arg0) };
6565
runcomponent_string_t ret;

src/wasmsamples/components/runcomponent.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ typedef struct runcomponent_string_t {
1414
size_t len;
1515
} runcomponent_string_t;
1616

17-
// Imported Functions from `example:runcomponent/host@0.1.0`
17+
// Imported Functions from `example:runcomponent/host`
1818
extern int64_t example_runcomponent_host_get_time_since_boot_microsecond(void);
1919

20-
// Exported Functions from `example:runcomponent/guest@0.1.0`
20+
// Exported Functions from `example:runcomponent/guest`
2121
void exports_example_runcomponent_guest_echo(runcomponent_string_t *msg, runcomponent_string_t *ret);
2222

2323
// Helper Functions

src/wasmsamples/components/runcomponent.wit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package example:runcomponent@0.1.0;
1+
package example:runcomponent;
22

33
interface guest {
44
echo: func(msg: string) -> string;
Binary file not shown.

0 commit comments

Comments
 (0)