blob_store_bench.cc 7.1 KB
Newer Older
I
Igor Canadi 已提交
1 2 3 4 5 6 7 8 9 10
#include <cstdio>
#include <vector>
#include <atomic>

#include "rocksdb/env.h"
#include "util/blob_store.h"
#include "util/testutil.h"

#define KB 1024LL
#define MB 1024*1024LL
I
Igor Canadi 已提交
11 12 13 14
// BlobStore does costly asserts to make sure it's running correctly, which
// significantly impacts benchmark runtime.
// NDEBUG will compile out those asserts.
#define NDEBUG
I
Igor Canadi 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 127 128 129 130 131

using namespace rocksdb;
using namespace std;

// used by all threads
uint64_t timeout_sec;
Env *env;
BlobStore* bs;

static std::string RandomString(Random* rnd, uint64_t len) {
  std::string r;
  test::RandomString(rnd, len, &r);
  return r;
}

struct Result {
  uint32_t writes;
  uint32_t reads;
  uint32_t deletes;
  uint64_t data_written;
  uint64_t data_read;

  void print() {
    printf("Total writes = %u\n", writes);
    printf("Total reads = %u\n", reads);
    printf("Total deletes = %u\n", deletes);
    printf("Write throughput = %lf MB/s\n",
           (double)data_written / (1024*1024.0) / timeout_sec);
    printf("Read throughput = %lf MB/s\n",
           (double)data_read / (1024*1024.0) / timeout_sec);
    printf("Total throughput = %lf MB/s\n",
           (double)(data_read + data_written) / (1024*1024.0) / timeout_sec);
  }

  Result() {
    writes = reads = deletes = data_read = data_written = 0;
  }

  Result (uint32_t writes, uint32_t reads, uint32_t deletes,
          uint64_t data_written, uint64_t data_read) :
    writes(writes), reads(reads), deletes(deletes),
    data_written(data_written), data_read(data_read) {}

};

Result operator + (const Result &a, const Result &b) {
  return Result(a.writes + b.writes, a.reads + b.reads,
                a.deletes + b.deletes, a.data_written + b.data_written,
                a.data_read + b.data_read);
}

struct WorkerThread {
  uint64_t data_size_from, data_size_to;
  double read_ratio;
  uint64_t working_set_size; // start deleting once you reach this
  Result result;
  atomic<bool> stopped;

  WorkerThread(uint64_t data_size_from, uint64_t data_size_to,
                double read_ratio, uint64_t working_set_size) :
    data_size_from(data_size_from), data_size_to(data_size_to),
    read_ratio(read_ratio), working_set_size(working_set_size),
    stopped(false) {}

  WorkerThread(const WorkerThread& wt) :
    data_size_from(wt.data_size_from), data_size_to(wt.data_size_to),
    read_ratio(wt.read_ratio), working_set_size(wt.working_set_size),
    stopped(false) {}
};

static void WorkerThreadBody(void* arg) {
  WorkerThread* t = reinterpret_cast<WorkerThread*>(arg);
  Random rnd(5);
  string buf;
  vector<pair<Blob, uint64_t>> blobs;
  vector<string> random_strings;

  for (int i = 0; i < 10; ++i) {
    random_strings.push_back(RandomString(&rnd, t->data_size_to));
  }

  uint64_t total_size = 0;

  uint64_t start_micros = env->NowMicros();
  while (env->NowMicros() - start_micros < timeout_sec * 1000 * 1000) {
    if (blobs.size() && rand() < RAND_MAX * t->read_ratio) {
      // read
      int bi = rand() % blobs.size();
      Status s = bs->Get(blobs[bi].first, &buf);
      assert(s.ok());
      t->result.data_read += buf.size();
      t->result.reads++;
    } else {
      // write
      uint64_t size = rand() % (t->data_size_to - t->data_size_from) +
        t->data_size_from;
      total_size += size;
      string put_str = random_strings[rand() % random_strings.size()];
      blobs.push_back(make_pair(Blob(), size));
      Status s = bs->Put(Slice(put_str.data(), size), &blobs.back().first);
      assert(s.ok());
      t->result.data_written += size;
      t->result.writes++;
    }

    while (total_size >= t->working_set_size) {
      // delete random
      int bi = rand() % blobs.size();
      total_size -= blobs[bi].second;
      bs->Delete(blobs[bi].first);
      blobs.erase(blobs.begin() + bi);
      t->result.deletes++;
    }
  }
  t->stopped.store(true);
}

