-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvancedThreadPoolBenchmark.h
More file actions
277 lines (233 loc) · 10.1 KB
/
AdvancedThreadPoolBenchmark.h
File metadata and controls
277 lines (233 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#ifndef __ADVANCED_THREAD_POOL_BENCHMARK_H__
#define __ADVANCED_THREAD_POOL_BENCHMARK_H__
#include <iostream>
#include <vector>
#include <future>
#include <atomic>
#include <mutex>
#include <chrono>
#include <thread>
#include <algorithm>
#include "ThreadPool.h"
void stress_test_short_tasks() {
ThreadPool pool(8); // 8个工作线程
const int TOTAL_TASKS = 100000; // 10万个任务
std::atomic<int> completed_tasks{0};
std::vector<std::future<void>> futures;
auto start = std::chrono::high_resolution_clock::now();
// 提交大量短时任务
for (int i = 0; i < TOTAL_TASKS; ++i) {
auto future = pool.submit([&completed_tasks, i]() {
// 模拟一个非常短的计算任务
volatile int result = i * i;
completed_tasks.fetch_add(1, std::memory_order_relaxed);
});
futures.push_back(std::move(future));
}
// 等待所有任务完成
for (auto& future : futures) {
future.get();
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "短任务压力测试完成:" << std::endl;
std::cout << " 总任务数: " << TOTAL_TASKS << std::endl;
std::cout << " 完成任务数: " << completed_tasks.load() << std::endl;
std::cout << " 总耗时: " << duration.count() << "ms" << std::endl;
std::cout << " 吞吐量: " << (TOTAL_TASKS * 1000.0 / duration.count()) << " 任务/秒" << std::endl;
// 验证完整性
if (completed_tasks == TOTAL_TASKS) {
std::cout << " ✅ 测试通过: 所有任务均完成" << std::endl;
} else {
std::cout << " ❌ 测试失败: 任务完成数不符预期" << std::endl;
}
}
void stress_test_mixed_workload() {
ThreadPool pool(6);
std::vector<std::future<std::string>> results;
std::atomic<int> short_tasks_done{0};
std::atomic<int> long_tasks_done{0};
// 混合提交短任务和长任务
for (int i = 0; i < 100; ++i) {
// 70%短任务,30%长任务
if (i % 10 < 7) {
// 短任务:快速计算
results.push_back(pool.submit([&short_tasks_done, i]() -> std::string {
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // 短延迟
short_tasks_done.fetch_add(1, std::memory_order_relaxed);
return "短任务-" + std::to_string(i) + " 完成";
}));
} else {
// 长任务:较重计算负载
results.push_back(pool.submit([&long_tasks_done, i]() -> std::string {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 长延迟
// 模拟计算密集型任务
volatile int sum = 0;
for (int j = 0; j < 100000; j++) {
sum += j % 10;
}
long_tasks_done.fetch_add(1, std::memory_order_relaxed);
return "长任务-" + std::to_string(i) + " 完成,计算结果:" + std::to_string(sum);
}));
}
}
// 收集结果,检查是否死锁
for (auto& future : results) {
try {
std::string result = future.get();
std::cout << result << std::endl;
} catch (const std::exception& e) {
std::cout << "任务执行异常: " << e.what() << std::endl;
}
}
std::cout << "混合负载测试完成: " << short_tasks_done << " 个短任务, "
<< long_tasks_done << " 个长任务" << std::endl;
}
void stress_test_data_race() {
ThreadPool pool(4);
std::vector<std::future<void>> futures;
std::atomic<int> counter{0};
const int INCREMENTS_PER_TASK = 1000;
const int TASK_COUNT = 100;
// 测试原子操作的正确性
for (int i = 0; i < TASK_COUNT; ++i) {
futures.push_back(pool.submit([&counter]() {
for (int j = 0; j < INCREMENTS_PER_TASK; ++j) {
counter.fetch_add(1, std::memory_order_relaxed);
}
}));
}
for (auto& future : futures) {
future.get();
}
std::cout << "数据竞争测试: 期望值=" << (TASK_COUNT * INCREMENTS_PER_TASK)
<< ", 实际值=" << counter.load() << std::endl;
if (counter == TASK_COUNT * INCREMENTS_PER_TASK) {
std::cout << " ✅ 原子性测试通过: 无数据竞争" << std::endl;
} else {
std::cout << " ❌ 原子性测试失败: 存在数据竞争" << std::endl;
}
}
// 测试任务:模拟一定计算负载
void sample_task(int task_complexity) {
volatile int result = 0;
for (int i = 0; i < task_complexity; ++i) {
result += i % 100;
}
// 防止编译器过度优化
asm volatile("" : "+r,m"(result) : : "memory");
}
// 线程池模式性能测试
void benchmark_thread_pool() {
ThreadPool pool(std::thread::hardware_concurrency()); // 根据CPU核心数创建线程池
const int TOTAL_TASKS = 10000;
const int TASK_COMPLEXITY = 1000;
auto start = std::chrono::high_resolution_clock::now();
std::vector<std::future<void>> futures;
for (int i = 0; i < TOTAL_TASKS; ++i) {
futures.push_back(pool.submit(sample_task, TASK_COMPLEXITY));
}
// 等待所有任务完成
for (auto& future : futures) {
future.get();
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "线程池模式统计:" << std::endl;
std::cout << " 任务数量: " << TOTAL_TASKS << std::endl;
std::cout << " 线程数量: " << std::thread::hardware_concurrency() << std::endl;
std::cout << " 总耗时: " << duration.count() << "ms" << std::endl;
std::cout << " 吞吐量: " << (TOTAL_TASKS * 1000.0 / duration.count()) << " 任务/秒" << std::endl;
}
// 原始线程模式性能测试(作为对比基线)
void benchmark_raw_threads() {
const int TOTAL_TASKS = 10000; // 减少任务数,避免创建过多线程导致系统问题
const int TASK_COMPLEXITY = 1000;
auto start = std::chrono::high_resolution_clock::now();
std::vector<std::thread> threads;
std::vector<std::future<void>> futures;
for (int i = 0; i < TOTAL_TASKS; ++i) {
// 每个任务创建一个新线程(不推荐在生产环境中使用)
std::packaged_task<void()> task([=]() { sample_task(TASK_COMPLEXITY); });
futures.push_back(task.get_future());
threads.emplace_back(std::move(task));
}
// 等待所有线程完成
for (auto& thread : threads) {
if (thread.joinable()) {
thread.join();
}
}
// 检查future结果(确保任务完成)
for (auto& future : futures) {
future.get();
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "原始线程模式统计:" << std::endl;
std::cout << " 任务数量: " << TOTAL_TASKS << std::endl;
std::cout << " 线程数量: " << TOTAL_TASKS << " (每个任务一个线程)" << std::endl;
std::cout << " 总耗时: " << duration.count() << "ms" << std::endl;
std::cout << " 吞吐量: " << (TOTAL_TASKS * 1000.0 / duration.count()) << " 任务/秒" << std::endl;
}
// 高级性能指标收集
class AdvancedThreadPoolBenchmark {
public:
struct BenchmarkResult {
size_t total_tasks;
long long total_time_ms;
double tasks_per_second;
double avg_latency_us;
long long p95_latency_us;
long long p99_latency_us;
};
static BenchmarkResult advanced_benchmark() {
ThreadPool pool(std::thread::hardware_concurrency());
const size_t TOTAL_TASKS = 5000;
std::vector<long long> task_latencies; // 记录每个任务的延迟(微秒)
std::vector<std::future<long long>> futures;
auto start_time = std::chrono::high_resolution_clock::now();
// 提交任务并记录每个任务的开始时间
for (size_t i = 0; i < TOTAL_TASKS; ++i) {
auto task_start = std::chrono::high_resolution_clock::now();
futures.push_back(pool.submit([task_start]() -> long long {
// 模拟真实工作负载
sample_task(500);
auto end_time = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(
end_time - task_start).count();
}));
}
// 收集结果
for (auto& future : futures) {
task_latencies.push_back(future.get());
}
auto end_time = std::chrono::high_resolution_clock::now();
auto total_duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time - start_time);
// 计算统计指标
return calculate_metrics(TOTAL_TASKS, task_latencies, total_duration.count());
}
private:
static BenchmarkResult calculate_metrics(size_t total_tasks,
const std::vector<long long>& latencies,
long long total_time_ms) {
BenchmarkResult result;
result.total_tasks = total_tasks;
result.total_time_ms = total_time_ms;
result.tasks_per_second = total_tasks * 1000.0 / total_time_ms;
// 计算平均延迟
long long sum = 0;
for (auto latency : latencies) {
sum += latency;
}
result.avg_latency_us = static_cast<double>(sum) / latencies.size();
// 计算百分位延迟
auto sorted_latencies = latencies;
std::sort(sorted_latencies.begin(), sorted_latencies.end());
result.p95_latency_us = sorted_latencies[sorted_latencies.size() * 95 / 100];
result.p99_latency_us = sorted_latencies[sorted_latencies.size() * 99 / 100];
return result;
}
};
#endif