forked from bhhbazinga/LockFreeHashTable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_test_zipfain.cpp
More file actions
103 lines (96 loc) · 3.73 KB
/
thread_test_zipfain.cpp
File metadata and controls
103 lines (96 loc) · 3.73 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
#include "LRUCache.h"
#include "double_link_list.h"
#include "lockfree_hashtable.h"
#include "create_zipfian_data.h"
#include "operate_list.h"
#include <iostream>
#include <random>
#include <vector>
#include <thread>
#include <chrono>
#include <cstdio>
using namespace std;
using key_type = string;
using value_type = string;
value_type error_info = "-1";
bool operator!=(const value_type& lhs, const value_type& rhs) {
return !(lhs == rhs);
}
typedef operate_list<key_type, value_type> list_type;
typedef list_type::Node node_type;
typedef LockFreeHashTable<key_type, node_type *> hash_type;
const int key_range = 1000; // [0, key_range] // Corpus size
const int job_num = 1000000;
const int client_num = 5;
const float read_rate = 0.8;
const int cache_capacity = 100;
const int key_len = 8;
const int value_len = 16;
std::atomic<int>get_sum(0);
std::atomic<int>hit_sum(0);
struct Task {
key_type key;
value_type value;
bool job_type;
};
Task task[client_num + 1][job_num / client_num + 1];
std::thread threads[client_num];
void start_task(int client_id, LRUCache<key_type, value_type, hash_type, list_type, node_type> *cache) {
for (int i = 1;i <= job_num / client_num;i ++) {
Task now = task[client_id][i];
if (now.job_type) { // get
if (cache->get(now.key) != error_info) {
hit_sum.fetch_add(1);
}
get_sum.fetch_add(1);
} else {
cache->put(now.key, now.value);
}
}
}
string keys[job_num], values[job_num];
void generate_zipfian_data() {
int numStrings = key_range; // 总共的字符串数量
double zipfianAlpha = 1.0; // Zipfian 分布的参数
ZipfianStringGenerator generator_key(numStrings, zipfianAlpha, key_len);
ZipfianStringGenerator generator_value(numStrings, zipfianAlpha, value_len);
int testCount = job_num;
for (int i = 0; i < testCount; ++i) {
std::string randomString = generator_key.getRandomString();
keys[i] = randomString;
// keys.push_back(randomString);
}
for (int i = 0; i < testCount; ++i) {
std::string randomString = generator_value.getRandomString();
values[i] = randomString;
// values.push_back(randomString);
}
}
int main() {
generate_zipfian_data();
list_type *list = new list_type(cache_capacity);
hash_type *hash = new hash_type();
LRUCache<key_type, value_type, hash_type, list_type, node_type> *lRUCache = new LRUCache<key_type, value_type, hash_type, list_type, node_type>(2, hash, list, &error_info);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, key_range);
std::uniform_real_distribution<> dis2(0, 1);
for (int i = 1; i <= client_num; i++) {
for (int j = 1; j <= job_num / client_num; j++) {
int now_num = (i - 1) * job_num / client_num;
task[i][j] = {keys[now_num + j], values[now_num + j], (dis2(gen) > read_rate) ? false : true}; // false -> put, true -> get;
}
}
printf("任务生成完毕, 线程数:%d, 任务总量:%d, 读操作比重:%f\n", client_num, job_num, read_rate);
auto start_time = std::chrono::high_resolution_clock::now();
for (int i = 1;i <= client_num;i ++) {
threads[i] = std::thread(start_task, i, lRUCache);
threads[i].join();
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
cout << "任务执行完毕" << endl;
std::cout << "程序运行时间:" << duration.count() << " 微秒" << std::endl;
std::cout << "总get:" << get_sum.load() << ", 命中次数:" << hit_sum.load() << ", 命中率:" << (float)hit_sum.load()/get_sum.load() << endl;
return 0;
}