1+ use std:: ffi:: CString ;
2+ use std:: io;
3+ use std:: mem:: MaybeUninit ;
14use std:: sync:: { Arc , Mutex } ;
25
3- use criterion:: { Bencher , Criterion , criterion_group , criterion_main } ;
4- use hyperlight_wasm:: { LoadedWasmSandbox , SandboxBuilder } ;
6+ use criterion:: { criterion_group , criterion_main , Bencher , Criterion } ;
7+ use hyperlight_wasm:: { LoadedWasmSandbox , SandboxBuilder , WasmSandbox } ;
58
69use crate :: bindings:: example:: runcomponent:: Guest ;
710
@@ -80,6 +83,75 @@ fn wasm_component_sandbox_benchmark(c: &mut Criterion) {
8083 group. finish ( ) ;
8184}
8285
86+ #[ cfg( not( windows) ) ]
87+ fn wasm_component_load_call_unload_benchmark ( c : & mut Criterion ) {
88+ let mut group = c. benchmark_group ( "wasm_component_load_call_unload" ) ;
89+
90+ let aot_path = concat ! (
91+ env!( "CARGO_MANIFEST_DIR" ) ,
92+ "/../../x64/release/runcomponent.aot"
93+ ) ;
94+ let ( mmap_base, mmap_len) = unsafe {
95+ let fd = libc:: open ( CString :: new ( aot_path) . unwrap ( ) . as_ptr ( ) , libc:: O_RDONLY ) ;
96+ assert ! (
97+ fd >= 0 ,
98+ "couldn't open {}: {:?}" ,
99+ aot_path,
100+ io:: Error :: last_os_error( )
101+ ) ;
102+
103+ let mut st = MaybeUninit :: < libc:: stat > :: uninit ( ) ;
104+ libc:: fstat ( fd, st. as_mut_ptr ( ) ) ;
105+ let st = st. assume_init ( ) ;
106+
107+ let page_size = libc:: sysconf ( libc:: _SC_PAGESIZE) as usize ;
108+ let len = ( st. st_size as usize ) . next_multiple_of ( page_size) ;
109+ let base = libc:: mmap (
110+ std:: ptr:: null_mut ( ) ,
111+ len,
112+ libc:: PROT_READ | libc:: PROT_WRITE | libc:: PROT_EXEC ,
113+ libc:: MAP_PRIVATE ,
114+ fd,
115+ 0 ,
116+ ) ;
117+ libc:: close ( fd) ;
118+ assert_ne ! (
119+ base,
120+ libc:: MAP_FAILED ,
121+ "mmap failed: {:?}" ,
122+ io:: Error :: last_os_error( )
123+ ) ;
124+ ( base, len)
125+ } ;
126+
127+ group. bench_function ( "load_call_unload" , |b : & mut Bencher < ' _ > | {
128+ let mut wasm_sandbox = Some ( {
129+ let state = State :: new ( ) ;
130+ let mut sandbox = SandboxBuilder :: new ( ) . build ( ) . unwrap ( ) ;
131+ let rt = bindings:: register_host_functions ( & mut sandbox, state) ;
132+ let sb = sandbox. load_runtime ( ) . unwrap ( ) ;
133+ ( sb, rt)
134+ } ) ;
135+
136+ b. iter ( || {
137+ let ( ws, rt) = wasm_sandbox. take ( ) . unwrap ( ) ;
138+ let loaded = unsafe { ws. load_module_by_mapping ( mmap_base, mmap_len) . unwrap ( ) } ;
139+ let mut wrapped = bindings:: RuncomponentSandbox { sb : loaded, rt } ;
140+ let instance =
141+ bindings:: example:: runcomponent:: RuncomponentExports :: guest ( & mut wrapped) ;
142+ instance. echo ( "Hello World!" . to_string ( ) ) ;
143+ let unloaded = wrapped. sb . unload_module ( ) . unwrap ( ) ;
144+ wasm_sandbox = Some ( ( unloaded, wrapped. rt ) ) ;
145+ } ) ;
146+ } ) ;
147+
148+ group. finish ( ) ;
149+
150+ unsafe {
151+ libc:: munmap ( mmap_base, mmap_len) ;
152+ }
153+ }
154+
83155fn get_loaded_wasm_sandbox ( ) -> (
84156 LoadedWasmSandbox ,
85157 Arc < Mutex < bindings:: RuncomponentResources < State > > > ,
@@ -90,15 +162,25 @@ fn get_loaded_wasm_sandbox() -> (
90162
91163 let sb = sandbox. load_runtime ( ) . unwrap ( ) ;
92164
93- let sb = sb
94- . load_module ( "../../x64/release/runcomponent.aot" )
95- . unwrap ( ) ;
165+ let aot_path = concat ! (
166+ env!( "CARGO_MANIFEST_DIR" ) ,
167+ "/../../x64/release/runcomponent.aot"
168+ ) ;
169+ let sb = sb. load_module ( aot_path) . unwrap ( ) ;
96170 ( sb, rt)
97171}
98172
173+ #[ cfg( not( windows) ) ]
99174criterion_group ! {
100175 name = benches_components;
101176 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
177+ targets = wasm_component_guest_call_benchmark, wasm_component_sandbox_benchmark, wasm_component_load_call_unload_benchmark
178+ }
179+
180+ #[ cfg( windows) ]
181+ criterion_group ! {
182+ name = benches_components;
183+ config = Criterion :: default ( ) ;
102184 targets = wasm_component_guest_call_benchmark, wasm_component_sandbox_benchmark
103185}
104186criterion_main ! ( benches_components) ;
0 commit comments