diff --git a/src/common/base/test/CMakeLists.txt b/src/common/base/test/CMakeLists.txt index 6fd0d068b01..c0ea9f91a93 100644 --- a/src/common/base/test/CMakeLists.txt +++ b/src/common/base/test/CMakeLists.txt @@ -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) diff --git a/src/common/base/test/RangeVsTransformBenchmark.cpp b/src/common/base/test/RangeVsTransformBenchmark.cpp new file mode 100644 index 00000000000..9a43a1111e4 --- /dev/null +++ b/src/common/base/test/RangeVsTransformBenchmark.cpp @@ -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 +#include +#include +#include + + +BENCHMARK(Test1_RangeTestStr) { + std::vector from; + std::vector 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 from; + std::vector 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 from; + std::vector 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 from; + std::vector 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 v; + BENCHMARK_SUSPEND { + v.resize(1000, 0); + } + for (auto& i : v) { + (void)(i); + int a; + folly::doNotOptimizeAway(a); + } +} + +BENCHMARK_RELATIVE(Test3_ForEachInt) { + std::vector 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 +============================================================================ + +*/ + diff --git a/src/kvstore/test/CMakeLists.txt b/src/kvstore/test/CMakeLists.txt index f06451f9fd3..e5b9f0f974c 100644 --- a/src/kvstore/test/CMakeLists.txt +++ b/src/kvstore/test/CMakeLists.txt @@ -157,4 +157,19 @@ nebula_link_libraries( ) nebula_add_test(log_encoder_test) +add_executable( + multi_versions_perf_test_bm + MultiVersionBenchmark.cpp + $ + $ +) + +nebula_link_libraries( + multi_versions_perf_test_bm + follybenchmark + ${ROCKSDB_LIBRARIES} + boost_regex +) + +nebula_add_test(multi_versions_perf_test_bm) diff --git a/src/kvstore/test/MultiVersionBenchmark.cpp b/src/kvstore/test/MultiVersionBenchmark.cpp new file mode 100644 index 00000000000..29b17e3bb46 --- /dev/null +++ b/src/kvstore/test/MultiVersionBenchmark.cpp @@ -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 +#include +#include +#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(&prefix), sizeof(prefix)); + key.append(reinterpret_cast(&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 +============================================================================ + * + * + * */