From 692c24bc3dc6289de483ac167b2fc9f8aa8022c3 Mon Sep 17 00:00:00 2001 From: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> Date: Wed, 10 Sep 2025 10:56:24 -0700 Subject: [PATCH] Add benchmark for measuring performance impact on moving sandbox across thread Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> --- src/hyperlight_host/benches/benchmarks.rs | 43 +++++++++++++++++++++++ src/tests/rust_guests/witguest/Cargo.lock | 4 +-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/hyperlight_host/benches/benchmarks.rs b/src/hyperlight_host/benches/benchmarks.rs index 3ed91820f..61d405370 100644 --- a/src/hyperlight_host/benches/benchmarks.rs +++ b/src/hyperlight_host/benches/benchmarks.rs @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +use std::sync::Mutex; + use criterion::{Criterion, criterion_group, criterion_main}; use flatbuffers::FlatBufferBuilder; use hyperlight_common::flatbuffer_wrappers::function_call::{FunctionCall, FunctionCallType}; @@ -74,6 +76,47 @@ fn guest_call_benchmark(c: &mut Criterion) { }); }); + // same as guest_call, but the call will be made on different thread + group.bench_function("guest_call_on_different_thread", |b| { + use std::sync::{Arc, Barrier}; + use std::thread; + use std::time::Instant; + + b.iter_custom(|iters| { + let mut total_duration = std::time::Duration::ZERO; + let sbox = Arc::new(Mutex::new(create_multiuse_sandbox())); + + for _ in 0..iters { + // Ensure vcpu is "bound" on this main thread + { + let mut sbox = sbox.lock().unwrap(); + sbox.call::("Echo", "warmup\n".to_string()).unwrap(); + } + + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = Arc::clone(&barrier); + let sbox_clone = Arc::clone(&sbox); + + let handle = thread::spawn(move || { + barrier_clone.wait(); + + let mut sbox = sbox_clone.lock().unwrap(); + let start = Instant::now(); + // Measure the first call after thread switch + // According to KVM docs, this should show performance impact + sbox.call::("Echo", "hello\n".to_string()).unwrap(); + start.elapsed() + }); + + barrier.wait(); + + total_duration += handle.join().unwrap(); + } + + total_duration + }); + }); + group.finish(); } diff --git a/src/tests/rust_guests/witguest/Cargo.lock b/src/tests/rust_guests/witguest/Cargo.lock index 762a25e97..6a33cb503 100644 --- a/src/tests/rust_guests/witguest/Cargo.lock +++ b/src/tests/rust_guests/witguest/Cargo.lock @@ -521,9 +521,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "wasmparser" -version = "0.238.1" +version = "0.239.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fa99c8328024423875ae4a55345cfde8f0371327fb2d0f33b0f52a06fc44408" +checksum = "8c9d90bb93e764f6beabf1d02028c70a2156a6583e63ac4218dd07ef733368b0" dependencies = [ "bitflags", "hashbrown",