fast_able/fast_thread_pool/mod.rs
1#[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 // fast_able::fast_thread_pool::_test_get_core_ids();
28
29 unsafe { std::env::set_var("RUST_LOG", "debug") };
30 env_logger::init();
31
32 // Average thread startup time: 1761 ns
33 // 线程开启平均耗时: 1761 ns
34 test::_main_loop(30, 100);
35
36 // Average thread startup time: 956.6 ns
37 // 线程开启平均耗时: 956.6 ns
38 // fast_able::fast_thread_pool::thread_mod::_test_ThreadPoolConstNum(1000);
39
40 // thread_lite test result: Average thread startup time: 168.1962 micros
41 // Because threads are started serially and synchronously, the time consumption is relatively long
42 // thread_lite 测试结果: 线程开启平均耗时: 168.1962 micros
43 // 因为是串行同步开启线程, 所以耗时会比较长
44 // fast_able::fast_thread_pool::thread_mod::_test_thread_lite(1000);
45
46 std::thread::sleep(std::time::Duration::from_secs(3));
47}
48
49/*
50[2024-07-01T08:11:32Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 456000, 所有任务耗时(微秒): 1245146, 平均耗时: 2, 耗时任务数(100微秒): 255, 耗时任务数占比: 6/10000
51[2024-07-01T08:11:35Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 456300, 所有任务耗时(微秒): 1181427, 平均耗时: 2, 耗时任务数(100微秒): 223, 耗时任务数占比: 5/10000
52[2024-07-01T08:11:38Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 456700, 所有任务耗时(微秒): 1176375, 平均耗时: 2, 耗时任务数(100微秒): 183, 耗时任务数占比: 4/10000
53[2024-07-01T08:11:41Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 457200, 所有任务耗时(微秒): 1218507, 平均耗时: 2, 耗时任务数(100微秒): 236, 耗时任务数占比: 5/10000
54[2024-07-01T08:11:44Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 457100, 所有任务耗时(微秒): 1159028, 平均耗时: 2, 耗时任务数(100微秒): 113, 耗时任务数占比: 2/10000
55[2024-07-01T08:11:47Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 456808, 所有任务耗时(微秒): 1170696, 平均耗时: 2, 耗时任务数(100微秒): 218, 耗时任务数占比: 5/10000
56[2024-07-01T08:11:50Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 456992, 所有任务耗时(微秒): 1254381, 平均耗时: 2, 耗时任务数(100微秒): 242, 耗时任务数占比: 5/10000
57[2024-07-01T08:11:53Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 458001, 所有任务耗时(微秒): 1181228, 平均耗时: 2, 耗时任务数(100微秒): 165, 耗时任务数占比: 4/10000
58[2024-07-01T08:11:56Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 454999, 所有任务耗时(微秒): 1220606, 平均耗时: 2, 耗时任务数(100微秒): 230, 耗时任务数占比: 5/10000
59[2024-07-01T08:11:59Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 457400, 所有任务耗时(微秒): 1203115, 平均耗时: 2, 耗时任务数(100微秒): 155, 耗时任务数占比: 3/10000
60[2024-07-01T08:12:02Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 456400, 所有任务耗时(微秒): 1316857, 平均耗时: 2, 耗时任务数(100微秒): 474, 耗时任务数占比: 10/10000
61[2024-07-01T08:12:05Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 456100, 所有任务耗时(微秒): 1189001, 平均耗时: 2, 耗时任务数(100微秒): 113, 耗时任务数占比: 2/10000
62[2024-07-01T08:12:08Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 457000, 所有任务耗时(微秒): 1273741, 平均耗时: 2, 耗时任务数(100微秒): 309, 耗时任务数占比: 7/10000
63[2024-07-01T08:12:11Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 456700, 所有任务耗时(微秒): 1175973, 平均耗时: 2, 耗时任务数(100微秒): 326, 耗时任务数占比: 7/10000
64[2024-07-01T08:12:14Z INFO fast_able::fast_thread_pool] 3秒钟执行任务数: 456400, 所有任务耗时(微秒): 1178047, 平均耗时: 2, 耗时任务数(100微秒): 221, 耗时任务数占比: 5/10000
65*/
66#[test]
67fn _test_task_executor() {
68 unsafe { std::env::set_var("RUST_LOG", "debug") };
69 env_logger::init();
70
71 init(false);
72 let pool = TaskExecutor::new(core_affinity::CoreId { id: 21 }, -1);
73 std::thread::sleep(std::time::Duration::from_millis(200));
74
75 let count = std::sync::Arc::new(crossbeam::atomic::AtomicCell::new(0_i64));
76 let elapsed_total = std::sync::Arc::new(crossbeam::atomic::AtomicCell::new(0_i64));
77 let elapsed_exp = std::sync::Arc::new(crossbeam::atomic::AtomicCell::new(0_i64));
78
79 let count_c = count.clone();
80 let elapsed_total_c = elapsed_total.clone();
81 let elapsed_exp_c = elapsed_exp.clone();
82 std::thread::spawn(move || loop {
83 std::thread::sleep(std::time::Duration::from_secs(3));
84 let count = count_c.fetch_and(0);
85 let elapsed_total = elapsed_total_c.fetch_and(0);
86 let elapsed_exp = elapsed_exp_c.fetch_and(0);
87 info!(
88 "3秒钟执行任务数: {}, 所有任务耗时(微秒): {}, 平均耗时: {}, 耗时任务数(100微秒): {}, 耗时任务数占比: {:.0}/10000",
89 count,
90 elapsed_total,
91 elapsed_total / count,
92 elapsed_exp,
93 elapsed_exp as f64 / count as f64 * 10000.0,
94 );
95 });
96
97 loop {
98 for i in 0..100 {
99 let time_hs = std::time::Instant::now();
100 let count = count.clone();
101 let elapsed_total = elapsed_total.clone();
102 let elapsed_exp = elapsed_exp.clone();
103 // spin::Barrier::new(i % 10).wait();
104 // spin::relax::Loop::(Duration::from_micros(i % 50));
105 pool.spawn(move |_| {
106 let micros = time_hs.elapsed().as_micros();
107 count.fetch_add(1);
108 elapsed_total.fetch_add(micros as i64);
109 if micros > 100 {
110 elapsed_exp.fetch_add(1);
111 }
112 });
113 }
114 std::thread::sleep(std::time::Duration::from_micros(110));
115 }
116 std::thread::sleep(std::time::Duration::from_secs(9999));
117}
118
119#[test]
120fn _test_tokio() {
121 unsafe { std::env::set_var("RUST_LOG", "debug") };
122 env_logger::init();
123
124 init(false);
125 let pool = tokio::runtime::Builder::new_multi_thread()
126 .worker_threads(4)
127 .enable_all()
128 .build()
129 .unwrap();
130 std::thread::sleep(std::time::Duration::from_millis(200));
131
132 static statis_v: once_cell::sync::Lazy<crate::statis::Statis> =
133 once_cell::sync::Lazy::new(|| crate::statis::Statis::new(|v| debug!("一秒并发: {v}")));
134
135 static thread_lite: once_cell::sync::Lazy<crate::fast_thread_pool::ThreadPoolLite> =
136 once_cell::sync::Lazy::new(|| crate::fast_thread_pool::ThreadPoolLite::new());
137 thread_lite.spawn(move || {
138 warn!("thread_lite init");
139 });
140 std::thread::sleep(std::time::Duration::from_millis(600));
141
142 loop {
143 for _ in 0..500 {
144 let time_hs = std::time::Instant::now();
145 pool.spawn_blocking(move || {
146 // println!("run _test_thread_lite i: {}", i);
147 let micros = time_hs.elapsed().as_micros();
148 if micros > 1000 {
149 thread_lite.spawn(move || {
150 warn!("任务耗时过长: {} micros", micros);
151 });
152 }
153 statis_v.add();
154 });
155 }
156 // std::thread::sleep(std::time::Duration::from_micros(550));
157 }
158 std::thread::sleep(std::time::Duration::from_secs(9999));
159}