Skip to content

Commit

Permalink
Add some benchmarks (vesoft-inc#493)
Browse files Browse the repository at this point in the history
* Add some benchmarks

* Update copyright

* Address dutor's comments

* Address dutor's and darion's comments
  • Loading branch information
dangleptr authored and dutor committed Jun 10, 2019
1 parent 41a92e9 commit 428f430
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/common/base/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,11 @@ nebula_link_libraries(
gtest_main
)
nebula_add_test(nebulakey_utils_test)

add_executable(range_vs_transform_bm RangeVsTransformBenchmark.cpp)
nebula_link_libraries(
range_vs_transform_bm
follybenchmark
boost_regex
)
target_compile_options(range_vs_transform_bm PRIVATE -O3)
130 changes: 130 additions & 0 deletions src/common/base/test/RangeVsTransformBenchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* Copyright (c) 2019 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
#include <folly/init/Init.h>
#include <folly/Benchmark.h>
#include <algorithm>
#include <vector>


BENCHMARK(Test1_RangeTestStr) {
std::vector<int32_t> from;
std::vector<std::string> to;
BENCHMARK_SUSPEND {
from.resize(1000, 0);
to.resize(1000);
}
int32_t index = 0;
for (auto& i : from) {
to[index++] = std::to_string(i);
}
folly::doNotOptimizeAway(to);
}
BENCHMARK_RELATIVE(Test1_TransformStr) {
std::vector<int32_t> from;
std::vector<std::string> to;
BENCHMARK_SUSPEND {
from.resize(1000, 0);
to.resize(1000);
}
std::transform(from.begin(), from.end(), to.begin(), [] (const auto& e) {
return std::to_string(e);
});
folly::doNotOptimizeAway(to);
}

BENCHMARK_DRAW_LINE();

BENCHMARK(Test2_RangeTestInt) {
std::vector<int32_t> from;
std::vector<int32_t> to;
BENCHMARK_SUSPEND {
from.resize(1000, 0);
to.resize(1000);
}
int32_t index = 0;
for (auto& i : from) {
to[index++] = i;
}
folly::doNotOptimizeAway(to);
}
BENCHMARK_RELATIVE(Test2_TransformInt) {
std::vector<int32_t> from;
std::vector<int32_t> to;
BENCHMARK_SUSPEND {
from.resize(1000, 0);
to.resize(1000);
}
std::transform(from.begin(), from.end(), to.begin(), [] (const auto& e) {
return e;
});
folly::doNotOptimizeAway(to);
}

BENCHMARK_DRAW_LINE();

BENCHMARK(Test3_RangeTestInt) {
std::vector<int32_t> v;
BENCHMARK_SUSPEND {
v.resize(1000, 0);
}
for (auto& i : v) {
(void)(i);
int a;
folly::doNotOptimizeAway(a);
}
}

BENCHMARK_RELATIVE(Test3_ForEachInt) {
std::vector<int32_t> v;
BENCHMARK_SUSPEND {
v.resize(1000, 0);
}
std::for_each(v.begin(), v.end(), [] (const auto&) {
int a;
folly::doNotOptimizeAway(a);
});
}

int main(int argc, char** argv) {
folly::init(&argc, &argv, true);
folly::runBenchmarks();
return 0;
}


/*
Intel(R) Xeon(R) CPU E5-2690 v2 @ 3.00GHz*
-O2
============================================================================
RangeVsTransformBenchmark.cpprelative time/iter iters/s
============================================================================
Test1_RangeTestStr 80.60us 12.41K
Test1_TransformStr 100.47% 80.22us 12.47K
----------------------------------------------------------------------------
Test2_RangeTestInt 713.67ns 1.40M
Test2_TransformInt 99.99% 713.72ns 1.40M
----------------------------------------------------------------------------
Test3_RangeTestInt 383.15ns 2.61M
Test3_ForEachInt 100.63% 380.75ns 2.63M
============================================================================
-O3
============================================================================
RangeVsTransformBenchmark.cpprelative time/iter iters/s
============================================================================
Test1_RangeTestStr 84.19us 11.88K
Test1_TransformStr 100.01% 84.18us 11.88K
----------------------------------------------------------------------------
Test2_RangeTestInt 222.70ns 4.49M
Test2_TransformInt 98.90% 225.18ns 4.44M
----------------------------------------------------------------------------
Test3_RangeTestInt 395.93ns 2.53M
Test3_ForEachInt 102.41% 386.61ns 2.59M
============================================================================
*/

15 changes: 15 additions & 0 deletions src/kvstore/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,19 @@ nebula_link_libraries(
)
nebula_add_test(log_encoder_test)

add_executable(
multi_versions_perf_test_bm
MultiVersionBenchmark.cpp
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:fs_obj>
)

nebula_link_libraries(
multi_versions_perf_test_bm
follybenchmark
${ROCKSDB_LIBRARIES}
boost_regex
)

nebula_add_test(multi_versions_perf_test_bm)

111 changes: 111 additions & 0 deletions src/kvstore/test/MultiVersionBenchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* Copyright (c) 2019 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#include "base/Base.h"
#include <gtest/gtest.h>
#include <rocksdb/db.h>
#include <folly/Benchmark.h>
#include "fs/TempDir.h"

DEFINE_bool(do_compact, false, "Do compaction after puts");
DEFINE_int32(versions, 100, "Total versions");

namespace nebula {
namespace kvstore {

std::string genKey(int prefix, int version) {
std::string key;
key.reserve(8);
key.append(reinterpret_cast<const char*>(&prefix), sizeof(prefix));
key.append(reinterpret_cast<const char*>(&version), sizeof(version));
return key;
}

void testFn(bool withVersion) {
rocksdb::DB* db = nullptr;
BENCHMARK_SUSPEND {
fs::TempDir rootPath("/tmp/multi_versions_test.XXXXXX");
rocksdb::Options options;
options.create_if_missing = true;
options.disable_auto_compactions = true;
auto status = rocksdb::DB::Open(options, rootPath.path(), &db);
CHECK(status.ok());
rocksdb::WriteOptions woptions;
for (int i = 0; i < 1000; i++) {
for (int v = 0; v < FLAGS_versions; v++) {
std::string key;
if (withVersion) {
key = genKey(i, v);
} else {
key = genKey(i, 0);
}
auto val = folly::stringPrintf("val_%d_%d", i, v);
db->Put(woptions,
rocksdb::Slice(key.data(), key.size()),
rocksdb::Slice(val.data(), val.size()));
}
}
if (FLAGS_do_compact) {
rocksdb::CompactRangeOptions croptions;
db->CompactRange(croptions, nullptr, nullptr);
}
}
auto start = genKey(0, 0);
rocksdb::ReadOptions roptions;
rocksdb::Iterator* iter = db->NewIterator(roptions);
if (iter) {
iter->Seek(rocksdb::Slice(start));
}

while (iter->Valid()) {
iter->Next();
}
BENCHMARK_SUSPEND {
delete iter;
db->Close();
delete db;
}
}

BENCHMARK(WithVersionTest) {
testFn(true);
}

BENCHMARK(WithOutVersionTest) {
testFn(false);
}

} // namespace kvstore
} // namespace nebula

int main(int argc, char** argv) {
folly::init(&argc, &argv, true);
folly::runBenchmarks();
return 0;
}

/**
Intel(R) Xeon(R) CPU E5-2690 v2 @ 3.00GHz
do_compact = false;
============================================================================
MultiVersionBenchmark.cpprelative time/iter iters/s
============================================================================
WithVersionTest 5.87ms 170.41
WithOutVersionTest 1.67ms 598.23
============================================================================
do_compact = true
============================================================================
MultiVersionBenchmark.cpprelative time/iter iters/s
============================================================================
WithVersionTest 25.44ms 39.30
WithOutVersionTest 329.21us 3.04K
============================================================================
*
*
* */

0 comments on commit 428f430

Please sign in to comment.