I
Igor Canadi 已提交
132
Result StartBenchmark(vector<WorkerThread*>& config) {
I
Igor Canadi 已提交
133
  for (auto w : config) {
I
Igor Canadi 已提交
134
    env->StartThread(WorkerThreadBody, w);
I
Igor Canadi 已提交
135 136 137 138 139
  }

  Result result;

  for (auto w : config) {
I
Igor Canadi 已提交
140 141 142 143 144 145
    while (!w->stopped.load());
    result = result + w->result;
  }

  for (auto w : config) {
    delete w;
I
Igor Canadi 已提交
146 147 148 149 150 151 152
  }

  delete bs;

  return result;
}

I
Igor Canadi 已提交
153
vector<WorkerThread*> SetupBenchmarkBalanced() {
I
Igor Canadi 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  string test_path;
  env->GetTestDirectory(&test_path);
  test_path.append("/blob_store");

  // config start
  uint32_t block_size = 16*KB;
  uint32_t file_size = 1*MB;
  double read_write_ratio = 0.5;
  uint64_t data_read_from = 16*KB;
  uint64_t data_read_to = 32*KB;
  int number_of_threads = 10;
  uint64_t working_set_size = 5*MB;
  timeout_sec = 5;
  // config end

  bs = new BlobStore(test_path, block_size, file_size / block_size, 10000, env);

I
Igor Canadi 已提交
171
  vector <WorkerThread*> config;
I
Igor Canadi 已提交
172 173

  for (int i = 0; i < number_of_threads; ++i) {
I
Igor Canadi 已提交
174 175 176 177
    config.push_back(new WorkerThread(data_read_from,
                                      data_read_to,
                                      read_write_ratio,
                                      working_set_size));
I
Igor Canadi 已提交
178 179 180 181 182
  };

  return config;
}

I
Igor Canadi 已提交
183
vector<WorkerThread*> SetupBenchmarkWriteHeavy() {
I
Igor Canadi 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  string test_path;
  env->GetTestDirectory(&test_path);
  test_path.append("/blob_store");

  // config start
  uint32_t block_size = 16*KB;
  uint32_t file_size = 1*MB;
  double read_write_ratio = 0.1;
  uint64_t data_read_from = 16*KB;
  uint64_t data_read_to = 32*KB;
  int number_of_threads = 10;
  uint64_t working_set_size = 5*MB;
  timeout_sec = 5;
  // config end

  bs = new BlobStore(test_path, block_size, file_size / block_size, 10000, env);

I
Igor Canadi 已提交
201
  vector <WorkerThread*> config;
I
Igor Canadi 已提交
202 203

  for (int i = 0; i < number_of_threads; ++i) {
I
Igor Canadi 已提交
204 205 206 207
    config.push_back(new WorkerThread(data_read_from,
                                      data_read_to,
                                      read_write_ratio,
                                      working_set_size));
I
Igor Canadi 已提交
208 209 210 211 212
  };

  return config;
}

I
Igor Canadi 已提交
213
vector<WorkerThread*> SetupBenchmarkReadHeavy() {
I
Igor Canadi 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  string test_path;
  env->GetTestDirectory(&test_path);
  test_path.append("/blob_store");

  // config start
  uint32_t block_size = 16*KB;
  uint32_t file_size = 1*MB;
  double read_write_ratio = 0.9;
  uint64_t data_read_from = 16*KB;
  uint64_t data_read_to = 32*KB;
  int number_of_threads = 10;
  uint64_t working_set_size = 5*MB;
  timeout_sec = 5;
  // config end

  bs = new BlobStore(test_path, block_size, file_size / block_size, 10000, env);

I
Igor Canadi 已提交
231
  vector <WorkerThread*> config;
I
Igor Canadi 已提交
232 233

  for (int i = 0; i < number_of_threads; ++i) {
I
Igor Canadi 已提交
234 235 236 237
    config.push_back(new WorkerThread(data_read_from,
                                      data_read_to,
                                      read_write_ratio,
                                      working_set_size));
I
Igor Canadi 已提交
238 239 240 241 242 243 244 245 246 247 248
  };

  return config;
}

int main(int argc, const char** argv) {
  srand(33);
  env = Env::Default();

  {
    printf("--- Balanced read/write benchmark ---\n");
I
Igor Canadi 已提交
249
    vector <WorkerThread*> config = SetupBenchmarkBalanced();
I
Igor Canadi 已提交
250 251 252 253 254
    Result r = StartBenchmark(config);
    r.print();
  }
  {
    printf("--- Write heavy benchmark ---\n");
I
Igor Canadi 已提交
255
    vector <WorkerThread*> config = SetupBenchmarkWriteHeavy();
I
Igor Canadi 已提交
256 257 258 259 260
    Result r = StartBenchmark(config);
    r.print();
  }
  {
    printf("--- Read heavy benchmark ---\n");
I
Igor Canadi 已提交
261
    vector <WorkerThread*> config = SetupBenchmarkReadHeavy();
I
Igor Canadi 已提交
262 263 264 265 266 267
    Result r = StartBenchmark(config);
    r.print();
  }

  return 0;
}