record_manager_concurrency_test.cpp 11.7 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Wangyunlai on 2023/05/04
//

#include <inttypes.h>
#include <random>
#include <stdexcept>
#include <benchmark/benchmark.h>

#include "storage/record/record_manager.h"
#include "storage/default/disk_buffer_pool.h"
#include "storage/common/condition_filter.h"
#include "storage/trx/vacuous_trx.h"
#include "rc.h"
#include "common/log/log.h"
#include "integer_generator.h"

using namespace std;
using namespace common;
using namespace benchmark;

羽飞's avatar
羽飞 已提交
32
once_flag         init_bpm_flag;
羽飞's avatar
羽飞 已提交
33 34 35 36 37
BufferPoolManager bpm{512};

struct Stat
{
  int64_t insert_success_count = 0;
羽飞's avatar
羽飞 已提交
38
  int64_t insert_other_count   = 0;
羽飞's avatar
羽飞 已提交
39 40

  int64_t delete_success_count = 0;
羽飞's avatar
羽飞 已提交
41 42
  int64_t not_exist_count      = 0;
  int64_t delete_other_count   = 0;
羽飞's avatar
羽飞 已提交
43

羽飞's avatar
羽飞 已提交
44
  int64_t scan_success_count     = 0;
羽飞's avatar
羽飞 已提交
45
  int64_t scan_open_failed_count = 0;
羽飞's avatar
羽飞 已提交
46 47
  int64_t mismatch_count         = 0;
  int64_t scan_other_count       = 0;
羽飞's avatar
羽飞 已提交
48 49 50 51
};

struct TestRecord
{
羽飞's avatar
羽飞 已提交
52
  int32_t int_fields[15];
羽飞's avatar
羽飞 已提交
53 54 55 56 57
};

class TestConditionFilter : public ConditionFilter
{
public:
羽飞's avatar
羽飞 已提交
58
  TestConditionFilter(int32_t begin, int32_t end) : begin_(begin), end_(end) {}
羽飞's avatar
羽飞 已提交
59 60 61

  bool filter(const Record &rec) const override
  {
羽飞's avatar
羽飞 已提交
62 63
    const char *data  = rec.data();
    int32_t     value = *(int32_t *)data;
羽飞's avatar
羽飞 已提交
64 65 66 67 68 69 70 71 72 73 74
    return value >= begin_ && value <= end_;
  }

private:
  int32_t begin_;
  int32_t end_;
};

class BenchmarkBase : public Fixture
{
public:
羽飞's avatar
羽飞 已提交
75
  BenchmarkBase() {}
羽飞's avatar
羽飞 已提交
76

羽飞's avatar
羽飞 已提交
77
  virtual ~BenchmarkBase() { BufferPoolManager::set_instance(nullptr); }
羽飞's avatar
羽飞 已提交
78 79 80 81

  virtual string Name() const = 0;

