系列数据库开发
文章目录
前言
一、mongodb 性能测试环境准备
测试基准:
Centos7.5
cpu:4
memory:6g
安装Mongodb数据库,版本为4.4.3
g++ 4.8.5
mongo启动使用默认配置,无任何修改的环境下。
二、c++ 在centos 7.5 mongo create与query性能分析
1. 代码实现
本测试中通过glags修改insert线程数threaNum,与内存分配大小size
std::atomic_bool running{false};
std::atomic_int timestart(0);
std::atomic_int count(0);
char *buffer = nullptr;
const int EverySeconds = 5;
DEFINE_int32(threaNum, 10, "thread num");
DEFINE_int32(length, 2*1024*1024, "memory size");
void wirteDBTask(int taskid)
{
std::cout<< "create Task------>"<< taskid <<std::endl;
std::thread([taskid](){
auto client = dbclient();
auto coll = (*client)["vtest"]["test"];
while (true) {
auto userid = butil::GenerateGUID();
const auto &doc = bsoncxx::builder::stream::document{}
<< "_id" << userid
<< "user" << butil::GenerateGUID()
<< "pwd" << butil::GenerateGUID()
<< "name" << "text"
<< "jpegdata" << buffer
<< finalize;
coll.insert_one(doc.view());
++count;
auto now = time(NULL);
if(now - timestart > EverySeconds){
LOG(INFO)<< "every second average: "<< count/EverySeconds;
count = 0;
timestart = now;
}
}
std::cout<< "destroy Task------>"<< taskid <<std::endl;
}).detach();
}
void readDBTask(int taskid)
{
std::cout<< "create Task------>"<< taskid <<std::endl;
std::thread([taskid](){
auto client = dbclient();
auto coll = (*client)["vtest"]["test"];
std::string user[5] = {
"7D1D01FD-41B7-FEC9-E0F9-FF64BF63E35F",
"DDDDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEEE",
"7D1D01FD-41B7-FEC9-E0F9-FF64BF63E35F",
"86680647-6966-24D8-00B3-FF38CAA608A7",
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
};
int circle = 0;
while (true) {
++circle;
if(circle%5==0){
circle = 0;
}
const auto &doc = bsoncxx::builder::stream::document{}
<< "user" << user[circle]
<< finalize;
auto resultdoc = coll.find_one(doc.view());
if(resultdoc){
resultdoc->view()["name"].get_utf8().value.to_string();
}
++count;
auto now = time(NULL);
if(now - timestart > EverySeconds){
LOG(INFO)<< "every second average: "<< count/EverySeconds;
count = 0;
timestart = now;
}
}
std::cout<< "destroy Task------>"<< taskid <<std::endl;
}).detach();
return ;
}
bool InsertDataByMutilThreads()
{
auto result = true;
running = true;
buffer = new char [FLAGS_length];
memset((void*)buffer,1,FLAGS_length);
timestart = time(NULL);
for(auto taskid=0;taskid<FLAGS_threaNum;taskid++){
wirteDBTask(taskid);
}
return res;
}
bool QueryDataByMutilThreads()
{
auto result = true;
running = true;
buffer = new char [FLAGS_length];
memset((void*)buffer,1,FLAGS_length);
timestart = time(NULL);
for(auto taskid=0;taskid<FLAGS_threaNum;taskid++){
readDBTask(taskid);
}
return res;
}
2. 写性能测试
实验1:固定buffersize = 102410242=2M,通过增加多线程来insert,当线程达到一定数量后,tps不会持续增加。
a)
thread Num: 1
buffer alloc length: 2097152
tps:60-70
b)
buffer alloc length: 2097152
thread Num: 4
tps:160-170
c)
buffer alloc length: 2097152
thread Num: 8
tps:160-170
实验2:固定线程数4,通过insert不同大小的buffer,当buffer减少到一定大小,就会保持tps稳定,也就是说tps随着buffersize的增加会降低。
a)
thread Num: 4
buffer alloc length: 2097152 = 2M
tps:160-170
b)
thread Num: 4
buffer alloc length: 1048576 = 1M
tps:320-350
c)
thread Num: 4
buffer alloc length: 524288= 512K
tps: 600-700
d)
thread Num: 4
buffer alloc length: 262144= 256K
tps: 1100-1200
e)
thread Num: 4
buffer alloc length: 1024= 1K
tps: 5000-6000
f)
thread Num: 4
buffer alloc length: 1Byte
tps: 5000-6000
3. 以_id为查询条件的读性能测试
默认以_id为索引
std::atomic_bool running{false};
std::atomic_int timestart(0);
std::atomic_int count(0);
char *buffer = nullptr;
const int EverySeconds = 5;
DEFINE_int32(threaNum, 10, "thread num");
DEFINE_int32(length, 2*1024*1024, "memory size");
void readDBTask(int taskid)
{
std::cout<< "create Task------>"<< taskid <<std::endl;
std::thread([taskid](){
auto client = dbclient();
auto coll = (*client)["vtest"]["test"];
std::string id[5] = {
"7D1D01FD-41B7-FEC9-E0F9-FF64BF63E35F",
"DDDDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEEE",
"7D1D01FD-41B7-FEC9-E0F9-FF64BF63E35F",
"86680647-6966-24D8-00B3-FF38CAA608A7",
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
};
int circle = 0;
while (true) {
++circle;
if(circle%5==0){
circle = 0;
}
const auto &doc = bsoncxx::builder::stream::document{}
<< "_id" << id[circle]
<< finalize;
auto resultdoc = coll.find_one(doc.view());
if(resultdoc){
resultdoc->view()["name"].get_utf8().value.to_string();
}
++count;
auto now = time(NULL);
if(now - timestart > EverySeconds){
LOG(INFO)<< "every second average: "<< count/EverySeconds;
count = 0;
timestart = now;
}
}
std::cout<< "destroy Task------>"<< taskid <<std::endl;
}).detach();
return ;
}
bool QueryDataByMutilThreads()
{
auto result = true;
running = true;
buffer = new char [FLAGS_length];
memset((void*)buffer,1,FLAGS_length);
timestart = time(NULL);
for(auto taskid=0;taskid<FLAGS_threaNum;taskid++){
readDBTask(taskid);
}
return res;
}
实验1:通过增加多线程来query,当线程达到一定数量后,tps不会持续增加。
本实验默认以_id为查询条件。
a)
thread Num: 1
tps:800-900
b)
thread Num: 4
tps:950-1050
c)
thread Num: 4
tps:950-1050
4. 无索引与有索引读性能测试
std::atomic_bool running{false};
std::atomic_int timestart(0);
std::atomic_int count(0);
char *buffer = nullptr;
const int EverySeconds = 5;
DEFINE_int32(threaNum, 10, "thread num");
DEFINE_int32(length, 2*1024*1024, "memory size");
void readDBTask(int taskid)
{
std::cout<< "create Task------>"<< taskid <<std::endl;
std::thread([taskid](){
auto client = dbclient();
auto coll = (*client)["vtest"]["test"];
std::string user[5] = {
"7D1D01FD-41B7-FEC9-E0F9-FF64BF63E35F",
"DDDDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEEE",
"7D1D01FD-41B7-FEC9-E0F9-FF64BF63E35F",
"86680647-6966-24D8-00B3-FF38CAA608A7",
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
};
int circle = 0;
while (true) {
++circle;
if(circle%5==0){
circle = 0;
}
const auto &doc = bsoncxx::builder::stream::document{}
<< "user" << user[circle]
<< finalize;
auto resultdoc = coll.find_one(doc.view());
if(resultdoc){
resultdoc->view()["name"].get_utf8().value.to_string();
}
++count;
auto now = time(NULL);
if(now - timestart > EverySeconds){
LOG(INFO)<< "every second average: "<< count/EverySeconds;
count = 0;
timestart = now;
}
}
std::cout<< "destroy Task------>"<< taskid <<std::endl;
}).detach();
return ;
}
bool QueryDataByMutilThreads()
{
auto result = true;
running = true;
buffer = new char [FLAGS_length];
memset((void*)buffer,1,FLAGS_length);
timestart = time(NULL);
for(auto taskid=0;taskid<FLAGS_threaNum;taskid++){
readDBTask(taskid);
}
return res;
}
实验1:
固定线程数,通过有索引查询与无索引query的tps,有索引的tps远远大于索引查询tps
本实验默认以_id为查询条件。
a)
thread Num: 4
tps:0-1
b)
db.test.createIndex({user:1});
thread Num: 4
tps:950-1050
总结
以上就是今天要讲的内容,希望你对mongodb性能有个大概的了解。