-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
281 lines (252 loc) · 6.83 KB
/
Copy pathtest.cpp
File metadata and controls
281 lines (252 loc) · 6.83 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
#include "test.h"
void test::testHotDataAccess()
{
const size_t CAPACITY = 20;//缓存容量
const int OPERATIONS = 500000;//总操作数
init(CAPACITY,OPERATIONS);//初始化
const int HOT_KEYS = 20;//热点数据数量
const int COLD_KEYS = 5000;//冷数据数量
//用于生成随机数
std::random_device rd;//随机数种子
std::mt19937 gen(rd());
std::thread animation_thread(&test::loading_animation,this);//创建线程来执行loading_animation这个函数
is_running = true;
//给缓存系统预热
for (int i = 0; i < cacheNum; ++i)
{
//先预热缓存,插入一定的数据
for (int key = 0; key < HOT_KEYS; ++key)
{
std::string value = "value" + std::to_string(key);
cache[i]->put(key, value);
}
for (int op = 0; op < OPERATIONS; op++)
{
//考虑:读还是写,热值还是冷值
//交替进行put和get,模拟真实场景
//真实场景中读操作会比写操作更多,因此70%进行读,30%进行写
bool isPut = gen() % 100 < 70 ? false : true;
//70%热值,30%冷值
int key = (gen() % 100 < 70) ? (gen() % HOT_KEYS) : (HOT_KEYS + gen() % COLD_KEYS);
if (isPut)
{
std::string value = "value" + std::to_string(key) + "_v" + std::to_string(op % 100);
cache[i]->put(key, value);
}
else
{
std::string result;
get_operations[i]++;
if (cache[i]->get(key, result))
{
hits[i]++;
}
}
}
}
is_running = false;
animation_thread.join();
print("热点数据访问测试");
clear();//清理测试时使用的容器s
}
void test::testLoopPattern()
{
const size_t CAPACITY = 50;
const int LOOP_SIZE = 500;//循环范围大小
const int OPERATIONS = 200000;//总操作数
init(CAPACITY, OPERATIONS);
//生成随机数
std::random_device rd;//随机数种子
std::mt19937 gen(rd());//gen()即为随机数
std::thread animation_thread(&test::loading_animation, this);
is_running = true;
for (int i = 0; i < cacheNum; ++i)
{
//预热缓存,先加载20%的数据
for (int k = 0; k < LOOP_SIZE / 5; ++k)
{
std::string v = "loop" + std::to_string(k);
cache[i]->put(k, v);
}
int LOOP_pos = 0;//设置循环扫描的起始位置
for(int j=0;j<OPERATIONS;++j)
{
//80%读 20%写
bool isPut = (gen() % 100 < 20);
int key;
//对于数据,依次的按照不同模式选择键
if (j % 100 < 60)//60%顺序扫描
{
key = LOOP_pos;
LOOP_pos = (LOOP_pos + 1) % LOOP_SIZE;
}
else if (j % 100 < 90)//30%随机跳跃
{
key = gen() % LOOP_SIZE;
}
else//10%访问循环范围外的键
{
key = LOOP_SIZE + (gen() % LOOP_SIZE);
}
if (isPut)
{
std::string value = "loop" + std::to_string(key) + "_v" + std::to_string(j % 100);
cache[i]->put(key, value);
}
else
{
std::string value;
get_operations[i]++;//操作次数加一
bool isHit = cache[i]->get(key,value);
if (isHit)hits[i]++;
}
}
}
is_running = false;
animation_thread.join();
print("数据循环扫描测试");
clear();
}
void test::testWorkloadShift()
{
const size_t CAPACITY = 30;//缓存容量
const int OPERATIONS = 80000;//总操作数
const int PHASE_LENGTH = OPERATIONS / 5;//每个阶段的长度
init(CAPACITY, OPERATIONS);//初始化
std::random_device rd;
std::mt19937 gen(rd());//随机数
std::thread animation_thread(&test::loading_animation, this);
is_running = true;
for(int i=0;i<cacheNum;++i)//遍历各个缓存系统
{
//初始化一部分数据
for(int key=0;key<30;++key)
{
std::string value = "init" + std::to_string(key);
cache[i]->put(key, value);
}
for (int op = 0; op < OPERATIONS; ++op)
{
int curPhase = op / PHASE_LENGTH;
int putProbability;
switch (curPhase)
{
case 0:putProbability = 15; break;//阶段1:热点访问,15%写操作
case 1:putProbability = 30; break;//阶段2:大范围随机,30%写操作
case 2:putProbability = 10; break;//阶段3:顺序扫描,10%写操作
case 3:putProbability = 25; break;//阶段4:局部性随机,微调
case 4:putProbability = 20; break;//阶段5:混合访问,20%写操作
default:putProbability = 20;
}
bool put = ((gen() % 100) < putProbability);
int key;
if (op < PHASE_LENGTH) {//阶段1:热点访问:热点数量为5
key = gen() % 5;
}
else if (op < PHASE_LENGTH * 2) {//阶段2,大范围随机,范围400
key = gen() % 400;
}
else if (op < PHASE_LENGTH * 3) {//阶段3:顺序扫描,保持100个键
key = (op - PHASE_LENGTH * 2) % 100;
}
else if (op < PHASE_LENGTH * 4) {//阶段4:局部性随机-优化局部性区域大小
//相当于一段时间在某个部分随机,然后又一段时间在另一个部分随机
int locality = (op / 800) % 5;//分成五个区域
key = 15 * locality + gen() % 15;//每个区域十五个键
}
else {//阶段5:混合访问-增加热点访问比例
int r = gen() % 100;
if (r < 40) {//40%访问热点数据
key = gen() % 5;
}
else if (r < 70) {//30%访问中范围数据
key = 5 + (gen() % 50);//中等范围为50个键
}
else {//30%概率访问大范围(从40%减少)
key = 55 + (gen() % 350);
}
}
if (put)
{
//执行写操作
std::string value = "value" + std::to_string(key) + "_p" + std::to_string(curPhase);
cache[i]->put(key, value);
}
else
{
//执行读操作
std::string result;
get_operations[i]++;
if (cache[i]->get(key, result))hits[i]++;//命中
}
}
}
is_running = false;
animation_thread.join();
print("工作负载剧烈变化测试");
clear();
}
void test::totalTest()
{
this->testHotDataAccess();
system("pause");
std::cout << "\n" << "点击任意键进行下一项测试" << std::endl;
this->testLoopPattern();
system("pause");
std::cout << "\n" << "点击任意键进行下一项测试" << std::endl;
this->testWorkloadShift();
}
void test::print(const std::string&testName)
{
std::cout << "-------" << testName << "-------" << "\n";
std::cout << "缓存大小: " << capacity << "\n";
std::cout << "总操作次数: " << opNum << "\n";
std::cout << std::endl;
for (int i = 0; i < cacheNum; ++i)
{
std::cout << "缓存系统名称: " << cacheName[i] << "\n";
std::cout << "读操作次数: " << get_operations[i] << "\n";
std::cout << "读操作命中次数: " << hits[i] << "\n";
std::cout << "命中率: " <<std::fixed<<std::setprecision(2)<<100.0 * hits[i] / get_operations[i]<<"%"<<"\n";
std::cout << std::endl;
}
}
void test::init(const size_t &capacity,const int &opN)
{
this->capacity = capacity;
this->opNum = opN;
cacheName.push_back("Lru");
cacheName.push_back("Lfu");
cacheName.push_back("lruCacheK");
cacheName.push_back("arcCache");
cacheNum = cacheName.size();
cache.push_back(new Cache::LruCache<int,std::string>(capacity));
cache.push_back(new Cache::LfuCache<int, std::string>(capacity));
cache.push_back(new Cache::lruCacheK<int, std::string>(capacity, 20, 2));
cache.push_back(new Cache::arcCache<int, std::string>(capacity, 2));
get_operations.assign(cacheNum, 0);
hits.assign(cacheNum, 0);
}
void test::loading_animation()
{
int dots = 0;
while (is_running)
{
std::string progress(dots % 4, '.');
std::cout << "\r程序运行中" << progress << " " << std::flush;
dots++;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
system("cls");//清屏
std::cout << "\r完成"<<std::endl;
}
void test::clear()
{
cache.clear();
get_operations.clear();
hits.clear();
cacheName.clear();
cacheNum = 0;
capacity = 0;
opNum = 0;
}