fast_able/fast_thread_pool/
mod.rs1#[cfg(test)]
2pub mod test;
3
4pub mod pool;
5pub use pool::*;
6
7pub mod lite;
8pub use lite::*;
9
10pub mod const_num;
11pub use const_num::*;
12
13pub mod channel_types;
14pub use channel_types::*;
15
16pub mod task_executor;
17pub use task_executor::*;
18
19pub mod utils;
20pub use utils::*;
21
22#[cfg(test)]
23pub mod test_channel_features;
24
25#[test]
26fn test_thread() {
27 unsafe { std::env::set_var("RUST_LOG", "debug") };
30 env_logger::init();
31
32 test::_main_loop(30, 100);
34
35 std::thread::sleep(std::time::Duration::from_secs(3));
43}
44
45#[test]
63fn _test_task_executor() {
64 unsafe { std::env::set_var("RUST_LOG", "debug") };
65 env_logger::init();
66
67 init(false);
68 let pool = TaskExecutor::new(core_affinity::CoreId { id: 21 }, -1);
69 std::thread::sleep(std::time::Duration::from_millis(200));
70
71 let count = std::sync::Arc::new(crossbeam::atomic::AtomicCell::new(0_i64));
72 let elapsed_total = std::sync::Arc::new(crossbeam::atomic::AtomicCell::new(0_i64));
73 let elapsed_exp = std::sync::Arc::new(crossbeam::atomic::AtomicCell::new(0_i64));
74
75 let count_c = count.clone();
76 let elapsed_total_c = elapsed_total.clone();
77 let elapsed_exp_c = elapsed_exp.clone();
78 std::thread::spawn(move || loop {
79 std::thread::sleep(std::time::Duration::from_secs(3));
80 let count = count_c.fetch_and(0);
81 let elapsed_total = elapsed_total_c.fetch_and(0);
82 let elapsed_exp = elapsed_exp_c.fetch_and(0);
83 info!(
84 "3秒钟执行任务数: {}, 所有任务耗时(微秒): {}, 平均耗时: {}, 耗时任务数(100微秒): {}, 耗时任务数占比: {:.0}/10000",
85 count,
86 elapsed_total,
87 elapsed_total / count,
88 elapsed_exp,
89 elapsed_exp as f64 / count as f64 * 10000.0,
90 );
91 });
92
93 loop {
94 for i in 0..100 {
95 let time_hs = std::time::Instant::now();
96 let count = count.clone();
97 let elapsed_total = elapsed_total.clone();
98 let elapsed_exp = elapsed_exp.clone();
99 pool.spawn(move |_| {
102 let micros = time_hs.elapsed().as_micros();
103 count.fetch_add(1);
104 elapsed_total.fetch_add(micros as i64);
105 if micros > 100 {
106 elapsed_exp.fetch_add(1);
107 }
108 });
109 }
110 std::thread::sleep(std::time::Duration::from_micros(110));
111 }
112 std::thread::sleep(std::time::Duration::from_secs(9999));
113}
114
115#[test]
116fn _test_tokio() {
117 unsafe { std::env::set_var("RUST_LOG", "debug") };
118 env_logger::init();
119
120 init(false);
121 let pool = tokio::runtime::Builder::new_multi_thread()
122 .worker_threads(4)
123 .enable_all()
124 .build()
125 .unwrap();
126 std::thread::sleep(std::time::Duration::from_millis(200));
127
128 static statis_v: once_cell::sync::Lazy<crate::statis::Statis> =
129 once_cell::sync::Lazy::new(|| crate::statis::Statis::new(|v| debug!("一秒并发: {v}")));
130
131 static thread_lite: once_cell::sync::Lazy<crate::fast_thread_pool::ThreadPoolLite> =
132 once_cell::sync::Lazy::new(|| crate::fast_thread_pool::ThreadPoolLite::new());
133 thread_lite.spawn(move || {
134 warn!("thread_lite init");
135 });
136 std::thread::sleep(std::time::Duration::from_millis(600));
137
138 loop {
139 for _ in 0..500 {
140 let time_hs = std::time::Instant::now();
141 pool.spawn_blocking(move || {
142 let micros = time_hs.elapsed().as_micros();
144 if micros > 1000 {
145 thread_lite.spawn(move || {
146 warn!("任务耗时过长: {} micros", micros);
147 });
148 }
149 statis_v.add();
150 });
151 }
152 }
154 std::thread::sleep(std::time::Duration::from_secs(9999));
155}