Repository navigation
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
358 lines (283 loc) · 10.6 KB
/
Copy pathbenchmark.cpp
File metadata and controls
358 lines (283 loc) · 10.6 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
#include <array>
#include <atomic>
#include <cstdint>
#include <deque>
#include <iostream>
#include <mutex>
#include <pthread.h>
#include <sched.h>
#include <thread>
#include <unordered_map>
#include <vector>
#include <x86intrin.h>
#include "FastHashMap.h"
#include "FastVector.h"
#include "mpmcQueue.h"
#include "spscQueue.cpp"
static constexpr size_t QUEUE_NUM_OPS = 10'000'000;
static constexpr size_t VECTOR_NUM_OPS = 100'000'000;
static constexpr size_t MAP_NUM_OPS = 10'000'000;
static constexpr size_t QUEUE_CAPACITY = 65536;
inline uint64_t rdtsc() {
unsigned int aux;
return __rdtscp(&aux);
}
template <typename T> inline void do_not_optimize(T &&value) {
asm volatile("" : : "g"(value) : "memory");
}
void set_thread_affinity_and_rt_priority(int core_id) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
sched_param param;
param.sched_priority = 99;
pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
}
namespace QueueBenchmarks {
void benchmark_std_deque() {
std::cout << "\n----------------------------------------\n";
std::cout << " Benchmarking std::deque + mutex (10M Ops) [Pinned Core 2 & 3]\n";
std::cout << "----------------------------------------\n";
std::deque<uint64_t> deq;
std::mutex mtx;
uint64_t start_cycles = rdtsc();
std::thread producer([&]() {
set_thread_affinity_and_rt_priority(2);
for (uint64_t i = 0; i < QUEUE_NUM_OPS; ++i) {
std::lock_guard<std::mutex> lock(mtx);
deq.push_back(i);
}
});
std::thread consumer([&]() {
set_thread_affinity_and_rt_priority(3);
uint64_t items_read = 0;
while (items_read < QUEUE_NUM_OPS) {
bool popped = false;
uint64_t val = 0;
{
std::lock_guard<std::mutex> lock(mtx);
if (!deq.empty()) {
val = deq.front();
deq.pop_front();
popped = true;
++items_read;
}
}
if (!popped) {
AETHON_PAUSE_CPU_INSTRUCTION;
}
}
});
producer.join();
consumer.join();
uint64_t end_cycles = rdtsc();
uint64_t total_cycles = end_cycles - start_cycles;
double avg_cycles_per_op = static_cast<double>(total_cycles) / QUEUE_NUM_OPS;
std::cout << "std::deque Total CPU Cycles: " << total_cycles << " cycles\n";
std::cout << "std::deque Avg Cycles / Op : " << avg_cycles_per_op << " cycles/op\n";
}
void benchmark_spsc() {
std::cout << "\n----------------------------------------\n";
std::cout << " Benchmarking SPSC Queue (10M Ops) [Pinned Core 2 & 3]\n";
std::cout << "----------------------------------------\n";
aethon::detail::ProducerConsumerQueue<uint64_t> queue(QUEUE_CAPACITY);
uint64_t start_cycles = rdtsc();
std::thread producer([&]() {
set_thread_affinity_and_rt_priority(2);
for (uint64_t i = 0; i < QUEUE_NUM_OPS; ++i) {
while (!queue.write(i)) {
AETHON_PAUSE_CPU_INSTRUCTION;
}
}
});
std::thread consumer([&]() {
set_thread_affinity_and_rt_priority(3);
uint64_t val = 0;
for (uint64_t i = 0; i < QUEUE_NUM_OPS; ++i) {
while (!queue.read(val)) {
AETHON_PAUSE_CPU_INSTRUCTION;
}
}
});
producer.join();
consumer.join();
uint64_t end_cycles = rdtsc();
uint64_t total_cycles = end_cycles - start_cycles;
double avg_cycles_per_op = static_cast<double>(total_cycles) / QUEUE_NUM_OPS;
std::cout << "SPSC Total CPU Cycles: " << total_cycles << " cycles\n";
std::cout << "SPSC Avg Cycles / Op : " << avg_cycles_per_op << " cycles/op\n";
}
void benchmark_mpmc() {
std::cout << "\n----------------------------------------\n";
std::cout << " Benchmarking MPMC Queue (10M Ops) [Pinned Core 2 & 3]\n";
std::cout << "----------------------------------------\n";
aethon::MpmcQueue<uint64_t> queue(QUEUE_CAPACITY);
uint64_t start_cycles = rdtsc();
std::thread producer([&]() {
set_thread_affinity_and_rt_priority(2);
for (uint64_t i = 0; i < QUEUE_NUM_OPS; ++i) {
queue.write(i);
}
});
std::thread consumer([&]() {
set_thread_affinity_and_rt_priority(3);
uint64_t val = 0;
for (uint64_t i = 0; i < QUEUE_NUM_OPS; ++i) {
queue.read(val);
}
});
producer.join();
consumer.join();
uint64_t end_cycles = rdtsc();
uint64_t total_cycles = end_cycles - start_cycles;
double avg_cycles_per_op = static_cast<double>(total_cycles) / QUEUE_NUM_OPS;
std::cout << "MPMC Total CPU Cycles: " << total_cycles << " cycles\n";
std::cout << "MPMC Avg Cycles / Op : " << avg_cycles_per_op << " cycles/op\n";
}
} // namespace QueueBenchmarks
namespace VectorBenchmarks {
void benchmark_std_array() {
std::cout << "\n----------------------------------------\n";
std::cout << " Benchmarking std::array (100M Short-Lived Arrays) [Pinned Core 2]\n";
std::cout << "----------------------------------------\n";
set_thread_affinity_and_rt_priority(2);
uint64_t start_cycles = rdtsc();
for (size_t i = 0; i < VECTOR_NUM_OPS; ++i) {
std::array<uint64_t, 4> arr;
arr[0] = i;
arr[1] = i + 1;
arr[2] = i + 2;
arr[3] = i + 3;
do_not_optimize(arr.data());
}
uint64_t end_cycles = rdtsc();
uint64_t total_cycles = end_cycles - start_cycles;
double avg_cycles_per_op = static_cast<double>(total_cycles) / VECTOR_NUM_OPS;
std::cout << "std::array Total Cycles: " << total_cycles << " cycles\n";
std::cout << "std::array Avg Cycles / Array (4 items): "
<< avg_cycles_per_op << " cycles/op\n";
}
void benchmark_std_vector() {
std::cout << "\n----------------------------------------\n";
std::cout << " Benchmarking std::vector (100M Short-Lived Vectors) [Pinned Core 2]\n";
std::cout << "----------------------------------------\n";
set_thread_affinity_and_rt_priority(2);
uint64_t start_cycles = rdtsc();
for (size_t i = 0; i < VECTOR_NUM_OPS; ++i) {
std::vector<uint64_t> vec;
vec.push_back(i);
vec.push_back(i + 1);
vec.push_back(i + 2);
vec.push_back(i + 3);
do_not_optimize(vec.data());
}
uint64_t end_cycles = rdtsc();
uint64_t total_cycles = end_cycles - start_cycles;
double avg_cycles_per_op = static_cast<double>(total_cycles) / VECTOR_NUM_OPS;
std::cout << "std::vector Total Cycles: " << total_cycles << " cycles\n";
std::cout << "std::vector Avg Cycles / Vector (4 items): "
<< avg_cycles_per_op << " cycles/op\n";
}
void benchmark_aethon_small_vector() {
std::cout << "\n----------------------------------------\n";
std::cout << " Benchmarking aethon::SmallVector (100M Short-Lived Vectors) [Pinned Core 2]\n";
std::cout << "----------------------------------------\n";
set_thread_affinity_and_rt_priority(2);
uint64_t start_cycles = rdtsc();
for (size_t i = 0; i < VECTOR_NUM_OPS; ++i) {
aethon::SmallVector<uint64_t, 4> vec;
vec.push_back(i);
vec.push_back(i + 1);
vec.push_back(i + 2);
vec.push_back(i + 3);
do_not_optimize(vec.data());
}
uint64_t end_cycles = rdtsc();
uint64_t total_cycles = end_cycles - start_cycles;
double avg_cycles_per_op = static_cast<double>(total_cycles) / VECTOR_NUM_OPS;
std::cout << "aethon::SmallVector Total Cycles: " << total_cycles << " cycles\n";
std::cout << "aethon::SmallVector Avg Cycles / Vector (4 items): "
<< avg_cycles_per_op << " cycles/op\n";
}
} // namespace VectorBenchmarks
namespace HashMapBenchmarks {
void benchmark_std_unordered_map() {
std::cout << "\n----------------------------------------\n";
std::cout << " Benchmarking std::unordered_map + mutex (10M Concurrent Ops, 4 Threads)\n";
std::cout << "----------------------------------------\n";
std::unordered_map<uint64_t, uint64_t> map;
std::mutex mtx;
constexpr size_t THREADS = 4;
constexpr size_t OPS_PER_THREAD = MAP_NUM_OPS / THREADS;
std::vector<std::thread> threads;
uint64_t start_cycles = rdtsc();
for (size_t t = 0; t < THREADS; ++t) {
threads.emplace_back([&, t]() {
set_thread_affinity_and_rt_priority(t % 4);
for (size_t i = 0; i < OPS_PER_THREAD; ++i) {
uint64_t key = (t * OPS_PER_THREAD) + i + 1;
std::lock_guard<std::mutex> lock(mtx);
map[key] = key * 10;
}
});
}
for (auto &th : threads) {
th.join();
}
uint64_t end_cycles = rdtsc();
uint64_t total_cycles = end_cycles - start_cycles;
double avg_cycles_per_op = static_cast<double>(total_cycles) / MAP_NUM_OPS;
std::cout << "std::unordered_map Total Cycles: " << total_cycles << " cycles\n";
std::cout << "std::unordered_map Avg Cycles / Insert: "
<< avg_cycles_per_op << " cycles/op\n";
}
void benchmark_aethon_lockfree_hashmap() {
std::cout << "\n----------------------------------------\n";
std::cout << " Benchmarking aethon::LockFreeHashMap (10M Concurrent Ops, 4 Threads)\n";
std::cout << "----------------------------------------\n";
aethon::LockFreeHashMap<uint64_t, uint64_t> map(16777216);
constexpr size_t THREADS = 4;
constexpr size_t OPS_PER_THREAD = MAP_NUM_OPS / THREADS;
std::vector<std::thread> threads;
uint64_t start_cycles = rdtsc();
for (size_t t = 0; t < THREADS; ++t) {
threads.emplace_back([&, t]() {
set_thread_affinity_and_rt_priority(t % 4);
for (size_t i = 0; i < OPS_PER_THREAD; ++i) {
uint64_t key = (t * OPS_PER_THREAD) + i + 1;
map.insert(key, key * 10);
}
});
}
for (auto &th : threads) {
th.join();
}
uint64_t end_cycles = rdtsc();
uint64_t total_cycles = end_cycles - start_cycles;
double avg_cycles_per_op = static_cast<double>(total_cycles) / MAP_NUM_OPS;
std::cout << "aethon::LockFreeHashMap Total Cycles: " << total_cycles << " cycles\n";
std::cout << "aethon::LockFreeHashMap Avg Cycles / Insert: "
<< avg_cycles_per_op << " cycles/op\n";
}
} // namespace HashMapBenchmarks
int main() {
std::cout << "=====================================================\n";
std::cout << " AETHON HIGH-PERFORMANCE SUITE RDTSC BENCHMARKS \n";
std::cout << "=====================================================\n";
// 1. Lock-Free Queue Benchmarks
QueueBenchmarks::benchmark_std_deque();
QueueBenchmarks::benchmark_spsc();
QueueBenchmarks::benchmark_mpmc();
// 2. Small Buffer Optimization Vector Benchmarks
VectorBenchmarks::benchmark_std_array();
VectorBenchmarks::benchmark_std_vector();
VectorBenchmarks::benchmark_aethon_small_vector();
// 3. Lock-Free Concurrent Hash Map Benchmarks
HashMapBenchmarks::benchmark_std_unordered_map();
HashMapBenchmarks::benchmark_aethon_lockfree_hashmap();
std::cout << "\n=====================================================\n";
std::cout << " ALL BENCHMARKS COMPLETED SUCCESSFULLY! \n";
std::cout << "=====================================================\n";
return 0;
}