  string record_filename() const { return this->Name() + ".record"; }
羽飞's avatar
羽飞 已提交
82

羽飞's avatar
羽飞 已提交
83 84 85 86 87 88
  virtual void SetUp(const State &state)
  {
    if (0 != state.thread_index()) {
      return;
    }

羽飞's avatar
羽飞 已提交
89
    string log_name        = this->Name() + ".log";
羽飞's avatar
羽飞 已提交
90 91 92 93 94 95 96 97 98
    string record_filename = this->record_filename();
    LoggerFactory::init_default(log_name.c_str(), LOG_LEVEL_TRACE);

    std::call_once(init_bpm_flag, []() { BufferPoolManager::set_instance(&bpm); });

    ::remove(record_filename.c_str());

    RC rc = bpm.create_file(record_filename.c_str());
    if (rc != RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
99
      LOG_WARN("failed to create record buffer pool file. filename=%s, rc=%s", record_filename.c_str(), strrc(rc));
羽飞's avatar
羽飞 已提交
100 101 102 103 104
      throw runtime_error("failed to create record buffer pool file.");
    }

    rc = bpm.open_file(record_filename.c_str(), buffer_pool_);
    if (rc != RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
105
      LOG_WARN("failed to open record file. filename=%s, rc=%s", record_filename.c_str(), strrc(rc));
羽飞's avatar
羽飞 已提交
106 107
      throw runtime_error("failed to open record file");
    }
羽飞's avatar
羽飞 已提交
108

羽飞's avatar
羽飞 已提交
109 110 111 112 113
    rc = handler_.init(buffer_pool_);
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to init record file handler. rc=%s", strrc(rc));
      throw runtime_error("failed to init record file handler");
    }
羽飞's avatar
羽飞 已提交
114 115
    LOG_INFO(
        "test %s setup done. threads=%d, thread index=%d", this->Name().c_str(), state.threads(), state.thread_index());
羽飞's avatar
羽飞 已提交
116 117 118 119 120 121 122 123 124 125 126
  }

  virtual void TearDown(const State &state)
  {
    if (0 != state.thread_index()) {
      return;
    }

    handler_.close();
    bpm.close_file(this->record_filename().c_str());
    buffer_pool_ = nullptr;
羽飞's avatar
羽飞 已提交
127 128 129 130
    LOG_INFO("test %s teardown done. threads=%d, thread index=%d",
        this->Name().c_str(),
        state.threads(),
        state.thread_index());
羽飞's avatar
羽飞 已提交
131 132 133 134 135
  }

  void FillUp(int32_t min, int32_t max, vector<RID> &rids)
  {
    rids.reserve(max - min);
羽飞's avatar
羽飞 已提交
136 137
    RID             rid;
    TestRecord      record;
羽飞's avatar
羽飞 已提交
138 139 140 141 142 143 144
    vector<int32_t> record_values;
    record_values.reserve(max - min);
    for (int32_t value = min; value < max; ++value) {
      record_values.push_back(value);
    }

    random_device rd;
羽飞's avatar
羽飞 已提交
145
    mt19937       random_generator(rd());
羽飞's avatar
羽飞 已提交
146 147 148
    shuffle(record_values.begin(), record_values.end(), random_generator);

    for (int32_t record_value : record_values) {
羽飞's avatar
羽飞 已提交
149
      record.int_fields[0]   = record_value;
羽飞's avatar
羽飞 已提交
150 151 152 153 154
      [[maybe_unused]] RC rc = handler_.insert_record(reinterpret_cast<const char *>(&record), sizeof(record), &rid);
      ASSERT(rc == RC::SUCCESS, "failed to insert record into record file. record value=%" PRIu32, record_value);
      rids.push_back(rid);
    }

羽飞's avatar
羽飞 已提交
155
    LOG_INFO("fill up done. min=%" PRIu32 ", max=%" PRIu32 ", distance=%" PRIu32, min, max, (max - min));
羽飞's avatar
羽飞 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  }

  uint32_t GetRangeMax(const State &state) const
  {
    uint32_t max = static_cast<uint32_t>(state.range(0) * 3);
    if (max <= 0) {
      max = (1 << 31);
    }
    return max;
  }

  void Insert(int32_t value, Stat &stat, RID &rid)
  {
    TestRecord record;
    record.int_fields[0] = value;
羽飞's avatar
羽飞 已提交
171

羽飞's avatar
羽飞 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    RC rc = handler_.insert_record(reinterpret_cast<const char *>(&record), sizeof(record), &rid);
    switch (rc) {
      case RC::SUCCESS: {
        stat.insert_success_count++;
      } break;
      default: {
        stat.insert_other_count++;
      } break;
    }
  }

  void Delete(const RID &rid, Stat &stat)
  {
    RC rc = handler_.delete_record(&rid);
    switch (rc) {
      case RC::SUCCESS: {
        stat.delete_success_count++;
      } break;
      case RC::RECORD_RECORD_NOT_EXIST: {
        stat.not_exist_count++;
      } break;
      default: {
        stat.delete_other_count++;
      } break;
    }
  }

  void Scan(int32_t begin, int32_t end, Stat &stat)
  {
    TestConditionFilter condition_filter(begin, end);
羽飞's avatar
羽飞 已提交
202 203 204
    RecordFileScanner   scanner;
    VacuousTrx          trx;
    RC rc = scanner.open_scan(nullptr /*table*/, *buffer_pool_, &trx, true /*readonly*/, &condition_filter);
羽飞's avatar
羽飞 已提交
205 206 207
    if (rc != RC::SUCCESS) {
      stat.scan_open_failed_count++;
    } else {
羽飞's avatar
羽飞 已提交
208
      Record  record;
羽飞's avatar
羽飞 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
      int32_t count = 0;
      while (scanner.has_next()) {
        rc = scanner.next(record);
        ASSERT(rc == RC::SUCCESS, "failed to get record, rc=%s", strrc(rc));
        count++;
      }

      if (rc != RC::SUCCESS) {
        stat.scan_other_count++;
      } else if (count != (end - begin + 1)) {
        stat.mismatch_count++;
      } else {
        stat.scan_success_count++;
      }

      scanner.close_scan();
    }
  }

