準(zhǔn)備工作:
1. 下載Snappy庫(kù)
Download source code from: http://code.google.com/p/snappy
編譯并安裝
./configure & make & sudo make install
2. 編譯leveldb自帶的db_bench
make db_bench
注意:在ubuntu 11.04上編譯會(huì)出錯(cuò),修改makefile:
1. 下載Snappy庫(kù)
Download source code from: http://code.google.com/p/snappy
編譯并安裝
./configure & make & sudo make install
2. 編譯leveldb自帶的db_bench
make db_bench
注意:在ubuntu 11.04上編譯會(huì)出錯(cuò),修改makefile:
$(CXX) $(LDFLAGS) db/db_bench.o $(LIBOBJECTS) $(TESTUTIL) -o $@
為
為了獲取更多的信息,我寫了一個(gè)簡(jiǎn)單的測(cè)試程序來(lái)測(cè)試寫性能。
我隔10w條記錄統(tǒng)計(jì)一下運(yùn)行時(shí)間和各層level的個(gè)數(shù)。
得到結(jié)果如下:

可以看出 插入時(shí)間不穩(wěn)定,一旦level 0 的文件個(gè)數(shù)達(dá)到8(leveldb在level0 sst file到達(dá)8會(huì)做流量控制),就會(huì)嚴(yán)重的影響插入速度。
數(shù)據(jù)如下: 前7欄為各level的文件個(gè)數(shù),最后一欄為插入時(shí)間(單位second).
為
$(CXX) db/db_bench.o $(LIBOBJECTS) $(TESTUTIL) -o $@ $(LDFLAGS)
為了獲取更多的信息,我寫了一個(gè)簡(jiǎn)單的測(cè)試程序來(lái)測(cè)試寫性能。
g++ src/ldbbench.cpp libleveldb.a -I../leveldb/include -o ldb_test -pthread -lsnappy
我隔10w條記錄統(tǒng)計(jì)一下運(yùn)行時(shí)間和各層level的個(gè)數(shù)。
#include <iostream>
#include <cstdlib>
#include <sys/time.h>
#include "leveldb/db.h"
#include "leveldb/env.h"
using namespace std;
using namespace leveldb;
static inline double micro_time(){
struct timeval tim;
double ret;
gettimeofday(&tim, NULL);
ret = tim.tv_sec+(tim.tv_usec/1000000.0);
return ret;
}
int main() {
srand ( time(NULL) );
DB *db ;
Options op;
op.create_if_missing = true;
Status s = DB::Open(op,"/tmp/testdb",&db);
Env * env = Env::Default();
WritableFile *file;
env->NewWritableFile("/tmp/bench.csv",&file);
if(s.ok()){
cout << "create successfully" << endl;
WriteOptions wop;
for(int j=0;j<100;++j){
double start = micro_time();
double cost;
for(int i=0;i<100000;++i){
char key[100];
char value[100];
sprintf(key,"%d_%d",i,rand());
sprintf(value,"%d",rand());
db->Put(wop,key,value);
}
cost = micro_time()-start;
cout << "write successfully:" << j << ",costs "<<cost<<endl;
// report the status
{
//output stats information
string value;
char buffer[40];
for(int i=0;i<7;++i){
sprintf(buffer,"leveldb.num-files-at-level%d",i);
db->GetProperty(buffer,&value);
file->Append(value+",");
}
sprintf(buffer,"%f",cost);
file->Append(buffer);
file->Append("\n");
file->Sync();
}
}
cout << "write completed" << endl;
}
delete db;
file->Close();
delete file;
return 0;
}
#include <cstdlib>
#include <sys/time.h>
#include "leveldb/db.h"
#include "leveldb/env.h"
using namespace std;
using namespace leveldb;
static inline double micro_time(){
struct timeval tim;
double ret;
gettimeofday(&tim, NULL);
ret = tim.tv_sec+(tim.tv_usec/1000000.0);
return ret;
}
int main() {
srand ( time(NULL) );
DB *db ;
Options op;
op.create_if_missing = true;
Status s = DB::Open(op,"/tmp/testdb",&db);
Env * env = Env::Default();
WritableFile *file;
env->NewWritableFile("/tmp/bench.csv",&file);
if(s.ok()){
cout << "create successfully" << endl;
WriteOptions wop;
for(int j=0;j<100;++j){
double start = micro_time();
double cost;
for(int i=0;i<100000;++i){
char key[100];
char value[100];
sprintf(key,"%d_%d",i,rand());
sprintf(value,"%d",rand());
db->Put(wop,key,value);
}
cost = micro_time()-start;
cout << "write successfully:" << j << ",costs "<<cost<<endl;
// report the status
{
//output stats information
string value;
char buffer[40];
for(int i=0;i<7;++i){
sprintf(buffer,"leveldb.num-files-at-level%d",i);
db->GetProperty(buffer,&value);
file->Append(value+",");
}
sprintf(buffer,"%f",cost);
file->Append(buffer);
file->Append("\n");
file->Sync();
}
}
cout << "write completed" << endl;
}
delete db;
file->Close();
delete file;
return 0;
}
得到結(jié)果如下:
可以看出 插入時(shí)間不穩(wěn)定,一旦level 0 的文件個(gè)數(shù)達(dá)到8(leveldb在level0 sst file到達(dá)8會(huì)做流量控制),就會(huì)嚴(yán)重的影響插入速度。
數(shù)據(jù)如下: 前7欄為各level的文件個(gè)數(shù),最后一欄為插入時(shí)間(單位second).
0,0,0,0,0,0,0,0.312044
0,0,1,0,0,0,0,0.339661
0,1,1,0,0,0,0,0.336554
1,1,1,0,0,0,0,0.338470
2,1,1,0,0,0,0,0.319139
4,1,1,0,0,0,0,0.322158
5,1,1,0,0,0,0,0.411267
6,1,1,0,0,0,0,0.452211
7,1,1,0,0,0,0,0.392227
4,6,1,0,0,0,0,0.599982
5,6,1,0,0,0,0,0.392222
6,6,1,0,0,0,0,0.426607
7,6,1,0,0,0,0,0.450604
0,9,7,0,0,0,0,1.884518
1,9,7,0,0,0,0,0.420226
2,8,8,0,0,0,0,0.395083
3,8,8,0,0,0,0,0.418100
4,7,9,0,0,0,0,0.421611
6,7,9,0,0,0,0,0.415739
7,7,9,0,0,0,0,0.407361
1,14,10,0,0,0,0,2.226791
2,14,10,0,0,0,0,0.401517
3,14,10,0,0,0,0,0.373305
4,13,11,0,0,0,0,0.419741
5,13,11,0,0,0,0,0.409911
6,12,12,0,0,0,0,0.410904
7,12,12,0,0,0,0,0.429305
0,19,15,0,0,0,0,3.586968
2,19,15,0,0,0,0,0.443083
3,18,16,0,0,0,0,0.403899
4,18,16,0,0,0,0,0.427664
5,17,17,0,0,0,0,0.398022
6,16,19,0,0,0,0,0.373106
7,16,19,0,0,0,0,0.381070
0,16,27,0,0,0,0,3.997287
1,16,27,0,0,0,0,0.415576
2,15,29,0,0,0,0,0.395088
3,15,29,0,0,0,0,0.421756
4,15,29,0,0,0,0,0.423345
5,14,30,0,0,0,0,0.443051
6,13,32,0,0,0,0,0.409214
0,21,35,0,0,0,0,3.724305
1,21,35,0,0,0,0,0.394496
2,20,36,0,0,0,0,0.400312
3,20,36,0,0,0,0,0.440494
4,19,36,0,0,0,0,0.401116
5,19,36,0,0,0,0,0.368698
6,19,36,0,0,0,0,0.392624
7,18,37,0,0,0,0,0.421263
0,20,45,0,0,0,0,5.280940
1,20,45,0,0,0,0,0.445995
2,19,46,0,0,0,0,0.427433
3,19,46,0,0,0,0,0.396355
4,19,46,0,0,0,0,0.412447
6,18,47,0,0,0,0,0.425992
7,18,47,0,0,0,0,0.409269
0,22,54,0,0,0,0,4.659271
1,22,54,0,0,0,0,0.353135
2,22,54,0,0,0,0,0.412604
3,22,54,0,0,0,0,0.387365
4,21,55,0,0,0,0,0.447579
5,20,56,0,0,0,0,0.423402
6,20,56,0,0,0,0,0.392983
7,19,58,0,0,0,0,0.372202
0,22,66,0,0,0,0,5.072227
1,22,66,0,0,0,0,0.389874
2,22,66,0,0,0,0,0.375599
4,22,66,0,0,0,0,0.405292
5,22,66,0,0,0,0,0.404367
6,22,66,0,0,0,0,0.394260
7,22,66,0,0,0,0,0.401855
0,24,77,0,0,0,0,5.980508
1,24,77,0,0,0,0,0.388424
2,24,77,0,0,0,0,0.429406
3,23,78,0,0,0,0,0.412908
4,23,78,0,0,0,0,0.428574
5,23,78,0,0,0,0,0.403336
6,22,79,0,0,0,0,0.394216
8,13,89,0,0,0,0,5.377096
1,23,89,0,0,0,0,0.816229
2,23,89,0,0,0,0,0.437396
3,23,89,0,0,0,0,0.399540
4,22,90,0,0,0,0,0.437927
5,22,90,0,0,0,0,0.424814
6,22,90,0,0,0,0,0.411747
7,21,92,0,0,0,0,0.384908
0,25,100,0,0,0,0,6.236974
1,25,100,0,0,0,0,0.403147
3,25,100,0,0,0,0,0.412086
4,25,100,0,0,0,0,0.403978
5,24,102,0,0,0,0,0.398120
6,24,102,0,0,0,0,0.374137
7,24,102,0,0,0,0,0.370625
0,22,102,10,0,0,0,6.692459
1,22,102,10,0,0,0,0.389345
2,22,102,10,0,0,0,0.411086
3,22,102,10,0,0,0,0.404387
4,21,103,10,0,0,0,0.443593
5,21,103,10,0,0,0,0.400221
6,21,103,10,0,0,0,0.414371
0,0,1,0,0,0,0,0.339661
0,1,1,0,0,0,0,0.336554
1,1,1,0,0,0,0,0.338470
2,1,1,0,0,0,0,0.319139
4,1,1,0,0,0,0,0.322158
5,1,1,0,0,0,0,0.411267
6,1,1,0,0,0,0,0.452211
7,1,1,0,0,0,0,0.392227
4,6,1,0,0,0,0,0.599982
5,6,1,0,0,0,0,0.392222
6,6,1,0,0,0,0,0.426607
7,6,1,0,0,0,0,0.450604
0,9,7,0,0,0,0,1.884518
1,9,7,0,0,0,0,0.420226
2,8,8,0,0,0,0,0.395083
3,8,8,0,0,0,0,0.418100
4,7,9,0,0,0,0,0.421611
6,7,9,0,0,0,0,0.415739
7,7,9,0,0,0,0,0.407361
1,14,10,0,0,0,0,2.226791
2,14,10,0,0,0,0,0.401517
3,14,10,0,0,0,0,0.373305
4,13,11,0,0,0,0,0.419741
5,13,11,0,0,0,0,0.409911
6,12,12,0,0,0,0,0.410904
7,12,12,0,0,0,0,0.429305
0,19,15,0,0,0,0,3.586968
2,19,15,0,0,0,0,0.443083
3,18,16,0,0,0,0,0.403899
4,18,16,0,0,0,0,0.427664
5,17,17,0,0,0,0,0.398022
6,16,19,0,0,0,0,0.373106
7,16,19,0,0,0,0,0.381070
0,16,27,0,0,0,0,3.997287
1,16,27,0,0,0,0,0.415576
2,15,29,0,0,0,0,0.395088
3,15,29,0,0,0,0,0.421756
4,15,29,0,0,0,0,0.423345
5,14,30,0,0,0,0,0.443051
6,13,32,0,0,0,0,0.409214
0,21,35,0,0,0,0,3.724305
1,21,35,0,0,0,0,0.394496
2,20,36,0,0,0,0,0.400312
3,20,36,0,0,0,0,0.440494
4,19,36,0,0,0,0,0.401116
5,19,36,0,0,0,0,0.368698
6,19,36,0,0,0,0,0.392624
7,18,37,0,0,0,0,0.421263
0,20,45,0,0,0,0,5.280940
1,20,45,0,0,0,0,0.445995
2,19,46,0,0,0,0,0.427433
3,19,46,0,0,0,0,0.396355
4,19,46,0,0,0,0,0.412447
6,18,47,0,0,0,0,0.425992
7,18,47,0,0,0,0,0.409269
0,22,54,0,0,0,0,4.659271
1,22,54,0,0,0,0,0.353135
2,22,54,0,0,0,0,0.412604
3,22,54,0,0,0,0,0.387365
4,21,55,0,0,0,0,0.447579
5,20,56,0,0,0,0,0.423402
6,20,56,0,0,0,0,0.392983
7,19,58,0,0,0,0,0.372202
0,22,66,0,0,0,0,5.072227
1,22,66,0,0,0,0,0.389874
2,22,66,0,0,0,0,0.375599
4,22,66,0,0,0,0,0.405292
5,22,66,0,0,0,0,0.404367
6,22,66,0,0,0,0,0.394260
7,22,66,0,0,0,0,0.401855
0,24,77,0,0,0,0,5.980508
1,24,77,0,0,0,0,0.388424
2,24,77,0,0,0,0,0.429406
3,23,78,0,0,0,0,0.412908
4,23,78,0,0,0,0,0.428574
5,23,78,0,0,0,0,0.403336
6,22,79,0,0,0,0,0.394216
8,13,89,0,0,0,0,5.377096
1,23,89,0,0,0,0,0.816229
2,23,89,0,0,0,0,0.437396
3,23,89,0,0,0,0,0.399540
4,22,90,0,0,0,0,0.437927
5,22,90,0,0,0,0,0.424814
6,22,90,0,0,0,0,0.411747
7,21,92,0,0,0,0,0.384908
0,25,100,0,0,0,0,6.236974
1,25,100,0,0,0,0,0.403147
3,25,100,0,0,0,0,0.412086
4,25,100,0,0,0,0,0.403978
5,24,102,0,0,0,0,0.398120
6,24,102,0,0,0,0,0.374137
7,24,102,0,0,0,0,0.370625
0,22,102,10,0,0,0,6.692459
1,22,102,10,0,0,0,0.389345
2,22,102,10,0,0,0,0.411086
3,22,102,10,0,0,0,0.404387
4,21,103,10,0,0,0,0.443593
5,21,103,10,0,0,0,0.400221
6,21,103,10,0,0,0,0.414371