Skip to content

Commit

Permalink
format benchmark codes (#181)
Browse files Browse the repository at this point in the history
### What problem were solved in this pull request?

Issue Number: ref #164 

Problem:

### What is changed and how it works?
使用clang-format格式化benchmark模块代码

### Other information
  • Loading branch information
hnwyllmm authored May 24, 2023
1 parent c3bd02e commit f907d01
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 160 deletions.
9 changes: 5 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ Language: Cpp
SortIncludes: false
AccessModifierOffset: -2
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
DerivePointerAlignment: false
PointerAlignment: Right
ConstructorInitializerIndentWidth: 4
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortFunctionsOnASingleLine: false
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
AlwaysBreakTemplateDeclarations: true
# false表示函数实参要么都在同一行,要么都各自一行
BinPackArguments: false
AlwaysBreakBeforeMultilineStrings: false
BreakBeforeBinaryOperators: None
Expand Down
133 changes: 66 additions & 67 deletions benchmark/bplus_tree_concurrency_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,31 @@ using namespace std;
using namespace common;
using namespace benchmark;

once_flag init_bpm_flag;
once_flag init_bpm_flag;
BufferPoolManager bpm{512};

struct Stat
{
int64_t insert_success_count = 0;
int64_t duplicate_count = 0;
int64_t insert_other_count = 0;
int64_t duplicate_count = 0;
int64_t insert_other_count = 0;

int64_t delete_success_count = 0;
int64_t not_exist_count = 0;
int64_t delete_other_count = 0;
int64_t not_exist_count = 0;
int64_t delete_other_count = 0;

int64_t scan_success_count = 0;
int64_t scan_success_count = 0;
int64_t scan_open_failed_count = 0;
int64_t mismatch_count = 0;
int64_t scan_other_count = 0;
int64_t mismatch_count = 0;
int64_t scan_other_count = 0;
};

class BenchmarkBase : public Fixture
{
public:
BenchmarkBase()
{
}
BenchmarkBase() {}

virtual ~BenchmarkBase()
{
BufferPoolManager::set_instance(nullptr);
}
virtual ~BenchmarkBase() { BufferPoolManager::set_instance(nullptr); }

virtual string Name() const = 0;

Expand All @@ -64,23 +59,25 @@ class BenchmarkBase : public Fixture
return;
}

string log_name = this->Name() + ".log";
string log_name = this->Name() + ".log";
string btree_filename = this->Name() + ".btree";
LoggerFactory::init_default(log_name.c_str(), LOG_LEVEL_TRACE);

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

::remove(btree_filename.c_str());

const int internal_max_size = 200;
const int leaf_max_size = 200;
RC rc = handler_.create(btree_filename.c_str(), INTS, sizeof(int32_t)/*attr_len*/,
internal_max_size, leaf_max_size);
const int leaf_max_size = 200;

const char *filename = btree_filename.c_str();

RC rc = handler_.create(filename, INTS, sizeof(int32_t) /*attr_len*/, internal_max_size, leaf_max_size);
if (rc != RC::SUCCESS) {
throw runtime_error("failed to create btree handler");
}
LOG_INFO("test %s setup done. threads=%d, thread index=%d",
this->Name().c_str(), state.threads(), state.thread_index());
LOG_INFO(
"test %s setup done. threads=%d, thread index=%d", this->Name().c_str(), state.threads(), state.thread_index());
}

virtual void TearDown(const State &state)
Expand All @@ -90,15 +87,18 @@ class BenchmarkBase : public Fixture
}

handler_.close();
LOG_INFO("test %s teardown done. threads=%d, thread index=%d",
this->Name().c_str(), state.threads(), state.thread_index());
LOG_INFO("test %s teardown done. threads=%d, thread index=%d",
this->Name().c_str(),
state.threads(),
state.thread_index());
}

