-
Notifications
You must be signed in to change notification settings - Fork 1
/
LamportQueueTest.cpp
110 lines (99 loc) · 3.45 KB
/
LamportQueueTest.cpp
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
#include "LamportQueue2.hpp"
#include "LamportQueue3.hpp"
#include "LamportQueue4.hpp"
#include "LamportQueue5.hpp"
#include "LamportQueue6.hpp"
#include "LamportQueue7.hpp"
#include "LamportQueue8.hpp"
#include "LamportQueue9.hpp"
#include "BenchmarkSupport.hpp"
#include "Platform.hpp"
#include <new>
template<typename type>
static void LamportQueueTest(benchmark::State& state) {
static std::atomic<type*> queue = nullptr;
if (state.thread_index() == 0) {
queue.store(new type);
} else {
while (queue.load() == nullptr) {}
}
type& q = *queue;
if (state.thread_index() == 0) {
PREPARE_THREAD(Thread1Affinity);
for (auto _ : state) {
int counter = 10000;
while (counter > 0) {
counter -= int(q.Enqueue());
}
}
} else if (state.thread_index() == 1) {
PREPARE_THREAD(Thread2Affinity);
for (auto _ : state) {
int counter = 10000;
while (counter > 0) {
counter -= int(q.Dequeue([](typename type::value_type&&) {}));
}
}
delete queue.load();
queue.store(nullptr);
}
state.SetItemsProcessed(state.iterations() * 10000);
state.SetBytesProcessed(state.iterations() * 10000 * sizeof(typename type::value_type));
}
template<typename type>
static void LamportQueueLatencyTest(benchmark::State& state) {
static std::atomic<type*> queue1 = nullptr;
static std::atomic<type*> queue2 = nullptr;
if (state.thread_index() == 0) {
queue1.store(new type);
queue2.store(new type);
} else {
while (queue1.load() == nullptr) {}
while (queue2.load() == nullptr) {}
}
type& q1 = *queue1;
type& q2 = *queue2;
if (state.thread_index() == 0) {
PREPARE_THREAD(Thread1Affinity);
for (auto _ : state) {
int counter = 10000;
while (counter > 0) {
q1.Enqueue();
while (q2.Dequeue([](typename type::value_type&&) {}) != 1) {}
counter -= 1;
}
}
delete queue1.load();
delete queue2.load();
queue1.store(nullptr);
queue2.store(nullptr);
} else if (state.thread_index() == 1) {
PREPARE_THREAD(Thread2Affinity);
for (auto _ : state) {
int counter = 10000;
while (counter > 0) {
counter -= int(q1.Dequeue(
[&q2](typename type::value_type&& e) {
q2.Enqueue(std::move(e));
}
));
}
}
}
}
QUEUE_BENCH(LamportQueueTest, LamportQueue2);
QUEUE_BENCH(LamportQueueTest, LamportQueue3);
QUEUE_BENCH(LamportQueueTest, LamportQueue4);
QUEUE_BENCH(LamportQueueTest, LamportQueue5);
QUEUE_BENCH(LamportQueueTest, LamportQueue6);
QUEUE_BENCH(LamportQueueTest, LamportQueue7);
QUEUE_BENCH(LamportQueueTest, LamportQueue8);
QUEUE_BENCH(LamportQueueTest, LamportQueue9);
QUEUE_BENCH_FOR_SIZE(LamportQueueLatencyTest, LamportQueue2, 8);
QUEUE_BENCH_FOR_SIZE(LamportQueueLatencyTest, LamportQueue3, 8);
QUEUE_BENCH_FOR_SIZE(LamportQueueLatencyTest, LamportQueue4, 8);
QUEUE_BENCH_FOR_SIZE(LamportQueueLatencyTest, LamportQueue5, 8);
QUEUE_BENCH_FOR_SIZE(LamportQueueLatencyTest, LamportQueue6, 8);
QUEUE_BENCH_FOR_SIZE(LamportQueueLatencyTest, LamportQueue7, 8);
QUEUE_BENCH_FOR_SIZE(LamportQueueLatencyTest, LamportQueue8, 8);
QUEUE_BENCH_FOR_SIZE(LamportQueueLatencyTest, LamportQueue9, 8);