-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
196 lines (172 loc) · 4.59 KB
/
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
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
#include<iostream>
#include<time.h>
#include"Test.h"
using namespace std;
void Test::bPlusTreeInsertTest(BPlusTree* bPlusTree,int start,int end)
{
logger.writeLog("insert num:"+to_string(end - start),Logger::LogType::INFO);
for(int i=start;i<end;i++)
{
bPlusTree->insert(i,i);
}
}
void Test::bPlusTreeInsertTestByRand(BPlusTree* bPlusTree,int n)
{
srand((unsigned long long)time(NULL));
logger.writeLog("insert num:"+to_string(n),Logger::LogType::INFO);
for(int i=0;i<n;i++)
{
int key=rand();
int value=rand();
bPlusTree->insert(key,value);
}
}
void Test::bPlusTreeDeleteTest(BPlusTree* bPlusTree,int start,int end)
{
logger.writeLog("delete num:"+to_string(end -start),Logger::LogType::INFO);
for(int i=start;i<end;i++)
{
bPlusTree->deleteData(i);
}
}
void Test::bPlusTreeDeleteTestByRand(BPlusTree* bPlusTree,int n)
{
logger.writeLog("delete num:"+to_string(n),Logger::LogType::INFO);
srand((unsigned long long)time(NULL));
for(int i=0;i<n;i++)
{
int key=rand();
bPlusTree->deleteData(key);
}
}
void Test::bPlusTreeSearchTest(BPlusTree* bPlusTree,int start,int end)
{
logger.writeLog("search num:"+to_string(end - start),Logger::LogType::INFO);
for(int i=start;i<end;i++)
{
bPlusTree->search(i);
}
}
void Test::bPlusTreeSearchTestByRand(BPlusTree* bPlusTree,int n)
{
logger.writeLog("search num:"+to_string(n),Logger::LogType::INFO);
srand((unsigned long long)time(NULL));
for(int i=0;i<n;i++)
{
int key=rand();
bPlusTree->search(key);
}
}
void Test::bPlusTreeUpdateTest(BPlusTree* bPlusTree,int start,int end)
{
logger.writeLog("update num:"+to_string(end - start),Logger::LogType::INFO);
for(int i=start;i<end;i++)
{
bPlusTree->update(i,i+1);
}
}
void Test::bPlusTreeUpdateTestByRand(BPlusTree* bPlusTree,int n)
{
logger.writeLog("update num:"+to_string(n),Logger::LogType::INFO);
srand((unsigned long long)time(NULL));
for(int i=0;i<n;i++)
{
int key=rand();
int value=rand();
bPlusTree->update(key,value);
}
}
void Test::testBPlusTree()
{
logger.writeLog("testBPlusTree...",Logger::LogType::INFO);
BPlusTree *bPlusTree=new BPlusTree(BPLUSTREE_ORDER);
vector<thread> thread_array;
//测试并发
thread_array.push_back(thread(&Test::bPlusTreeInsertTest,this,bPlusTree,0,500000));
thread_array.push_back(thread(&Test::bPlusTreeInsertTestByRand,this,bPlusTree,300000));
thread_array.push_back(thread(&Test::bPlusTreeDeleteTest,this,bPlusTree,200000,300000));
thread_array.push_back(thread(&Test::bPlusTreeDeleteTestByRand,this,bPlusTree,500000));
thread_array.push_back(thread(&Test::bPlusTreeSearchTest,this,bPlusTree,0,100000));
thread_array.push_back(thread(&Test::bPlusTreeSearchTestByRand,this,bPlusTree,300000));
thread_array.push_back(thread(&Test::bPlusTreeUpdateTest,this,bPlusTree,300000,400000));
thread_array.push_back(thread(&Test::bPlusTreeUpdateTestByRand,this,bPlusTree,300000));
for(auto ite=thread_array.begin();ite!=thread_array.end();ite++)
{
if(ite->joinable())
{
ite->join();
}
}
if(bPlusTree->checkTree())
{
cout<<"满足B+树定义."<<endl;
}
else
{
cout<<"不满足B+树定义."<<endl;
}
delete bPlusTree;
logger.writeLog("testBPlusTree end.",Logger::LogType::INFO);
}
//多线程添加
void Test::apendTest(int n)
{
TableManager& tableManager = TableManager::getTableManagerInstance();
vector<thread> thread_array;
//数据添加
for(int i=0;i<n;i++)
{
Record r;
thread_array.push_back(thread(&TableManager::append,ref(tableManager),ref(r)));
}
for(auto ite=thread_array.begin();ite!=thread_array.end();ite++)
{
ite->join();
}
}
//整体测试
void Test::testAll()
{
int best_thread_num=thread::hardware_concurrency();
TableManager& tableManager = TableManager::getTableManagerInstance();
int N=2;
vector<thread> thread_array;
//直接搜索
map<int,Record> res=tableManager.search(0,100,2000);
for(auto ite=res.begin();ite!=res.end();ite++)
{
cout<<ite->first<<": ";
Record r=ite->second;
//打印前8个属性
for(int i=0;i<8;i++)
{
cout<<r.m_gAttributes[i]<<" ";
}
cout<<endl;
}
//添加多线程测试
apendTest(best_thread_num);
//创建索引测试
for(int i=0;i<N;i++)
{
Record r;
thread_array.push_back(thread(&TableManager::createIndex,ref(tableManager),i));
}
for(auto ite=thread_array.begin();ite!=thread_array.end();ite++)
{
ite->join();
}
//使用索引查找数据
res=tableManager.search(0,100,2000);
for(auto ite=res.begin();ite!=res.end();ite++)
{
cout<<ite->first<<": ";
Record r=ite->second;
//打印前8个属性
for(int i=0;i<8;i++)
{
cout<<r.m_gAttributes[i]<<" ";
}
cout<<endl;
}
}