-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathbenchmarks.rs
More file actions
144 lines (115 loc) · 5.22 KB
/
benchmarks.rs
File metadata and controls
144 lines (115 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Copyright 2025 The Hyperlight Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use criterion::{Criterion, criterion_group, criterion_main};
use hyperlight_host::GuestBinary;
use hyperlight_host::sandbox::{Callable, Sandbox, SandboxConfiguration, UninitializedSandbox};
use hyperlight_testing::simple_guest_as_string;
fn create_uninit_sandbox() -> UninitializedSandbox {
let path = simple_guest_as_string().unwrap();
UninitializedSandbox::new(GuestBinary::FilePath(path), None).unwrap()
}
fn create_multiuse_sandbox() -> Sandbox {
create_uninit_sandbox().init().unwrap()
}
fn guest_call_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("guest_functions");
// Benchmarks a single guest function call.
// The benchmark does **not** include the time to reset the sandbox memory after the call.
group.bench_function("guest_call", |b| {
let mut sbox = create_multiuse_sandbox();
b.iter(|| sbox.call::<String>("Echo", "hello\n".to_string()).unwrap());
});
// Benchmarks a single guest function call.
// The benchmark does include the time to reset the sandbox memory after the call.
group.bench_function("guest_call_with_restore", |b| {
let mut sbox = create_multiuse_sandbox();
let snapshot = sbox.snapshot().unwrap();
b.iter(|| {
sbox.call::<String>("Echo", "hello\n".to_string()).unwrap();
sbox.restore(&snapshot).unwrap();
});
});
// Benchmarks a guest function call calling into the host.
// The benchmark does **not** include the time to reset the sandbox memory after the call.
group.bench_function("guest_call_with_call_to_host_function", |b| {
let mut uninitialized_sandbox = create_uninit_sandbox();
// Define a host function that adds two integers and register it.
uninitialized_sandbox
.register("HostAdd", |a: i32, b: i32| Ok(a + b))
.unwrap();
let mut multiuse_sandbox: Sandbox = uninitialized_sandbox.init().unwrap();
b.iter(|| {
multiuse_sandbox
.call::<i32>("Add", (1_i32, 41_i32))
.unwrap()
});
});
group.finish();
}
fn guest_call_benchmark_large_param(c: &mut Criterion) {
let mut group = c.benchmark_group("guest_functions_with_large_parameters");
#[cfg(target_os = "windows")]
group.sample_size(10); // This benchmark is very slow on Windows, so we reduce the sample size to avoid long test runs.
// 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
group.bench_function("guest_call_with_large_parameters", |b| {
const SIZE: usize = 50 * 1024 * 1024; // 50 MB
let large_vec = vec![0u8; SIZE];
let large_string = unsafe { String::from_utf8_unchecked(large_vec.clone()) }; // Safety: indeed above vec is valid utf8
let mut config = SandboxConfiguration::default();
config.set_input_data_size(2 * SIZE + (1024 * 1024)); // 2 * SIZE + 1 MB, to allow 1MB for the rest of the serialized function call
config.set_heap_size(SIZE as u64 * 15);
let sandbox = UninitializedSandbox::new(
GuestBinary::FilePath(simple_guest_as_string().unwrap()),
Some(config),
)
.unwrap();
let mut sandbox = sandbox.init().unwrap();
b.iter(|| {
sandbox
.call_guest_function_by_name::<()>(
"LargeParameters",
(large_vec.clone(), large_string.clone()),
)
.unwrap()
});
});
group.finish();
}
fn sandbox_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("sandboxes");
// Benchmarks the time to create a new uninitialized sandbox.
// Does **not** include the time to drop the sandbox.
group.bench_function("create_uninitialized_sandbox", |b| {
b.iter_with_large_drop(create_uninit_sandbox);
});
// Benchmarks the time to create a new uninitialized sandbox and drop it.
group.bench_function("create_uninitialized_sandbox_and_drop", |b| {
b.iter(create_uninit_sandbox);
});
// Benchmarks the time to create a new sandbox.
// Does **not** include the time to drop the sandbox.
group.bench_function("create_sandbox", |b| {
b.iter_with_large_drop(create_multiuse_sandbox);
});
// Benchmarks the time to create a new sandbox and drop it.
group.bench_function("create_sandbox_and_drop", |b| {
b.iter(create_multiuse_sandbox);
});
group.finish();
}
criterion_group! {
name = benches;
config = Criterion::default();
targets = guest_call_benchmark, sandbox_benchmark, guest_call_benchmark_large_param
}
criterion_main!(benches);