@@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17+ use std:: sync:: Mutex ;
18+
1719use criterion:: { Criterion , criterion_group, criterion_main} ;
1820use flatbuffers:: FlatBufferBuilder ;
1921use 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
0 commit comments