-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcache_test.cpp
165 lines (135 loc) · 3.22 KB
/
cache_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
// Copyright (C) 2021-
#define ZEUS_LIBRARY_DEBUG
#include "cache.hpp"
#include <iostream>
#define PASSED 0
#define N 10 << 20
using namespace zeus;
using namespace std;
struct tester {
atomic_ulong& cnt;
tester(atomic<ulong>& c) : cnt(c) { cnt++; }
~tester() { cnt--; }
};
int test_cache_case1(zeus::Pool<tester, 16>&);
int test_cache_case2(zeus::Pool<tester, 16>&);
int test_cache_case3(zeus::Pool<tester, 16>&);
int test_cache_case4(zeus::Pool<tester, 16>&);
using Case = int (*)(zeus::Pool<tester, 16>&);
/**
* zeus::pool unit test.
*/
int main() {
zeus::Pool<tester, 16> pool{};
Case cases[4]
{
test_cache_case1,
test_cache_case2,
test_cache_case3,
test_cache_case4
};
char const* hdr = "zeus::pool test case #";
for (int i = 0; i < 4; i++) {
int ret = cases[i](pool);
printf(
"=== RUN %s%d "
"--------------"
"-------------- %s\n",
hdr, i + 1, ret == PASSED ? "passed" : "fail");
}
return 0;
}
int test_cache_case1(zeus::Pool<tester, 16>& pool) {
atomic_ulong cnt = 0;
vector<thread> ths{};
atomic_bool flag = false;
auto [Vcnt, Ccnt] = decay_t<decltype(pool)>::ZeusUnitMonitor();
for (uint i = 0; i < 8; i++) {
ths.emplace_back(thread([&] {
auto* p = pool.Get(cnt);
flag.wait(false);
pool.Put(p);
}));
}
flag = true;
flag.notify_all();
for (auto& th : ths) {
th.join();
}
return cnt.load() || Vcnt.load() || Ccnt.load();
}
// one to one
int test_cache_case2(zeus::Pool<tester, 16>& pool) {
auto [Vcnt, Ccnt] = decay_t<decltype(pool)>::ZeusUnitMonitor();
atomic_ulong cnt{};
atomic_bool flag{};
thread th1([&] {
for (ulong i = 0; i < N; i++) {
pool.Put(new tester{cnt});
}
flag = true;
});
for (;;) {
auto* p = pool.Get(cnt);
if (!p && !flag) continue;
if (p) {
delete p;
} else {
break;
}
}
th1.join();
return cnt.load() || Vcnt.load() || Ccnt.load();
}
// many to one
int test_cache_case3(zeus::Pool<tester, 16>& pool) {
atomic_ulong cnt = 0;
atomic_uint cnt2 = 0;
vector<thread> ths{};
auto [Vcnt, Ccnt] = decay_t<decltype(pool)>::ZeusUnitMonitor();
for (uint i = 0; i < 4; i++) {
ths.emplace_back(thread([&] {
for (ulong j = 0; j < N / 4; j++) {
pool.Put(new tester(cnt));
}
cnt2++;
}));
}
for (;;) {
auto* p = pool.Get(cnt);
if (p) delete p;
if (cnt2 == 4) break;
}
for (auto& th : ths) {
th.join();
}
return cnt.load() || Vcnt.load() || Ccnt.load();
}
// many to many
int test_cache_case4(zeus::Pool<tester, 16>& pool) {
atomic_ulong cnt = 0;
atomic_uint cnt2 = 0;
vector<thread> ths{};
auto [Vcnt, Ccnt] = decay_t<decltype(pool)>::ZeusUnitMonitor();
for (uint i = 0; i < 8; i++) {
ths.emplace_back(thread([&] {
for (ulong j = 0; j < N / 8; j++) {
pool.Put(new tester(cnt));
}
cnt2++;
}));
}
for (uint i = 0; i < 8; i++) {
ths.emplace_back(thread([&] {
for (;;) {
auto* p = pool.Get(cnt);
if (p) delete p;
if (cnt2 == 8) break;
}
}));
}
for (auto& th : ths) {
th.join();
}
return cnt.load() || Vcnt.load() || Ccnt.load();
}