void FillUp(uint32_t min, uint32_t max)
{
for (uint32_t value = min; value < max; ++value) {
const char *key = reinterpret_cast<const char *>(&value);
RID rid(value, value);
RID rid(value, value);

[[maybe_unused]] RC rc = handler_.insert_entry(key, &rid);
ASSERT(rc == RC::SUCCESS, "failed to insert entry into btree. key=%" PRIu32, value);
}
Expand All @@ -116,7 +116,8 @@ class BenchmarkBase : public Fixture
void Insert(uint32_t value, Stat &stat)
{
const char *key = reinterpret_cast<const char *>(&value);
RID rid(value, value);
RID rid(value, value);

RC rc = handler_.insert_entry(key, &rid);
switch (rc) {
case RC::SUCCESS: {
Expand All @@ -134,7 +135,8 @@ class BenchmarkBase : public Fixture
void Delete(uint32_t value, Stat &stat)
{
const char *key = reinterpret_cast<const char *>(&value);
RID rid(value, value);
RID rid(value, value);

RC rc = handler_.delete_entry(key, &rid);
switch (rc) {
case RC::SUCCESS: {
Expand All @@ -152,15 +154,16 @@ class BenchmarkBase : public Fixture
void Scan(uint32_t begin, uint32_t end, Stat &stat)
{
const char *begin_key = reinterpret_cast<const char *>(&begin);
const char *end_key = reinterpret_cast<const char *>(&end);
const char *end_key = reinterpret_cast<const char *>(&end);

BplusTreeScanner scanner(handler_);
RC rc = scanner.open(begin_key, sizeof(begin_key), true /*inclusive*/,
end_key, sizeof(end_key), true /*inclusive*/);

RC rc =
scanner.open(begin_key, sizeof(begin_key), true /*inclusive*/, end_key, sizeof(end_key), true /*inclusive*/);
if (rc != RC::SUCCESS) {
stat.scan_open_failed_count++;
} else {
RID rid;
RID rid;
uint32_t count = 0;
while (RC::RECORD_EOF != (rc = scanner.next_entry(rid))) {
count++;
Expand Down Expand Up @@ -189,19 +192,19 @@ struct InsertionBenchmark : public BenchmarkBase
string Name() const override { return "insertion"; }
};

BENCHMARK_DEFINE_F(InsertionBenchmark, Insertion) (State &state)
BENCHMARK_DEFINE_F(InsertionBenchmark, Insertion)(State &state)
{
IntegerGenerator generator(1, 1 << 31);
Stat stat;
Stat stat;

for (auto _ : state) {
uint32_t value = static_cast<uint32_t>(generator.next());
Insert(value, stat);
}

state.counters["success"] = Counter(stat.insert_success_count, Counter::kIsRate);
state.counters["success"] = Counter(stat.insert_success_count, Counter::kIsRate);
state.counters["duplicate"] = Counter(stat.duplicate_count, Counter::kIsRate);
state.counters["other"] = Counter(stat.insert_other_count, Counter::kIsRate);
state.counters["other"] = Counter(stat.insert_other_count, Counter::kIsRate);
}

BENCHMARK_REGISTER_F(InsertionBenchmark, Insertion)->Threads(10);
Expand All @@ -211,7 +214,6 @@ BENCHMARK_REGISTER_F(InsertionBenchmark, Insertion)->Threads(10);
class DeletionBenchmark : public BenchmarkBase
{
public:

string Name() const override { return "deletion"; }

void SetUp(const State &state) override
Expand All @@ -228,30 +230,29 @@ class DeletionBenchmark : public BenchmarkBase
}
};

BENCHMARK_DEFINE_F(DeletionBenchmark, Deletion) (State &state)
BENCHMARK_DEFINE_F(DeletionBenchmark, Deletion)(State &state)
{
uint32_t max = GetRangeMax(state);
uint32_t max = GetRangeMax(state);
IntegerGenerator generator(0, max);
Stat stat;
Stat stat;

for (auto _ : state) {
uint32_t value = static_cast<uint32_t>(generator.next());
Delete(value, stat);
}

state.counters["success"] = Counter(stat.delete_success_count, Counter::kIsRate);
state.counters["success"] = Counter(stat.delete_success_count, Counter::kIsRate);
state.counters["not_exist"] = Counter(stat.not_exist_count, Counter::kIsRate);
state.counters["other"] = Counter(stat.delete_other_count, Counter::kIsRate);
state.counters["other"] = Counter(stat.delete_other_count, Counter::kIsRate);
}

BENCHMARK_REGISTER_F(DeletionBenchmark, Deletion)->Threads(10)->Arg(4* 10000);
BENCHMARK_REGISTER_F(DeletionBenchmark, Deletion)->Threads(10)->Arg(4 * 10000);

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

class ScanBenchmark : public BenchmarkBase
{
public:

string Name() const override { return "scan"; }

void SetUp(const State &state) override
Expand All @@ -268,24 +269,24 @@ class ScanBenchmark : public BenchmarkBase
}
};

BENCHMARK_DEFINE_F(ScanBenchmark, Scan) (State &state)
BENCHMARK_DEFINE_F(ScanBenchmark, Scan)(State &state)
{
int max_range_size = 100;
uint32_t max = GetRangeMax(state);
int max_range_size = 100;
uint32_t max = GetRangeMax(state);
IntegerGenerator begin_generator(1, max - max_range_size);
IntegerGenerator range_generator(1, max_range_size);
Stat stat;
Stat stat;

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

state.counters["success"] = Counter(stat.scan_success_count, Counter::kIsRate);
state.counters["open_failed_count"] = Counter(stat.scan_open_failed_count, Counter::kIsRate);
state.counters["success"] = Counter(stat.scan_success_count, Counter::kIsRate);
state.counters["open_failed_count"] = Counter(stat.scan_open_failed_count, Counter::kIsRate);
state.counters["mismatch_number_count"] = Counter(stat.mismatch_count, Counter::kIsRate);
state.counters["other"] = Counter(stat.scan_other_count, Counter::kIsRate);
state.counters["other"] = Counter(stat.scan_other_count, Counter::kIsRate);
}

BENCHMARK_REGISTER_F(ScanBenchmark, Scan)->Threads(10)->Arg(4 * 10000);
Expand All @@ -297,7 +298,7 @@ struct MixtureBenchmark : public BenchmarkBase
string Name() const override { return "mixture"; }
};

BENCHMARK_DEFINE_F(MixtureBenchmark, Mixture) (State &state)
BENCHMARK_DEFINE_F(MixtureBenchmark, Mixture)(State &state)
{
pair<uint32_t, uint32_t> data_range{0, GetRangeMax(state)};
pair<uint32_t, uint32_t> scan_range{1, 100};
Expand All @@ -311,17 +312,17 @@ BENCHMARK_DEFINE_F(MixtureBenchmark, Mixture) (State &state)
for (auto _ : state) {
int64_t operation_type = operation_generator.next();
switch (operation_type) {
case 0: { // insert
case 0: { // insert
uint32_t value = static_cast<uint32_t>(data_generator.next());
Insert(value, stat);
} break;
case 1: { // delete
case 1: { // delete
uint32_t value = static_cast<uint32_t>(data_generator.next());
Delete(value, stat);
} break;
case 2: { // scan
case 2: { // scan
uint32_t begin = static_cast<uint32_t>(data_generator.next());
uint32_t end = begin + static_cast<uint32_t>(scan_range_generator.next());
uint32_t end = begin + static_cast<uint32_t>(scan_range_generator.next());
Scan(begin, end, stat);
} break;
default: {
Expand All @@ -330,18 +331,16 @@ BENCHMARK_DEFINE_F(MixtureBenchmark, Mixture) (State &state)
}
}

state.counters.insert({
{"insert_success", Counter(stat.insert_success_count, Counter::kIsRate)},
{"insert_other", Counter(stat.insert_other_count, Counter::kIsRate)},
{"insert_duplicate", Counter(stat.duplicate_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)}
});
state.counters.insert({{"insert_success", Counter(stat.insert_success_count, Counter::kIsRate)},
{"insert_other", Counter(stat.insert_other_count, Counter::kIsRate)},
{"insert_duplicate", Counter(stat.duplicate_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)}});
}

BENCHMARK_REGISTER_F(MixtureBenchmark, Mixture)->Threads(10)->Arg(4 * 10000);
Expand Down
11 changes: 3 additions & 8 deletions benchmark/integer_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@ See the Mulan PSL v2 for more details. */
class IntegerGenerator
{
public:
IntegerGenerator(int min, int max)
: distrib_(min, max)
{}
IntegerGenerator(int min, int max) : distrib_(min, max) {}

int next()
{
return distrib_(rd_);
}
int next() { return distrib_(rd_); }

private:
std::random_device rd_;
std::random_device rd_;
std::uniform_int_distribution<> distrib_;
};
Loading

0 comments on commit f907d01

Please sign in to comment.