|
| 1 | +use std::sync::{Arc, Mutex}; |
| 2 | + |
| 3 | +use criterion::{Bencher, Criterion, criterion_group, criterion_main}; |
| 4 | +use hyperlight_wasm::{LoadedWasmSandbox, SandboxBuilder}; |
| 5 | + |
| 6 | +use crate::bindings::example::runcomponent::Guest; |
| 7 | + |
| 8 | +extern crate alloc; |
| 9 | +mod bindings { |
| 10 | + hyperlight_component_macro::host_bindgen!( |
| 11 | + "../../src/wasmsamples/components/runcomponent-world.wasm" |
| 12 | + ); |
| 13 | +} |
| 14 | + |
| 15 | +pub struct State {} |
| 16 | +impl State { |
| 17 | + pub fn new() -> Self { |
| 18 | + State {} |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl Default for State { |
| 23 | + fn default() -> Self { |
| 24 | + Self::new() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl bindings::example::runcomponent::Host for State { |
| 29 | + fn r#get_time_since_boot_microsecond(&mut self) -> i64 { |
| 30 | + let res = std::time::SystemTime::now() |
| 31 | + .duration_since(std::time::SystemTime::UNIX_EPOCH) |
| 32 | + .unwrap() |
| 33 | + .as_micros(); |
| 34 | + i64::try_from(res).unwrap() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl bindings::example::runcomponent::RuncomponentImports for State { |
| 39 | + type Host = State; |
| 40 | + |
| 41 | + fn r#host(&mut self) -> impl ::core::borrow::BorrowMut<Self::Host> { |
| 42 | + self |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn wasm_component_guest_call_benchmark(c: &mut Criterion) { |
| 47 | + let mut group = c.benchmark_group("wasm_component_guest_functions"); |
| 48 | + |
| 49 | + let bench_guest_function = |b: &mut Bencher<'_>, ext| { |
| 50 | + let (sb, rt) = get_loaded_wasm_sandbox(ext); |
| 51 | + let mut wrapped = bindings::RuncomponentSandbox { sb, rt }; |
| 52 | + let instance = bindings::example::runcomponent::RuncomponentExports::guest(&mut wrapped); |
| 53 | + |
| 54 | + b.iter(|| { |
| 55 | + instance.echo("Hello World!".to_string()); |
| 56 | + }); |
| 57 | + }; |
| 58 | + |
| 59 | + group.bench_function("wasm_guest_call", |b: &mut Bencher<'_>| { |
| 60 | + bench_guest_function(b, "wasm"); |
| 61 | + }); |
| 62 | + |
| 63 | + group.bench_function("wasm_guest_call_aot", |b: &mut Bencher<'_>| { |
| 64 | + bench_guest_function(b, "aot"); |
| 65 | + }); |
| 66 | + |
| 67 | + group.finish(); |
| 68 | +} |
| 69 | + |
| 70 | +fn wasm_component_sandbox_benchmark(c: &mut Criterion) { |
| 71 | + let mut group = c.benchmark_group("wasm_component_sandboxes"); |
| 72 | + let create_wasm_sandbox = || { |
| 73 | + get_loaded_wasm_sandbox("wasm"); |
| 74 | + }; |
| 75 | + |
| 76 | + group.bench_function("create_sandbox", |b| { |
| 77 | + b.iter_with_large_drop(create_wasm_sandbox); |
| 78 | + }); |
| 79 | + |
| 80 | + group.bench_function("create_sandbox_and_drop", |b| { |
| 81 | + b.iter(create_wasm_sandbox); |
| 82 | + }); |
| 83 | + |
| 84 | + group.finish(); |
| 85 | +} |
| 86 | + |
| 87 | +fn get_loaded_wasm_sandbox( |
| 88 | + ext: &str, |
| 89 | +) -> ( |
| 90 | + LoadedWasmSandbox, |
| 91 | + Arc<Mutex<bindings::RuncomponentResources<State>>>, |
| 92 | +) { |
| 93 | + let state = State::new(); |
| 94 | + let mut sandbox = SandboxBuilder::new().build().unwrap(); |
| 95 | + let rt = bindings::register_host_functions(&mut sandbox, state); |
| 96 | + |
| 97 | + let sb = sandbox.load_runtime().unwrap(); |
| 98 | + |
| 99 | + let sb = sb |
| 100 | + .load_module(format!("../../x64/release/runcomponent.{ext}",)) |
| 101 | + .unwrap(); |
| 102 | + (sb, rt) |
| 103 | +} |
| 104 | + |
| 105 | +criterion_group! { |
| 106 | + name = benches_components; |
| 107 | + config = Criterion::default();//.warm_up_time(Duration::from_millis(50)); // If warm_up_time is default 3s warmup, the benchmark will fail due memory error |
| 108 | + targets = wasm_component_guest_call_benchmark, wasm_component_sandbox_benchmark |
| 109 | +} |
| 110 | +criterion_main!(benches_components); |
0 commit comments