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