protected:
羽飞's avatar
羽飞 已提交
229 230
  DiskBufferPool   *buffer_pool_ = nullptr;
  RecordFileHandler handler_;
羽飞's avatar
羽飞 已提交
231 232 233 234 235 236 237 238 239
};

////////////////////////////////////////////////////////////////////////////////

struct InsertionBenchmark : public BenchmarkBase
{
  string Name() const override { return "insertion"; }
};

羽飞's avatar
羽飞 已提交
240
BENCHMARK_DEFINE_F(InsertionBenchmark, Insertion)(State &state)
羽飞's avatar
羽飞 已提交
241 242
{
  IntegerGenerator generator(1, 1 << 31);
羽飞's avatar
羽飞 已提交
243
  Stat             stat;
羽飞's avatar
羽飞 已提交
244 245 246 247 248 249 250

  RID rid;
  for (auto _ : state) {
    Insert(generator.next(), stat, rid);
  }

  state.counters["success"] = Counter(stat.insert_success_count, Counter::kIsRate);
羽飞's avatar
羽飞 已提交
251
  state.counters["other"]   = Counter(stat.insert_other_count, Counter::kIsRate);
羽飞's avatar
羽飞 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}

BENCHMARK_REGISTER_F(InsertionBenchmark, Insertion)->Threads(10);

////////////////////////////////////////////////////////////////////////////////

class DeletionBenchmark : public BenchmarkBase
{
public:
  string Name() const override { return "deletion"; }

  void SetUp(const State &state) override
  {
    if (0 != state.thread_index()) {
      return;
    }

    BenchmarkBase::SetUp(state);

    uint32_t max = GetRangeMax(state);
    ASSERT(max > 0, "invalid argument count. %ld", state.range(0));
    FillUp(0, max, rids_);
  }

protected:
  vector<RID> rids_;
};

羽飞's avatar
羽飞 已提交
280
BENCHMARK_DEFINE_F(DeletionBenchmark, Deletion)(State &state)
羽飞's avatar
羽飞 已提交
281 282
{
  IntegerGenerator generator(0, static_cast<int>(rids_.size()));
羽飞's avatar
羽飞 已提交
283
  Stat             stat;
羽飞's avatar
羽飞 已提交
284 285 286

  for (auto _ : state) {
    int32_t value = generator.next();
羽飞's avatar
羽飞 已提交
287
    RID     rid   = rids_[value];
羽飞's avatar
羽飞 已提交
288 289 290
    Delete(rid, stat);
  }

羽飞's avatar
羽飞 已提交
291
  state.counters["success"]   = Counter(stat.delete_success_count, Counter::kIsRate);
羽飞's avatar
羽飞 已提交
292
  state.counters["not_exist"] = Counter(stat.not_exist_count, Counter::kIsRate);
羽飞's avatar
羽飞 已提交
293
  state.counters["other"]     = Counter(stat.delete_other_count, Counter::kIsRate);
羽飞's avatar
羽飞 已提交
294 295
}

羽飞's avatar
羽飞 已提交
296
BENCHMARK_REGISTER_F(DeletionBenchmark, Deletion)->Threads(10)->Arg(4 * 10000);
羽飞's avatar
羽飞 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

////////////////////////////////////////////////////////////////////////////////

class ScanBenchmark : public BenchmarkBase
{
public:
  string Name() const override { return "scan"; }

  void SetUp(const State &state) override
  {
    if (0 != state.thread_index()) {
      return;
    }

    BenchmarkBase::SetUp(state);

    int32_t max = state.range(0) * 3;
    ASSERT(max > 0, "invalid argument count. %ld", state.range(0));
    vector<RID> rids;
    FillUp(0, max, rids);
  }
};

