InvoDB/invodb/main.cpp

139 lines
3.1 KiB
C++
Raw Normal View History

2021-10-05 14:41:15 +08:00
//
// Created by YuhangQ on 2021/9/24.
//
#include "main.h"
2021-10-30 19:48:39 +08:00
void testAndBenchmark(int n);
2021-10-05 14:41:15 +08:00
int main() {
2021-10-28 23:15:15 +08:00
int t = time(0);
2021-11-03 22:05:13 +08:00
srand(1635418590);
//srand(t);
2021-10-28 23:15:15 +08:00
printf("seed: %d\n", t);
2021-10-25 22:07:18 +08:00
2021-11-05 23:15:09 +08:00
system("rm -rf test.invodb && touch test.invodb");
2021-10-24 20:21:04 +08:00
2021-10-23 16:28:57 +08:00
PageManager::loadDatabase("test.invodb");
2021-11-01 15:11:33 +08:00
2021-10-23 16:28:57 +08:00
Collection::loadCollections();
2021-10-11 22:05:45 +08:00
2021-10-23 16:28:57 +08:00
PageManager& manager = PageManager::Instance();
2021-10-11 22:05:45 +08:00
2021-10-25 22:07:18 +08:00
Collection *col;
try {
col = &Collection::getCollection("hello");
} catch(const char *error) {
Collection::createCollection("hello");
2021-11-01 15:11:33 +08:00
col = &Collection::getCollection("hello");
2021-10-25 22:07:18 +08:00
}
2021-10-23 16:28:57 +08:00
2021-11-03 09:39:13 +08:00
2021-11-07 12:57:26 +08:00
freopen("qq.txt", "r", stdin);
const int n = 1000000;
char qq[100], phone[100];
clock_t start = clock();
for(int i=0; i<n; i++) {
scanf("%s%s", qq, phone);
nlohmann::json json;
json["qq"] = qq;
json["phone"] = phone;
col->insert(json);
if(i % (n/1000) == 0) {
printf("[%d/%d] time=%fs!\n", i, n, (double)(clock() - start) / CLOCKS_PER_SEC);
start = clock();
}
}
2021-11-01 10:35:16 +00:00
2021-11-05 23:15:09 +08:00
//col->test();
2021-11-01 10:35:16 +00:00
2021-11-03 22:05:13 +08:00
// nlohmann::json j = nlohmann::json::parse(R"(
//{
// "string": "this is a string!",
// "double": 3.1415,
// "int": 25565,
// "bool": true,
// "child": {
// "id": 3
// },
// "array": ["1", "2", "3"]
//}
// )");
2021-11-02 11:35:41 +08:00
2021-11-07 12:57:26 +08:00
testAndBenchmark(20000);
2021-11-03 09:39:13 +08:00
2021-10-05 14:41:15 +08:00
return 0;
2021-10-30 19:48:39 +08:00
}
void testAndBenchmark(int n) {
2021-11-03 22:05:13 +08:00
auto btree = new BTree<std::string, 128>(PageManager::Instance().allocate());
2021-10-30 19:48:39 +08:00
printf("nodeSize: %d\n", btree->getNodeSize());
clock_t start = clock();
2021-10-30 20:52:06 +08:00
std::map<std::string, int> map;
2021-10-30 19:48:39 +08:00
for(int i=0; i<n; i++) {
int opt = rand() % 4;
2021-11-03 22:05:13 +08:00
2021-11-05 23:15:09 +08:00
if(i%(n/100) == 0) {
printf("[%d/%d] eeeeee\n", i, n);
}
2021-10-30 19:48:39 +08:00
// insert
if(opt <= 1) {
std::string uuid = generateUUID();
2021-10-30 20:52:06 +08:00
int addr = rand();
2021-10-30 19:48:39 +08:00
btree->insert(uuid, addr);
map[uuid] = addr;
}
// update
else if(opt == 2) {
if(map.size() == 0) continue;
auto it = map.begin();
std::advance(it, rand() % map.size());
std::string uuid = it->first;
2021-10-30 20:52:06 +08:00
double addr = rand();
2021-10-30 19:48:39 +08:00
map[uuid] = addr;
btree->update(uuid, addr);
}
// remove
else {
if(map.size() == 0) continue;
auto it = map.begin();
std::advance(it, rand() % map.size());
std::string uuid = it->first;
map.erase(uuid);
btree->remove(uuid);
}
2021-11-03 22:05:13 +08:00
//printf("opt: %d\n", opt);
//btree->print();
2021-10-30 19:48:39 +08:00
}
if(map.size() != btree->size()) {
printf("%d %d\n", map.size(), btree->size());
printf("BTree has BUG!\n");
exit(0);
}
printf("test res k-v: %d\n", map.size());
for(auto it=map.begin(); it != map.end(); it++) {
if(btree->find(it->first) != it->second) {
printf("BTree has BUG!\n");
2021-10-30 20:52:06 +08:00
exit(0);
2021-10-30 19:48:39 +08:00
}
}
clock_t end = clock();
printf("BTree pass the test with n=%d, time=%fs!\n", n, (double)(end - start) / CLOCKS_PER_SEC);
}