-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
175 lines (116 loc) · 3.47 KB
/
main.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
//
// Created by 唐仁初 on 2022/10/13.
//
#include "ValueTable.h"
#include "SkipList.h"
#include <iostream>
#include <map>
#include <thread>
using namespace mvcc;
using namespace std;
int ValueConcurrentInsertTest() {
std::atomic<int> version = 0;
Value value;
auto start = std::chrono::system_clock::now();
std::thread th([&version, &value] {
for (int i = 0; i < 100000; i++) {
auto v = version.fetch_add(1);
value.write("1", v);
}
});
for (int i = 0; i < 100000; i++) {
auto v = version.fetch_add(1);
value.write("1", v);
}
th.join();
auto end = std::chrono::system_clock::now();
std::cout << "Insert time ms : " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
<< std::endl;
return 0;
}
int singleInsertTest() {
ValueTable value;
//Value value;
auto start = std::chrono::system_clock::now();
std::thread th([&value] {
for (int i = 0; i < 100000; i++) {
value.emplace("1", "1");
}
});
for (int i = 0; i < 100000; i++) {
value.emplace("1", "1");
}
th.join();
auto end = std::chrono::system_clock::now();
std::cout << "Insert time ms : " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
<< std::endl;
return 0;
}
void ops() {
ValueTable table;
// 插入一个键值对
table.emplace("key", "value");
// 查找键值对是否存在
table.exist("key");
// 读取键值对
table.read("key");
// 修改键值对
table.update("key", "new_value");
//删除键值对
table.erase("key");
for(auto it = table.begin();it != table.end();++it){
auto value = *it;
}
for(auto it = table.find("start");it != table.end();++it){
auto value = *it;
}
for(auto it = table.find("start"),end = table.find("end");it != end;++it){
auto value = *it;
}
std::vector<std::pair<std::string, std::string>> kvs = {{"k1","v1"},{"k2","v2"}};
bool finished = table.bulkWrite(kvs);
bool committed = table.transaction(kvs);
}
int garbageCleanTest(){
ValueTable table(3);
table.emplace("1","1");
table.emplace("2","2");
table.erase("2");
table.compact();
//table.emplace("3","1");
//std::cout << table.size();
this_thread::sleep_for(std::chrono::seconds(2));
return 0;
}
int main() {
return garbageCleanTest();
std::vector<std::string> keys(100000);
for (int i = 0; i < 100000; i++) {
keys[i] = std::to_string(i);
}
ValueTable table;
mvcc::SkipList<Value> skipList(18);
std::map<std::string, mvcc::Value *> map;
std::mutex mtx;
auto start = std::chrono::system_clock::now();
// std::thread th([&skipList,&keys,&mtx,&map](){
// for(int i = 0;i < 50000;i++){
//// mtx.lock();
//// map.emplace(keys[i],"1");
// skipList.insert(keys[i],"1");
// // mtx.unlock();
// }
// });
for (int i = 0; i < 100000; i++) {
// mtx.lock();
// map.emplace(keys[i],new Value("1",0));
// skipList.insert(keys[i]);
// mtx.unlock();
table.emplace(keys[i],"1");
}
auto end = std::chrono::system_clock::now();
// th.join();
std::cout << "Insert time ms : " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
<< std::endl;
return 0;
}