Skip to content

Commit 08d8d8c

Browse files
authored
Add benchmark for measuring performance impact on moving sandbox across thread (#882)
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 3c82695 commit 08d8d8c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/hyperlight_host/benches/benchmarks.rs

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

17+
use std::sync::Mutex;
18+
1719
use criterion::{Criterion, criterion_group, criterion_main};
1820
use flatbuffers::FlatBufferBuilder;
1921
use hyperlight_common::flatbuffer_wrappers::function_call::{FunctionCall, FunctionCallType};
@@ -74,6 +76,47 @@ fn guest_call_benchmark(c: &mut Criterion) {
7476
});
7577
});
7678

79+
// same as guest_call, but the call will be made on different thread
80+
group.bench_function("guest_call_on_different_thread", |b| {
81+
use std::sync::{Arc, Barrier};
82+
use std::thread;
83+
use std::time::Instant;
84+
85+
b.iter_custom(|iters| {
86+
let mut total_duration = std::time::Duration::ZERO;
87+
let sbox = Arc::new(Mutex::new(create_multiuse_sandbox()));
88+
89+
for _ in 0..iters {
90+
// Ensure vcpu is "bound" on this main thread
91+
{
92+
let mut sbox = sbox.lock().unwrap();
93+
sbox.call::<String>("Echo", "warmup\n".to_string()).unwrap();
94+
}
95+
96+
let barrier = Arc::new(Barrier::new(2));
97+
let barrier_clone = Arc::clone(&barrier);
98+
let sbox_clone = Arc::clone(&sbox);
99+
100+
let handle = thread::spawn(move || {
101+
barrier_clone.wait();
102+
103+
let mut sbox = sbox_clone.lock().unwrap();
104+
let start = Instant::now();
105+
// Measure the first call after thread switch
106+
// According to KVM docs, this should show performance impact
107+
sbox.call::<String>("Echo", "hello\n".to_string()).unwrap();
108+
start.elapsed()
109+
});
110+
111+
barrier.wait();
112+
113+
total_duration += handle.join().unwrap();
114+
}
115+
116+
total_duration
117+
});
118+
});
119+
77120
group.finish();
78121
}
79122

src/tests/rust_guests/witguest/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)