@@ -79,37 +79,65 @@ fn guest_call_benchmark(c: &mut Criterion) {
7979 group. finish ( ) ;
8080}
8181
82- fn guest_call_benchmark_large_param ( c : & mut Criterion ) {
82+ fn guest_call_benchmark_large_params ( c : & mut Criterion ) {
8383 let mut group = c. benchmark_group ( "guest_functions_with_large_parameters" ) ;
8484 #[ cfg( target_os = "windows" ) ]
8585 group. sample_size ( 10 ) ; // This benchark is very slow on Windows, so we reduce the sample size to avoid long test runs.
8686
87- // This benchmark includes time to first clone a vector and string, so it is not a "pure' benchmark of the guest call, but it's still useful
88- group. bench_function ( "guest_call_with_large_parameters" , |b| {
89- const SIZE : usize = 50 * 1024 * 1024 ; // 50 MB
90- let large_vec = vec ! [ 0u8 ; SIZE ] ;
91- let large_string = unsafe { String :: from_utf8_unchecked ( large_vec. clone ( ) ) } ; // Safety: indeed above vec is valid utf8
87+ // Helper function to create a benchmark for a specific size
88+ let create_benchmark = |group : & mut criterion:: BenchmarkGroup < _ > , size_mb : usize | {
89+ let benchmark_name = format ! ( "guest_call_with_2_large_parameters_{}mb each" , size_mb) ;
90+ group. bench_function ( & benchmark_name, |b| {
91+ let size = size_mb * 1024 * 1024 ; // Convert MB to bytes
92+ let large_vec = vec ! [ 0u8 ; size] ;
93+ let large_string = unsafe { String :: from_utf8_unchecked ( large_vec. clone ( ) ) } ; // Safety: indeed above vec is valid utf8
9294
93- let mut config = SandboxConfiguration :: default ( ) ;
94- config. set_input_data_size ( 2 * SIZE + ( 1024 * 1024 ) ) ; // 2 * SIZE + 1 MB, to allow 1MB for the rest of the serialized function call
95- config. set_heap_size ( SIZE as u64 * 15 ) ;
96-
97- let sandbox = UninitializedSandbox :: new (
98- GuestBinary :: FilePath ( simple_guest_as_string ( ) . unwrap ( ) ) ,
99- Some ( config) ,
100- )
101- . unwrap ( ) ;
102- let mut sandbox = sandbox. evolve ( Noop :: default ( ) ) . unwrap ( ) ;
103-
104- b. iter ( || {
105- sandbox
106- . call_guest_function_by_name :: < ( ) > (
107- "LargeParameters" ,
108- ( large_vec. clone ( ) , large_string. clone ( ) ) ,
109- )
110- . unwrap ( )
95+ let mut config = SandboxConfiguration :: default ( ) ;
96+ config. set_input_data_size ( 2 * size + ( 1024 * 1024 ) ) ;
97+
98+ if size < 50 * 1024 * 1024 {
99+ config. set_heap_size ( size as u64 * 16 ) ;
100+ } else {
101+ config. set_heap_size ( size as u64 * 11 ) ; // Set to 1GB for larger sizes
102+ }
103+
104+ let sandbox = UninitializedSandbox :: new (
105+ GuestBinary :: FilePath ( simple_guest_as_string ( ) . unwrap ( ) ) ,
106+ Some ( config) ,
107+ )
108+ . unwrap ( ) ;
109+ let mut sandbox = sandbox. evolve ( Noop :: default ( ) ) . unwrap ( ) ;
110+
111+ b. iter_custom ( |iters| {
112+ let mut total_duration = std:: time:: Duration :: new ( 0 , 0 ) ;
113+
114+ for _ in 0 ..iters {
115+ // Clone the data (not measured)
116+ let vec_clone = large_vec. clone ( ) ;
117+ let string_clone = large_string. clone ( ) ;
118+
119+ // Measure only the guest function call
120+ let start = std:: time:: Instant :: now ( ) ;
121+ sandbox
122+ . call_guest_function_by_name :: < ( ) > (
123+ "LargeParameters" ,
124+ ( vec_clone, string_clone) ,
125+ )
126+ . unwrap ( ) ;
127+ total_duration += start. elapsed ( ) ;
128+ }
129+
130+ total_duration
131+ } ) ;
111132 } ) ;
112- } ) ;
133+ } ;
134+
135+ // Create benchmarks for different sizes
136+ create_benchmark ( & mut group, 5 ) ; // 5MB
137+ create_benchmark ( & mut group, 10 ) ; // 10MB
138+ create_benchmark ( & mut group, 20 ) ; // 20MB
139+ create_benchmark ( & mut group, 40 ) ; // 40MB
140+ create_benchmark ( & mut group, 60 ) ; // 60MB
113141
114142 group. finish ( ) ;
115143}
@@ -290,6 +318,6 @@ fn guest_call_heap_size_benchmark(c: &mut Criterion) {
290318criterion_group ! {
291319 name = benches;
292320 config = Criterion :: default ( ) ;
293- targets = guest_call_benchmark, sandbox_benchmark, sandbox_heap_size_benchmark, guest_call_benchmark_large_param , guest_call_heap_size_benchmark
321+ targets = guest_call_benchmark, sandbox_benchmark, sandbox_heap_size_benchmark, guest_call_benchmark_large_params , guest_call_heap_size_benchmark
294322}
295323criterion_main ! ( benches) ;
0 commit comments