Skip to content

Commit f4fbb26

Browse files
authored
Add benchmark for measuring time to interrupt a sandbox (#893)
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent f3f610b commit f4fbb26

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/hyperlight_host/benches/benchmarks.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
#![expect(
18+
clippy::disallowed_macros,
19+
reason = "This is a benchmark file, so using disallowed macros is fine here."
20+
)]
21+
1722
use std::sync::Mutex;
1823

1924
use criterion::{Criterion, criterion_group, criterion_main};
@@ -117,6 +122,54 @@ fn guest_call_benchmark(c: &mut Criterion) {
117122
});
118123
});
119124

125+
// Measure the time between calling interrupt_handle.kill() and the guest function returning.
126+
group.bench_function("guest_call_time_to_interrupt", |b| {
127+
use std::sync::{Arc, Barrier};
128+
use std::thread;
129+
use std::time::Instant;
130+
131+
b.iter_custom(|iters| {
132+
let mut total_interrupt_latency = std::time::Duration::ZERO;
133+
134+
for _ in 0..iters {
135+
let mut sbox = create_multiuse_sandbox();
136+
let interrupt_handle = sbox.interrupt_handle();
137+
138+
let start_barrier = Arc::new(Barrier::new(2));
139+
let start_barrier_clone = Arc::clone(&start_barrier);
140+
141+
let observer_thread = thread::spawn(move || {
142+
start_barrier_clone.wait();
143+
144+
// Small delay to ensure the guest function is running in VM before interrupting
145+
thread::sleep(std::time::Duration::from_millis(1));
146+
let kill_start = Instant::now();
147+
interrupt_handle.kill();
148+
kill_start
149+
});
150+
151+
start_barrier.wait();
152+
153+
let result = sbox.call::<i32>("Spin", ());
154+
155+
let call_end = Instant::now();
156+
let kill_start = observer_thread.join().unwrap();
157+
158+
assert!(
159+
matches!(
160+
result,
161+
Err(hyperlight_host::HyperlightError::ExecutionCanceledByHost())
162+
),
163+
"Guest function should be interrupted"
164+
);
165+
166+
total_interrupt_latency += call_end.duration_since(kill_start);
167+
}
168+
169+
total_interrupt_latency
170+
});
171+
});
172+
120173
group.finish();
121174
}
122175

@@ -228,7 +281,6 @@ fn function_call_serialization_benchmark(c: &mut Criterion) {
228281
group.finish();
229282
}
230283

231-
#[allow(clippy::disallowed_macros)]
232284
fn sample_workloads_benchmark(c: &mut Criterion) {
233285
let mut group = c.benchmark_group("sample_workloads");
234286

0 commit comments

Comments
 (0)