-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
388 lines (317 loc) · 14.1 KB
/
main.cpp
File metadata and controls
388 lines (317 loc) · 14.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "dictionary_codec.h"
#include <iostream>
#include <fstream>
#include <chrono>
#include <random>
#include <thread>
#include <iomanip>
#include <filesystem>
#include <numeric>
#include <algorithm>
namespace chr = std::chrono;
struct EncodingResult {
int threads;
double duration_ms;
double throughput_mbs;
size_t dictionary_size;
};
struct SearchStats {
double min_latency_us;
double max_latency_us;
double avg_latency_us;
double median_latency_us;
double p95_latency_us;
double throughput_qps;
size_t total_matches;
};
struct SearchResult {
std::string method;
SearchStats stats;
};
struct PrefixResult {
std::string method;
size_t prefix_length;
SearchStats stats;
};
SearchStats calculateStats(std::vector<double>& times, size_t total_matches) {
if (times.empty()) return SearchStats{0, 0, 0, 0, 0, 0, total_matches};
std::sort(times.begin(), times.end());
size_t size = times.size();
double min = times.front();
double max = times.back();
double sum = std::accumulate(times.begin(), times.end(), 0.0);
double avg = sum / size;
double median = size % 2 == 0 ?
(times[size/2 - 1] + times[size/2]) / 2 :
times[size/2];
double p95 = times[static_cast<size_t>(size * 0.95)];
double throughput = (size * 1000000.0) / sum;
return SearchStats{
min,
max,
avg,
median,
p95,
throughput,
total_matches
};
}
void writeResults(const std::string& output_dir,
const std::vector<EncodingResult>& encoding_results,
const std::vector<SearchResult>& search_results,
const std::vector<PrefixResult>& prefix_results) {
std::filesystem::create_directories(output_dir);
// Write encoding results
std::ofstream encoding_file(output_dir + "/encoding_results.csv");
encoding_file << "Threads,Duration_ms,Throughput_MBps,DictionarySize\n";
for (const auto& result : encoding_results) {
encoding_file << result.threads << ","
<< result.duration_ms << ","
<< result.throughput_mbs << ","
<< result.dictionary_size << "\n";
}
// Write search results with detailed stats
std::ofstream search_file(output_dir + "/search_results.csv");
search_file << "Method,MinLatency_us,MaxLatency_us,AvgLatency_us,MedianLatency_us,P95Latency_us,Throughput_QPS,TotalMatches\n";
for (const auto& result : search_results) {
search_file << result.method << ","
<< result.stats.min_latency_us << ","
<< result.stats.max_latency_us << ","
<< result.stats.avg_latency_us << ","
<< result.stats.median_latency_us << ","
<< result.stats.p95_latency_us << ","
<< result.stats.throughput_qps << ","
<< result.stats.total_matches << "\n";
}
// Write prefix search results with detailed stats
std::ofstream prefix_file(output_dir + "/prefix_results.csv");
prefix_file << "Method,PrefixLength,MinLatency_us,MaxLatency_us,AvgLatency_us,MedianLatency_us,P95Latency_us,Throughput_QPS,TotalMatches\n";
for (const auto& result : prefix_results) {
prefix_file << result.method << ","
<< result.prefix_length << ","
<< result.stats.min_latency_us << ","
<< result.stats.max_latency_us << ","
<< result.stats.avg_latency_us << ","
<< result.stats.median_latency_us << ","
<< result.stats.p95_latency_us << ","
<< result.stats.throughput_qps << ","
<< result.stats.total_matches << "\n";
}
}
void validateFile(const std::string& filename) {
std::ifstream file(filename);
if (!file) {
throw std::runtime_error("Cannot open file: " + filename);
}
std::cout << "File opened successfully\n";
std::string line;
for (int i = 0; i < 5 && std::getline(file, line); i++) {
std::cout << "Sample line " << (i+1) << ": " << line << "\n";
}
}
int main(int argc, char* argv[]) {
try {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <input_file>\n";
return 1;
}
const std::string input_filename = argv[1];
std::string output_dir = "benchmark_results_" +
std::filesystem::path(input_filename).stem().string();
std::cout << "Dictionary Codec Performance Analysis\n";
std::cout << "===================================\n\n";
validateFile(input_filename);
DictionaryCodec codec;
std::vector<EncodingResult> encoding_results;
std::vector<SearchResult> search_results;
std::vector<PrefixResult> prefix_results;
// Part 1: Multi-threaded Encoding Performance
std::cout << "\n1. Dictionary Encoding Performance with Different Thread Counts\n";
std::cout << "--------------------------------------------------------\n";
std::vector<int> thread_counts = {1, 2, 4, 8};
for (int threads : thread_counts) {
std::cout << "\nTesting with " << threads << " threads...\n";
auto start = chr::steady_clock::now();
codec.encodeFile(input_filename, threads);
auto end = chr::steady_clock::now();
auto duration = chr::duration_cast<chr::milliseconds>(end - start).count();
size_t file_size = std::ifstream(input_filename, std::ios::ate | std::ios::binary).tellg();
double throughput = (file_size / 1024.0 / 1024.0) / (duration / 1000.0);
encoding_results.push_back({
threads,
static_cast<double>(duration),
throughput,
codec.getDictionarySize()
});
std::cout << "Time: " << duration << "ms\n";
std::cout << "Throughput: " << throughput << " MB/s\n";
std::cout << "Dictionary size: " << codec.getDictionarySize() << " entries\n";
}
// Part 2: Generate test queries
const auto& reverse_dict = codec.getReverseDictionary();
std::vector<std::string> test_queries;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, reverse_dict.size() - 1);
const int NUM_QUERIES = 100; // Adjust for choosing between testing time and accurarcy. More Accuracy = More time.
for (int i = 0; i < NUM_QUERIES; i++) {
test_queries.push_back(reverse_dict[dis(gen)]);
}
// Single Item Search Tests
std::cout << "\n2. Single Item Search Performance\n";
std::cout << "--------------------------------\n";
// Warmup phase
std::cout << "Warming up...\n";
for (int i = 0; i < 3; i++) {
for (const auto& query : test_queries) {
codec.baselineFind(query);
codec.findMatches(query);
codec.findMatchesSIMD(query);
}
}
// Vanilla search
std::cout << "Running vanilla search benchmark...\n";
size_t total_matches = 0;
std::vector<double> vanilla_times;
for (const auto& query : test_queries) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
auto start = chr::steady_clock::now();
auto results = codec.baselineFind(query);
auto end = chr::steady_clock::now();
auto duration = chr::duration_cast<chr::microseconds>(end - start).count();
total_matches += results.size();
if (duration > 0) {
vanilla_times.push_back(duration);
}
}
if (!vanilla_times.empty()) {
search_results.push_back({"Vanilla", calculateStats(vanilla_times, total_matches)});
}
// Dictionary search without SIMD
std::cout << "\nRunning dictionary search benchmark...\n";
total_matches = 0;
std::vector<double> dict_times;
for (const auto& query : test_queries) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
auto start = chr::steady_clock::now();
auto results = codec.findMatches(query);
auto end = chr::steady_clock::now();
auto duration = chr::duration_cast<chr::microseconds>(end - start).count();
total_matches += results.size();
if (duration > 0) {
dict_times.push_back(duration);
}
}
if (!dict_times.empty()) {
search_results.push_back({"Dictionary", calculateStats(dict_times, total_matches)});
}
// Dictionary search with SIMD
std::cout << "\nRunning SIMD search benchmark...\n";
total_matches = 0;
std::vector<double> simd_times;
for (const auto& query : test_queries) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
auto start = chr::steady_clock::now();
auto results = codec.findMatchesSIMD(query);
auto end = chr::steady_clock::now();
auto duration = chr::duration_cast<chr::microseconds>(end - start).count();
total_matches += results.size();
if (duration > 0) {
simd_times.push_back(duration);
}
}
if (!simd_times.empty()) {
search_results.push_back({"SIMD", calculateStats(simd_times, total_matches)});
}
// Prefix Search Tests
std::cout << "\n3. Prefix Search Performance\n";
std::cout << "---------------------------\n";
std::vector<size_t> prefix_lengths = {2, 4, 8};
for (size_t prefix_len : prefix_lengths) {
std::vector<std::string> prefix_queries;
for (int i = 0; i < NUM_QUERIES; i++) {
std::string str = reverse_dict[dis(gen)];
if (str.length() > prefix_len) {
prefix_queries.push_back(str.substr(0, prefix_len));
}
}
if (prefix_queries.empty()) continue;
std::cout << "\nPrefix length " << prefix_len << ":\n";
// Warmup
std::cout << "Warming up...\n";
for (const auto& prefix : prefix_queries) {
codec.baselinePrefixSearch(prefix);
codec.prefixSearchSIMD(prefix);
}
// Baseline prefix search
size_t progress = 0;
total_matches = 0;
std::vector<double> vanilla_prefix_times;
std::cout << "Running baseline prefix search...\n";
for (const auto& prefix : prefix_queries) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
auto start = chr::steady_clock::now();
auto results = codec.baselinePrefixSearch(prefix);
auto end = chr::steady_clock::now();
auto duration = chr::duration_cast<chr::microseconds>(end - start).count();
for (const auto& match : results) {
total_matches += match.second.size();
}
if (duration > 0) {
vanilla_prefix_times.push_back(duration);
}
// Update progress
progress++;
std::cout << "\rProgress: " << progress << "/" << prefix_queries.size()
<< " queries (" << (progress * 100.0 / prefix_queries.size())
<< "%)" << std::flush;
}
if (!vanilla_prefix_times.empty()) {
prefix_results.push_back({
"Vanilla",
prefix_len,
calculateStats(vanilla_prefix_times, total_matches)
});
}
std::cout << "\nCompleted with " << total_matches << " total matches\n";
// SIMD prefix search
progress = 0;
total_matches = 0;
std::vector<double> simd_prefix_times;
std::cout << "\nRunning SIMD prefix search...\n";
for (const auto& prefix : prefix_queries) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
auto start = chr::steady_clock::now();
auto results = codec.prefixSearchSIMD(prefix);
auto end = chr::steady_clock::now();
auto duration = chr::duration_cast<chr::microseconds>(end - start).count();
for (const auto& match : results) {
total_matches += match.second.size();
}
if (duration > 0) {
simd_prefix_times.push_back(duration);
}
// Update progress
progress++;
std::cout << "\rProgress: " << progress << "/" << prefix_queries.size()
<< " queries (" << (progress * 100.0 / prefix_queries.size())
<< "%)" << std::flush;
}
if (!simd_prefix_times.empty()) {
prefix_results.push_back({
"SIMD",
prefix_len,
calculateStats(simd_prefix_times, total_matches)
});
}
std::cout << "\nCompleted with " << total_matches << " total matches\n";
}
// Write results to files
writeResults(output_dir, encoding_results, search_results, prefix_results);
std::cout << "\nResults have been written to: " << output_dir << "\n";
return 0;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}