-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_FeatureExtractor.cpp
More file actions
235 lines (193 loc) · 8.53 KB
/
test_FeatureExtractor.cpp
File metadata and controls
235 lines (193 loc) · 8.53 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
#include "FeatureExtractor.h"
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <numeric>
// 测试提取器的基本功能
void test_basic_functionality(const std::string &filepath) {
std::cout << "===== 测试基本功能 =====" << std::endl;
try {
// 创建特征提取器,限制处理10个数据包
FeatureExtractor fe(filepath, 10);
// 获取特征数量
size_t num_features = fe.get_num_features();
std::cout << "特征数量: " << num_features << std::endl;
// 处理前5个数据包
for (int i = 0; i < 5; ++i) {
std::vector<double> features = fe.get_next_vector();
if (features.empty()) {
std::cout << "数据包 #" << i << ": 无特征" << std::endl;
continue;
}
std::cout << "数据包 #" << i << ": 提取了 " << features.size()
<< " 个特征" << std::endl;
// 输出前5个特征值
std::cout << " 前5个特征值: ";
for (size_t j = 0; j < std::min(size_t(5), features.size()); ++j) {
std::cout << features[j] << " ";
}
std::cout << std::endl;
}
std::cout << "基本功能测试完成!" << std::endl;
} catch (const std::exception &e) {
std::cerr << "测试失败: " << e.what() << std::endl;
}
}
// 测试性能
void benchmark_performance(const std::string &filepath, int num_packets) {
std::cout << "\n===== 性能测试 =====" << std::endl;
try {
// 创建特征提取器
auto start_time = std::chrono::high_resolution_clock::now();
FeatureExtractor fe(filepath, num_packets);
auto end_time = std::chrono::high_resolution_clock::now();
auto init_duration =
std::chrono::duration_cast<std::chrono::milliseconds>(end_time -
start_time)
.count();
std::cout << "初始化时间: " << init_duration << " ms" << std::endl;
// 处理所有数据包并测量时间
start_time = std::chrono::high_resolution_clock::now();
std::vector<double> processing_times;
int processed_packets = 0;
int feature_dimensions = 0;
for (int i = 0; i < num_packets; ++i) {
auto packet_start = std::chrono::high_resolution_clock::now();
std::vector<double> features = fe.get_next_vector();
auto packet_end = std::chrono::high_resolution_clock::now();
if (features.empty()) {
break;
}
double packet_time =
std::chrono::duration_cast<std::chrono::microseconds>(
packet_end - packet_start)
.count() /
1000.0; // 转换为毫秒
processing_times.push_back(packet_time);
feature_dimensions = features.size();
processed_packets++;
}
end_time = std::chrono::high_resolution_clock::now();
auto total_duration =
std::chrono::duration_cast<std::chrono::milliseconds>(end_time -
start_time)
.count();
// 计算统计信息
double avg_time =
std::accumulate(processing_times.begin(), processing_times.end(),
0.0) /
(processing_times.empty() ? 1 : processing_times.size());
double min_time = processing_times.empty()
? 0
: *std::min_element(processing_times.begin(),
processing_times.end());
double max_time = processing_times.empty()
? 0
: *std::max_element(processing_times.begin(),
processing_times.end());
std::cout << "处理了 " << processed_packets << " 个数据包" << std::endl;
std::cout << "每个特征向量维度: " << feature_dimensions << std::endl;
std::cout << "总处理时间: " << total_duration << " ms" << std::endl;
std::cout << "平均每个数据包处理时间: " << avg_time << " ms"
<< std::endl;
std::cout << "最小处理时间: " << min_time << " ms" << std::endl;
std::cout << "最大处理时间: " << max_time << " ms" << std::endl;
std::cout << "每秒处理数据包数: "
<< (1000.0 * processed_packets / total_duration) << std::endl;
std::cout << "性能测试完成!" << std::endl;
} catch (const std::exception &e) {
std::cerr << "测试失败: " << e.what() << std::endl;
}
}
// 比较Python版本和C++版本的性能
void compare_with_python(const std::string &filepath,
const std::string &python_results_file) {
std::cout << "\n===== 与Python版本比较 =====" << std::endl;
try {
// 读取Python版本的性能结果
std::ifstream python_file(python_results_file);
if (!python_file.is_open()) {
std::cerr << "无法打开Python结果文件: " << python_results_file
<< std::endl;
return;
}
double python_init_time, python_total_time, python_avg_time;
int python_processed_packets;
python_file >> python_init_time >> python_total_time >>
python_avg_time >> python_processed_packets;
python_file.close();
// 测量C++版本的性能
auto start_time = std::chrono::high_resolution_clock::now();
FeatureExtractor fe(filepath, python_processed_packets);
auto end_time = std::chrono::high_resolution_clock::now();
auto init_duration =
std::chrono::duration_cast<std::chrono::milliseconds>(end_time -
start_time)
.count();
start_time = std::chrono::high_resolution_clock::now();
int processed_packets = 0;
for (int i = 0; i < python_processed_packets; ++i) {
std::vector<double> features = fe.get_next_vector();
if (features.empty()) {
break;
}
processed_packets++;
}
end_time = std::chrono::high_resolution_clock::now();
auto total_duration =
std::chrono::duration_cast<std::chrono::milliseconds>(end_time -
start_time)
.count();
double avg_time =
static_cast<double>(total_duration) / processed_packets;
// 输出比较结果
std::cout << "Python版本:" << std::endl;
std::cout << " 初始化时间: " << python_init_time << " ms" << std::endl;
std::cout << " 总处理时间: " << python_total_time << " ms"
<< std::endl;
std::cout << " 平均每个数据包处理时间: " << python_avg_time << " ms"
<< std::endl;
std::cout << " 处理了 " << python_processed_packets << " 个数据包"
<< std::endl;
std::cout << "C++版本:" << std::endl;
std::cout << " 初始化时间: " << init_duration << " ms" << std::endl;
std::cout << " 总处理时间: " << total_duration << " ms" << std::endl;
std::cout << " 平均每个数据包处理时间: " << avg_time << " ms"
<< std::endl;
std::cout << " 处理了 " << processed_packets << " 个数据包"
<< std::endl;
// 计算加速比
double init_speedup = python_init_time / init_duration;
double total_speedup = python_total_time / total_duration;
double avg_speedup = python_avg_time / avg_time;
std::cout << "加速比:" << std::endl;
std::cout << " 初始化时间加速比: " << init_speedup << "x" << std::endl;
std::cout << " 总处理时间加速比: " << total_speedup << "x"
<< std::endl;
std::cout << " 平均处理时间加速比: " << avg_speedup << "x"
<< std::endl;
std::cout << "比较完成!" << std::endl;
} catch (const std::exception &e) {
std::cerr << "比较失败: " << e.what() << std::endl;
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "用法: " << argv[0]
<< " <pcap/tsv文件路径> [python结果文件]" << std::endl;
return 1;
}
std::string filepath = argv[1];
// 测试基本功能
test_basic_functionality(filepath);
// 测试性能
benchmark_performance(filepath, 10000);
// 如果提供了Python结果文件,进行比较
if (argc >= 3) {
std::string python_results_file = argv[2];
compare_with_python(filepath, python_results_file);
}
return 0;
}