lmdb性能测试——代码及结果
#include <iostream>
#include <string>
#include <chrono>
#include "lmdb.h"
using namespace std;
int main(int argc, char* argv[]){
int res;
MDB_env *env;
MDB_dbi dbi;
MDB_val key, data;
MDB_txn *txn;
//init lmdb
cout<<"lmdb version:"<<mdb_version(, , )<<endl;
res = mdb_env_create(&env);
if(res){
cout<<"mdb_env_create error,error:"<<mdb_strerror(res)<<endl;
return -1;
}
res=mdb_env_set_mapsize(env,1024*1024*1024);
if(res!=){
cout<<"mdb_env_set_mapsize error,detail:"<< mdb_strerror(res)<<endl;
return -1;
}
res = mdb_env_open(env, "./lmdb_test", , 0644);
if(res){
cout<<"mdb_env_open error,detail:"<< mdb_strerror(res)<<endl;
return -1;
}
res = mdb_txn_begin(env, NULL, , &txn);
if(res){
cout<<"mdb_txn_begin error,detail:"<< mdb_strerror(res)<<endl;
return -1;
}
res = mdb_dbi_open(txn, NULL, , &dbi);
if(res){
cout<<"mdb_dbi_open error,detail:"<< mdb_strerror(res)<<endl;
return -1;
}
do{
//write data to lmdb
unsigned long long count=1000000;
unsigned long long value=;
auto start=std::chrono::system_clock::now();
unsigned long long i=;
for(;i<count;++i){
value=i+1;
key.mv_size =sizeof(i);
key.mv_data =(void*)&i;
data.mv_size = sizeof(value);
data.mv_data = (void*)&value;
res = mdb_put(txn, dbi, &key, &data, );
if(res!=)
{
cout<<"mdb_put error,res="<<res<<",detail:"<<mdb_strerror(res)<<endl;
break;
}
}
auto stop=std::chrono::system_clock::now();
std::chrono::nanoseconds time_used=stop-start;
cout<<"write "<<i<<" items use:"<<time_used.count()<<" ns"<<endl;
start=std::chrono::system_clock::now();
res = mdb_txn_commit(txn);
stop=std::chrono::system_clock::now();
if (res) {
cerr<<"mdb_txn_commit error:"<< res<<":"<< mdb_strerror(res)<<endl;
break;
}
time_used=stop-start;
cout<<"commit use:"<<time_used.count()<<" ns"<<endl;
res = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
MDB_cursor *cursor;
res = mdb_cursor_open(txn, dbi, &cursor);
//read data from lmdb
start=std::chrono::system_clock::now();
unsigned long long read_count=;
while ((res = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == ) {
int r_key=*(int *)key.mv_data;
int r_value=*(int *)data.mv_data;
++read_count;
//cout<<"<"<<r_key<<","<<r_value<<">"<<endl;
}
stop=std::chrono::system_clock::now();
time_used=stop-start;
cout<<"read "<<read_count<<" items, use:"<<time_used.count()<<" ns"<<endl;
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
}while();
//free
mdb_dbi_close(env, dbi);
mdb_env_close(env);
return ;
}
使用-O3编译完成后,在4核/2GB/win10虚拟的centos进行性能测试,分别读写100万条unsigned long long数据,测试结果如下:
读数据:4~5ns/条
写数据:90~120ns/条
提交事务:265ms/100万条数据(与size大小有关系)
综合来看,速度还是非常之快的,比redis快了N多倍,这还是在linux虚拟机上测试的结果,在物理机上效果会更好;
————————————————
原文链接:https://blog.csdn.net/lianshaohua/article/details/108666612
相关文章