#[cfg(test)]
pub mod test;
pub mod pool;
pub use pool::*;
pub mod lite;
pub use lite::*;
pub mod const_num;
pub use const_num::*;
pub mod channel_types;
pub use channel_types::*;
pub mod task_executor;
pub use task_executor::*;
pub mod utils;
pub use utils::*;
#[cfg(test)]
pub mod test_channel_features;
#[test]
fn test_thread() {
unsafe { std::env::set_var("RUST_LOG", "debug") };
env_logger::init();
test::_main_loop(30, 100);
std::thread::sleep(std::time::Duration::from_secs(3));
}
#[test]
fn _test_task_executor() {
unsafe { std::env::set_var("RUST_LOG", "debug") };
env_logger::init();
init(false);
let pool = TaskExecutor::new(core_affinity::CoreId { id: 21 }, -1);
std::thread::sleep(std::time::Duration::from_millis(200));
let count = std::sync::Arc::new(crossbeam::atomic::AtomicCell::new(0_i64));
let elapsed_total = std::sync::Arc::new(crossbeam::atomic::AtomicCell::new(0_i64));
let elapsed_exp = std::sync::Arc::new(crossbeam::atomic::AtomicCell::new(0_i64));
let count_c = count.clone();
let elapsed_total_c = elapsed_total.clone();
let elapsed_exp_c = elapsed_exp.clone();
std::thread::spawn(move || loop {
std::thread::sleep(std::time::Duration::from_secs(3));
let count = count_c.fetch_and(0);
let elapsed_total = elapsed_total_c.fetch_and(0);
let elapsed_exp = elapsed_exp_c.fetch_and(0);
info!(
"3秒钟执行任务数: {}, 所有任务耗时(微秒): {}, 平均耗时: {}, 耗时任务数(100微秒): {}, 耗时任务数占比: {:.0}/10000",
count,
elapsed_total,
elapsed_total / count,
elapsed_exp,
elapsed_exp as f64 / count as f64 * 10000.0,
);
});
loop {
for i in 0..100 {
let time_hs = std::time::Instant::now();
let count = count.clone();
let elapsed_total = elapsed_total.clone();
let elapsed_exp = elapsed_exp.clone();
pool.spawn(move |_| {
let micros = time_hs.elapsed().as_micros();
count.fetch_add(1);
elapsed_total.fetch_add(micros as i64);
if micros > 100 {
elapsed_exp.fetch_add(1);
}
});
}
std::thread::sleep(std::time::Duration::from_micros(110));
}
std::thread::sleep(std::time::Duration::from_secs(9999));
}
#[test]
fn _test_tokio() {
unsafe { std::env::set_var("RUST_LOG", "debug") };
env_logger::init();
init(false);
let pool = tokio::runtime::Builder::new_multi_thread()
.worker_threads(4)
.enable_all()
.build()
.unwrap();
std::thread::sleep(std::time::Duration::from_millis(200));
static statis_v: once_cell::sync::Lazy<crate::statis::Statis> =
once_cell::sync::Lazy::new(|| crate::statis::Statis::new(|v| debug!("一秒并发: {v}")));
static thread_lite: once_cell::sync::Lazy<crate::fast_thread_pool::ThreadPoolLite> =
once_cell::sync::Lazy::new(|| crate::fast_thread_pool::ThreadPoolLite::new());
thread_lite.spawn(move || {
warn!("thread_lite init");
});
std::thread::sleep(std::time::Duration::from_millis(600));
loop {
for _ in 0..500 {
let time_hs = std::time::Instant::now();
pool.spawn_blocking(move || {
let micros = time_hs.elapsed().as_micros();
if micros > 1000 {
thread_lite.spawn(move || {
warn!("任务耗时过长: {} micros", micros);
});
}
statis_v.add();
});
}
}
std::thread::sleep(std::time::Duration::from_secs(9999));
}