羽飞's avatar
羽飞 已提交
320
BENCHMARK_DEFINE_F(ScanBenchmark, Scan)(State &state)
羽飞's avatar
羽飞 已提交
321
{
羽飞's avatar
羽飞 已提交
322 323
  int              max_range_size = 100;
  uint32_t         max            = GetRangeMax(state);
羽飞's avatar
羽飞 已提交
324 325
  IntegerGenerator begin_generator(1, max - max_range_size);
  IntegerGenerator range_generator(1, max_range_size);
羽飞's avatar
羽飞 已提交
326
  Stat             stat;
羽飞's avatar
羽飞 已提交
327 328 329 330 331 332 333

  for (auto _ : state) {
    int32_t begin = begin_generator.next();
    int32_t end   = begin + range_generator.next();
    Scan(begin, end, stat);
  }

羽飞's avatar
羽飞 已提交
334 335
  state.counters["success"]               = Counter(stat.scan_success_count, Counter::kIsRate);
  state.counters["open_failed_count"]     = Counter(stat.scan_open_failed_count, Counter::kIsRate);
羽飞's avatar
羽飞 已提交
336
  state.counters["mismatch_number_count"] = Counter(stat.mismatch_count, Counter::kIsRate);
羽飞's avatar
羽飞 已提交
337
  state.counters["other"]                 = Counter(stat.scan_other_count, Counter::kIsRate);
羽飞's avatar
羽飞 已提交
338 339 340 341 342 343 344 345 346 347 348
}

BENCHMARK_REGISTER_F(ScanBenchmark, Scan)->Threads(10)->Arg(4 * 10000);

////////////////////////////////////////////////////////////////////////////////

struct MixtureBenchmark : public BenchmarkBase
{
  string Name() const override { return "mixture"; }
};

羽飞's avatar
羽飞 已提交
349
BENCHMARK_DEFINE_F(MixtureBenchmark, Mixture)(State &state)
羽飞's avatar
羽飞 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363
{
  pair<int32_t, int32_t> data_range{0, GetRangeMax(state)};
  pair<int32_t, int32_t> scan_range{1, 100};

  IntegerGenerator data_generator(data_range.first, data_range.second);
  IntegerGenerator scan_range_generator(scan_range.first, scan_range.second);
  IntegerGenerator operation_generator(0, 2);

  Stat stat;

  vector<RID> rids;
  for (auto _ : state) {
    int operation_type = operation_generator.next();
    switch (operation_type) {
羽飞's avatar
羽飞 已提交
364
      case 0: {  // insert
羽飞's avatar
羽飞 已提交
365
        int32_t value = data_generator.next();
羽飞's avatar
羽飞 已提交
366
        RID     rid;
羽飞's avatar
羽飞 已提交
367 368 369 370 371
        Insert(value, stat, rid);
        if (rids.size() < 1000000) {
          rids.push_back(rid);
        }
      } break;
羽飞's avatar
羽飞 已提交
372
      case 1: {  // delete
羽飞's avatar
羽飞 已提交
373 374 375 376 377 378 379 380
        int32_t index = data_generator.next();
        if (!rids.empty()) {
          index %= rids.size();
          RID rid = rids[index];
          rids.erase(rids.begin() + index);
          Delete(rid, stat);
        }
      } break;
羽飞's avatar
羽飞 已提交
381
      case 2: {  // scan
羽飞's avatar
羽飞 已提交
382
        int32_t begin = data_generator.next();
羽飞's avatar
羽飞 已提交
383
        int32_t end   = begin + scan_range_generator.next();
羽飞's avatar
羽飞 已提交
384 385 386 387 388 389 390 391
        Scan(begin, end, stat);
      } break;
      default: {
        ASSERT(false, "should not happen. operation=%ld", operation_type);
      }
    }
  }

羽飞's avatar
羽飞 已提交
392 393 394 395 396 397 398 399 400
  state.counters.insert({{"insert_success", Counter(stat.insert_success_count, Counter::kIsRate)},
      {"insert_other", Counter(stat.insert_other_count, Counter::kIsRate)},
      {"delete_success", Counter(stat.delete_success_count, Counter::kIsRate)},
      {"delete_other", Counter(stat.delete_other_count, Counter::kIsRate)},
      {"delete_not_exist", Counter(stat.not_exist_count, Counter::kIsRate)},
      {"scan_success", Counter(stat.scan_success_count, Counter::kIsRate)},
      {"scan_other", Counter(stat.scan_other_count, Counter::kIsRate)},
      {"scan_mismatch", Counter(stat.mismatch_count, Counter::kIsRate)},
      {"scan_open_failed", Counter(stat.scan_open_failed_count, Counter::kIsRate)}});
羽飞's avatar
羽飞 已提交
401 402 403 404 405 406 407
}

BENCHMARK_REGISTER_F(MixtureBenchmark, Mixture)->Threads(10)->Arg(4 * 10000);

////////////////////////////////////////////////////////////////////////////////

BENCHMARK_MAIN();