indexBench.cc 3.7 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3 * or later ("AGPL"), as published by the Free
 * Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#include <gtest/gtest.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <thread>
#include "index.h"
dengyihao's avatar
dengyihao 已提交
21 22 23 24 25 26 27 28
#include "indexCache.h"
#include "indexFst.h"
#include "indexFstUtil.h"
#include "indexInt.h"
#include "indexTfile.h"
#include "indexUtil.h"
#include "tskiplist.h"
#include "tutil.h"
dengyihao's avatar
dengyihao 已提交
29 30
using namespace std;

dengyihao's avatar
dengyihao 已提交
31 32
static std::string logDir = TD_TMP_DIR_PATH "log";

dengyihao's avatar
dengyihao 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
static void initLog() {
  const char   *defaultLogFileNamePrefix = "taoslog";
  const int32_t maxLogFileNum = 10;

  tsAsyncLog = 0;
  idxDebugFlag = 143;
  strcpy(tsLogDir, logDir.c_str());
  taosRemoveDir(tsLogDir);
  taosMkDir(tsLogDir);

  if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) {
    printf("failed to open log file in directory:%s\n", tsLogDir);
  }
}

struct WriteBatch {
  SIndexMultiTerm *terms;
};
class Idx {
 public:
  Idx(int _cacheSize = 1024 * 1024 * 4, const char *_path = "tindex") {
    opts.cacheSize = _cacheSize;
dengyihao's avatar
dengyihao 已提交
55 56
    path += TD_TMP_DIR_PATH;
    path += _path;
dengyihao's avatar
dengyihao 已提交
57 58 59 60
  }
  int SetUp(bool remove) {
    initLog();

dengyihao's avatar
dengyihao 已提交
61
    if (remove) taosRemoveDir(path.c_str());
dengyihao's avatar
dengyihao 已提交
62

dengyihao's avatar
dengyihao 已提交
63
    int ret = indexJsonOpen(&opts, path.c_str(), &index);
dengyihao's avatar
dengyihao 已提交
64 65
    return ret;
  }
dengyihao's avatar
dengyihao 已提交
66
  int Write(WriteBatch *batch, uint64_t uid) {
dengyihao's avatar
dengyihao 已提交
67
    // write batch
dengyihao's avatar
dengyihao 已提交
68
    indexJsonPut(index, batch->terms, uid);
dengyihao's avatar
dengyihao 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    return 0;
  }
  int Read(const char *json, void *key, int64_t *id) {
    // read batch
    return 0;
  }

  void TearDown() { indexJsonClose(index); }

  std::string path;

  SIndexOpts opts;
  SIndex    *index;
};

dengyihao's avatar
dengyihao 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
SIndexTerm *indexTermCreateT(int64_t suid, SIndexOperOnColumn oper, uint8_t colType, const char *colName,
                             int32_t nColName, const char *colVal, int32_t nColVal) {
  char    buf[256] = {0};
  int16_t sz = nColVal;
  memcpy(buf, (uint16_t *)&sz, 2);
  memcpy(buf + 2, colVal, nColVal);
  if (colType == TSDB_DATA_TYPE_BINARY) {
    return indexTermCreate(suid, oper, colType, colName, nColName, buf, sizeof(buf));
  } else {
    return indexTermCreate(suid, oper, colType, colName, nColName, colVal, nColVal);
  }
  return NULL;
}
int initWriteBatch(WriteBatch *wb, int batchSize) {
  SIndexMultiTerm *terms = indexMultiTermCreate();

  std::string colName;
  std::string colVal;

  for (int i = 0; i < 64; i++) {
    colName += '0' + i;
    colVal += '0' + i;
  }

  for (int i = 0; i < batchSize; i++) {
    colVal[i % colVal.size()] = '0' + i % 128;
    colName[i % colName.size()] = '0' + i % 128;
    SIndexTerm *term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
    indexMultiTermAdd(terms, term);
  }

  wb->terms = terms;
  return 0;
}

int BenchWrite(Idx *idx, int batchSize, int limit) {
  for (int i = 0; i < limit; i += batchSize) {
    WriteBatch wb;
    idx->Write(&wb, i);
  }
  return 0;
}
dengyihao's avatar
dengyihao 已提交
127 128 129 130 131 132 133 134 135 136 137

int BenchRead(Idx *idx) { return 0; }

int main() {
  Idx *idx = new Idx;
  if (idx->SetUp(true) != 0) {
    std::cout << "failed to setup index" << std::endl;
    return 0;
  } else {
    std::cout << "succ to setup index" << std::endl;
  }
dengyihao's avatar
dengyihao 已提交
138 139
  BenchWrite(idx, 100, 10000);
  return 1;
dengyihao's avatar
dengyihao 已提交
140
}