小明思考

Just a software engineer
posts - 124, comments - 36, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

leveldb性能分析 - 随机写

Posted on 2012-03-22 17:32 小明 阅读(4154) 评论(0)  编辑  收藏 所属分类: 分布式计算
准备工作:

1. 下载Snappy库
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上编译会出错,修改makefile:
$(CXX) $(LDFLAGS) db/db_bench.o $(LIBOBJECTS) $(TESTUTIL) -o $@

$(CXX) db/db_bench.o $(LIBOBJECTS) $(TESTUTIL) -o $@ $(LDFLAGS)

为了获取更多的信息,我写了一个简单的测试程序来测试写性能。
g++ src/ldbbench.cpp libleveldb.a -I../leveldb/include -o ldb_test -pthread -lsnappy

我隔10w条记录统计一下运行时间和各层level的个数。

#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;
}

得到结果如下:


可以看出 插入时间不稳定,一旦level 0 的文件个数达到8(leveldb在level0 sst file到达8会做流量控制),就会严重的影响插入速度。

数据如下: 前7栏为各level的文件个数,最后一栏为插入时间(单位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




只有注册用户登录后才能发表评论。


网站导航: