-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.cc
187 lines (157 loc) · 4.73 KB
/
test.cc
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
#include <chrono>
#include <iostream>
#include <memory>
#include <thread>
#include <unordered_map>
#include <vector>
#include "lockfree_queue.h"
const int kMaxThreads = std::thread::hardware_concurrency();
int maxElements;
LockFreeQueue<int> q;
// Insert sucessfully then ++cnt, delete succesfully then --cnt.
std::atomic<int> cnt = 0;
std::atomic<bool> start = false;
std::unordered_map<int, int*> elements2timespan;
const int kElements1 = 10000;
const int kElements2 = 100000;
const int kElements3 = 1000000;
void onInsert(int divide) {
while (!start) {
std::this_thread::yield();
}
int n = maxElements / divide;
for (int i = 0; i < n; ++i) {
q.Enqueue(i);
++cnt;
}
}
void onDelete(int divide) {
while (!start) {
std::this_thread::yield();
}
int n = maxElements / divide;
for (int i = 0; i < n; ++i) {
int x;
if (q.Dequeue(x)) {
--cnt;
}
}
}
void TestConcurrentInsert() {
int old_size = q.size();
std::vector<std::thread> threads;
for (int i = 0; i < kMaxThreads; ++i) {
threads.push_back(std::thread(onInsert, kMaxThreads));
}
start = true;
auto t1_ = std::chrono::steady_clock::now();
for (int i = 0; i < kMaxThreads; ++i) {
threads[i].join();
}
auto t2_ = std::chrono::steady_clock::now();
assert(cnt + old_size == static_cast<int>(q.size()));
int ms =
std::chrono::duration_cast<std::chrono::milliseconds>(t2_ - t1_).count();
elements2timespan[maxElements][0] += ms;
std::cout << maxElements << " elements dequeue concurrently, timespan=" << ms
<< "ms"
<< "\n";
start = false;
}
void TestConcurrentDelete() {
int old_size = q.size();
std::vector<std::thread> threads;
for (int i = 0; i < kMaxThreads; ++i) {
threads.push_back(std::thread(onDelete, kMaxThreads));
}
cnt = 0;
start = true;
auto t1_ = std::chrono::steady_clock::now();
for (int i = 0; i < kMaxThreads; ++i) {
threads[i].join();
}
auto t2_ = std::chrono::steady_clock::now();
assert(cnt + old_size == static_cast<int>(q.size()));
int ms =
std::chrono::duration_cast<std::chrono::milliseconds>(t2_ - t1_).count();
elements2timespan[maxElements][1] += ms;
std::cout << maxElements << " elements dequeue concurrently, timespan=" << ms
<< "ms"
<< "\n";
cnt = 0;
start = false;
}
void TestConcurrentInsertAndDequeue() {
int old_size = q.size();
int divide = kMaxThreads / 2;
std::vector<std::thread> insert_threads;
for (int i = 0; i < divide; ++i) {
insert_threads.push_back(std::thread(onInsert, divide));
}
std::vector<std::thread> delete_threads;
for (int i = 0; i < divide; ++i) {
delete_threads.push_back(std::thread(onDelete, divide));
}
cnt = 0;
start = true;
auto t1_ = std::chrono::steady_clock::now();
for (int i = 0; i < divide; ++i) {
insert_threads[i].join();
}
for (int i = 0; i < divide; ++i) {
delete_threads[i].join();
}
auto t2_ = std::chrono::steady_clock::now();
assert(cnt + old_size == static_cast<int>(q.size()));
int ms =
std::chrono::duration_cast<std::chrono::milliseconds>(t2_ - t1_).count();
elements2timespan[maxElements][2] += ms;
std::cout << maxElements
<< " elements dequeue and delete concurrently, timespan=" << ms
<< "ms"
<< "\n";
cnt = 0;
start = false;
}
int main(int argc, char const* argv[]) {
(void)argc;
(void)argv;
std::cout << "Benchmark with " << kMaxThreads << " threads:"
<< "\n";
int elements[] = {kElements1, kElements2, kElements3};
int timespan1[] = {0, 0, 0};
int timespan2[] = {0, 0, 0};
int timespan3[] = {0, 0, 0};
elements2timespan[kElements1] = timespan1;
elements2timespan[kElements2] = timespan2;
elements2timespan[kElements3] = timespan3;
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 3; ++j) {
maxElements = elements[j];
TestConcurrentInsert();
TestConcurrentDelete();
TestConcurrentInsertAndDequeue();
std::cout << "\n";
}
}
for (int i = 0; i < 3; ++i) {
maxElements = elements[i];
float avg = static_cast<float>(elements2timespan[maxElements][0]) / 10.0f;
std::cout << maxElements
<< " elements dequeue concurrently, average timespan=" << avg
<< "ms"
<< "\n";
avg = static_cast<float>(elements2timespan[maxElements][1]) / 10.0f;
std::cout << maxElements
<< " elements dequeue concurrently, average timespan=" << avg
<< "ms"
<< "\n";
avg = static_cast<float>(elements2timespan[maxElements][2]) / 10.0f;
std::cout << maxElements
<< " elements dequeue and dequeue concurrently, average timespan="
<< avg << "ms"
<< "\n";
std::cout << "\n";
}
return 0;
}