use callcomapi_macros::{com_thread, with_com};
use callcomapi_runtime::{ComModel, init_com};
use std::time::Instant;
mod common;
#[with_com]
fn macro_with() {
common::call_com_api().unwrap();
}
#[com_thread]
fn macro_thread() -> std::thread::ThreadId {
common::call_com_api().unwrap();
std::thread::current().id()
}
#[com_thread]
async fn macro_thread_async() -> std::thread::ThreadId {
common::call_com_api().unwrap();
std::thread::current().id()
}
#[test]
fn frequent_calls_comparison() {
const ITER: usize = 1_00;
let start = Instant::now();
for _ in 0..ITER {
let _guard = unsafe { init_com(ComModel::STA) };
common::call_com_api().unwrap();
}
let manual_dur = start.elapsed();
let start = Instant::now();
for _ in 0..ITER {
macro_with();
}
let with_com_dur = start.elapsed();
let start = Instant::now();
let mut first_tid = None;
for _ in 0..ITER {
let tid = macro_thread();
if let Some(ft) = first_tid {
assert_eq!(tid, ft, "com_thread 应该在相同线程上执行");
} else {
first_tid = Some(tid);
}
}
let thread_sync_dur = start.elapsed();
let start = Instant::now();
let mut first_tid_async = None;
for _ in 0..ITER {
let tid = futures::executor::block_on(macro_thread_async());
if let Some(ft) = first_tid_async {
assert_eq!(tid, ft, "com_thread async 应该在相同线程上执行");
} else {
first_tid_async = Some(tid);
}
}
let thread_async_dur = start.elapsed();
println!(
"manual init: {:?}\nwith_com: {:?}\ncom_thread(sync): {:?}\ncom_thread(async): {:?}",
manual_dur, with_com_dur, thread_sync_dur, thread_async_dur
);
}