c++ mongodb driver 性能分析

系列数据库开发



前言


一、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性能有个大概的了解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

c+猿辅导

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值