-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatomic_counter_test.cpp
355 lines (337 loc) · 12.1 KB
/
atomic_counter_test.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
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
#include <tr1/functional>
#include "unit_test.hpp"
#include "thread.hpp"
#include "atomic_counter.hpp"
#include "ticks_clock.hpp"
namespace{
using std::tr1::bind;
using base::makeThread;
using base::Atomic_Counter;
using base::TicksClock;
// const int REPEAT_TIME = 1000;
// 1 million
const int REPEAT_TIME = 1000000;
// const int MEGA_REPEAT_TIME = 10000000;
double getTotal(TicksClock::Ticks start,
TicksClock::Ticks end) {
double duration = end - start;
double time = duration / TicksClock::ticksPerSecond();
return time;
}
struct TestCombo{
int repeat_time;
double update_total;
double read_total;
TestCombo(int repeat):repeat_time(repeat), update_total(0),
read_total(0){}
};
// the abstract class for test
class AtomicTester{
public:
AtomicTester(Atomic_Counter * atomic_counter);
void start(TestCombo * tc_p);
void join();
virtual void test(TestCombo * tc_p) = 0;
protected:
Atomic_Counter * atomic_counter_;
pthread_t tid_;
};
AtomicTester::AtomicTester(Atomic_Counter * atomic_counter):
atomic_counter_(atomic_counter){}
void AtomicTester::start(TestCombo * tc_p){
tid_ = makeThread(std::tr1::bind(&AtomicTester::test, this, tc_p));
}
void AtomicTester::join(){
pthread_join(tid_,NULL);
}
// a test class for simple test
class AtomicTester_1 : public AtomicTester{
public:
AtomicTester_1(Atomic_Counter * atomic_counter) : AtomicTester(atomic_counter){}
void test(TestCombo * tc_p); // overwrite the pure vitural function
};
void AtomicTester_1::test(TestCombo * tc_p){
TicksClock::Ticks start = TicksClock::getTicks();
int counter = 0;
for(int i = 0 ; i < tc_p->repeat_time; i++){
counter = atomic_counter_->getAndIncrement();
// std::cout<<counter<<std::endl;
}
TicksClock::Ticks end = TicksClock::getTicks();
tc_p->update_total = getTotal(start, end);
}
class AtomicTester_2 : public AtomicTester{
public:
AtomicTester_2(Atomic_Counter * atomic_counter) : AtomicTester(atomic_counter){}
void test(TestCombo * tc_p); // overwrite the pure vitural function
};
void AtomicTester_2::test(TestCombo * tc_p){
TicksClock::Ticks start = TicksClock::getTicks();
for(int i = 0 ; i < tc_p->repeat_time; i++){
atomic_counter_->getResult();
}
TicksClock::Ticks end = TicksClock::getTicks();
tc_p->read_total = getTotal(start, end);
// std::cout << "read time inside is: " << tc_p->read_total << std::endl;
}
// helper class
class AtomicTestHelper{
public:
void runner(Atomic_Counter * atomic_counter,
int thread_num,
int repeat_time);
void runner2(Atomic_Counter * atomic_counter,
int thread_num,
int repeat_time);
void runner3(Atomic_Counter * atomic_counter,
int thread_num,
int repeat_time);
static AtomicTestHelper& getInstance(){
static AtomicTestHelper instance;
return instance;
}
private:
AtomicTestHelper(){}
AtomicTestHelper(AtomicTestHelper&);
AtomicTestHelper& operator=(AtomicTestHelper&);
};
void AtomicTestHelper::runner(Atomic_Counter * atomic_counter,
int thread_num,
int repeat_time)
{
double update_sum = 0;
AtomicTester_1 ** atomicTester = new AtomicTester_1 * [thread_num];
TestCombo **testCombo = new TestCombo *[thread_num];
for(int i = 0 ; i < thread_num; i++){
atomicTester[i] = new AtomicTester_1(atomic_counter);
testCombo[i] = new TestCombo(repeat_time);
}
for(int i = 0 ; i < thread_num; i++){
atomicTester[i]->start(testCombo[i]);
}
for(int i = 0 ; i < thread_num; i++){
atomicTester[i]->join();
}
for(int i = 0; i < thread_num; i++){
update_sum += testCombo[i]->update_total;
}
double update_avg = (update_sum / (thread_num*repeat_time)) * 1e9;
std::cout << "average update is: " << update_avg << std::endl;
for(int i = 0; i < thread_num; i++){
delete atomicTester[i];
delete testCombo[i];
}
delete [] atomicTester;
delete [] testCombo;
}
// single read multiple updates
void AtomicTestHelper::runner2(Atomic_Counter * atomic_counter,
int thread_num,
int repeat_time)
{
double update_sum = 0;
double read_sum = 0;
AtomicTester_1 ** at_update = new AtomicTester_1 * [thread_num];
AtomicTester_2 * at_read = new AtomicTester_2(atomic_counter);
TestCombo **testCombo = new TestCombo *[thread_num];
for(int i = 0 ; i < thread_num-1; i++){
at_update[i] = new AtomicTester_1(atomic_counter);
}
for(int i = 0; i < thread_num; i++){
testCombo[i] = new TestCombo(repeat_time);
}
for(int i = 0 ; i < thread_num-1; i++){
at_update[i]->start(testCombo[i]);
}
at_read->start(testCombo[thread_num-1]);
for(int i = 0 ; i < thread_num-1; i++){
at_update[i]->join();
}
at_read->join();
for(int i = 0; i < thread_num-1; i++){
update_sum += testCombo[i]->update_total;
}
read_sum += testCombo[thread_num-1]->read_total;
double update_avg = (update_sum / (repeat_time*(thread_num-1))) * 1e9;
double read_avg = (read_sum / repeat_time) * 1e9;
std::cout << "average update is: " << update_avg << std::endl;
std::cout << "average read is: " << read_avg << std::endl;
for(int i = 0; i < thread_num-1; i++){
delete at_update[i];
}
for(int i = 0; i < thread_num; i++){
delete testCombo[i];
}
delete at_read;
delete [] at_update;
delete [] testCombo;
}
// single update multiple read
void AtomicTestHelper::runner3(Atomic_Counter * atomic_counter,
int thread_num,
int repeat_time)
{
double update_sum = 0;
double read_sum = 0;
AtomicTester_1 * at_update = new AtomicTester_1(atomic_counter);
AtomicTester_2 ** at_read = new AtomicTester_2 *[thread_num-1];
TestCombo **testCombo = new TestCombo *[thread_num];
for(int i = 0 ; i < thread_num-1; i++){
at_read[i] = new AtomicTester_2(atomic_counter);
}
for(int i = 0; i < thread_num; i++){
testCombo[i] = new TestCombo(repeat_time);
}
at_update->start(testCombo[0]);
for(int i = 0 ; i < thread_num-1; i++){
at_read[i]->start(testCombo[i+1]);
}
at_update->join();
for(int i = 0 ; i < thread_num-1; i++){
at_read[i]->join();
}
for(int i = 0; i < thread_num-1; i++){
read_sum += testCombo[i+1]->read_total;
}
update_sum += testCombo[0]->update_total;
double read_avg = (read_sum / (repeat_time*(thread_num-1))) * 1e9;
double update_avg = (update_sum / repeat_time) * 1e9;
std::cout << "average update is: " << update_avg << "ns" << std::endl;
std::cout << "average read is: " << read_avg << "ns" << std::endl;
for(int i = 0; i < thread_num-1; i++){
delete at_read[i];
}
for(int i = 0; i < thread_num; i++){
delete testCombo[i];
}
delete at_update;
delete [] at_read;
delete [] testCombo;
}
// update test
TEST(Basics, Sequential){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner(&atomic_counter,1,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME);
}
TEST(Basics, Concurrency2){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner(&atomic_counter,2,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*2);
}
TEST(Basics, Concurrency4){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner(&atomic_counter,4,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*4);
}
TEST(Basics, Concurrency8){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner(&atomic_counter,8,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*8);
}
TEST(Basics, Concurrency16){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner(&atomic_counter,16,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*16);
}
TEST(Basics, Concurrency32){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner(&atomic_counter,32,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*32);
}
TEST(Basics, Concurrency64){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner(&atomic_counter,64,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*64);
}
TEST(Basics, Concurrency128){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner(&atomic_counter,128,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*128);
}
// single read multiple updates
TEST(SingleRead, Sequential){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner2(&atomic_counter,2,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME);
}
TEST(SingleRead, Concurrency2){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner2(&atomic_counter,3,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*2);
}
TEST(SingleRead, Concurrency4){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner2(&atomic_counter,5,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*4);
}
TEST(SingleRead, Concurrency8){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner2(&atomic_counter,9,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*8);
}
TEST(SingleRead, Concurrency16){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner2(&atomic_counter,17,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*16);
}
TEST(SingleRead, Concurrency32){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner2(&atomic_counter,33,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*32);
}
TEST(SingleRead, Concurrency64){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner2(&atomic_counter,65,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*64);
}
TEST(SingleRead, Concurrency128){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner2(&atomic_counter,129,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME*128);
}
// single update multiple reads
TEST(SingleUpdate, Sequential){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner3(&atomic_counter,2,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME);
}
TEST(SingleUpdate, Concurrency2){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner3(&atomic_counter,3,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME);
}
TEST(SingleUpdate, Concurrency4){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner3(&atomic_counter,5,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME);
}
TEST(SingleUpdate, Concurrency8){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner3(&atomic_counter,9,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME);
}
TEST(SingleUpdate, Concurrency16){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner3(&atomic_counter,17,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME);
}
TEST(SingleUpdate, Concurrency32){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner3(&atomic_counter,33,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME);
}
TEST(SingleUpdate, Concurrency64){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner3(&atomic_counter,65,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME);
}
TEST(SingleUpdate, Concurrency128){
Atomic_Counter atomic_counter;
AtomicTestHelper::getInstance().runner3(&atomic_counter,129,REPEAT_TIME);
EXPECT_EQ(atomic_counter.getResult(),REPEAT_TIME);
}
}// end of non-name namespace
int main(int argc, char *argv[]) {
return RUN_TESTS();
}