小明思考

          Just a software engineer
          posts - 124, comments - 36, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          leveldb性能分析 - 隨機寫

          Posted on 2012-03-22 17:32 小明 閱讀(4175) 評論(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



          主站蜘蛛池模板: 日土县| 沙河市| 玉林市| 大港区| 桓台县| 南涧| 新乡县| 阿合奇县| 武宁县| 三穗县| 札达县| 洱源县| 吐鲁番市| 黎川县| 朝阳市| 高邮市| 三门县| 永和县| 博乐市| 广河县| 布尔津县| 渝中区| 彩票| 南郑县| 蕲春县| 永康市| 鄯善县| 沂源县| 崇信县| 长垣县| 汽车| 武义县| 堆龙德庆县| 兴化市| 淮滨县| 德昌县| 陇南市| 襄汾县| 东台市| 宝山区| 开化县|