From b2801ee1a0e3a5c0c393dc04eef63691f79ed694 Mon Sep 17 00:00:00 2001 From: Victor Costan Date: Wed, 29 Dec 2021 03:48:42 +0000 Subject: Extract benchmark from db_test.cc. The benchmark in db/db_test.cc is extracted to its own file, benchmarks/db_bench_log.cc. PiperOrigin-RevId: 418713499 --- CMakeLists.txt | 14 +++---- benchmarks/db_bench_log.cc | 92 ++++++++++++++++++++++++++++++++++++++++++++++ db/db_test.cc | 64 -------------------------------- 3 files changed, 99 insertions(+), 71 deletions(-) create mode 100644 benchmarks/db_bench_log.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index f8285b8..7690302 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -298,11 +298,6 @@ if(LEVELDB_BUILD_TESTS) # This project is tested using GoogleTest. add_subdirectory("third_party/googletest") - # This project uses Google benchmark for benchmarking. - set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE) - set(BENCHMARK_ENABLE_EXCEPTIONS OFF CACHE BOOL "" FORCE) - add_subdirectory("third_party/benchmark") - # GoogleTest triggers a missing field initializers warning. if(LEVELDB_HAVE_NO_MISSING_FIELD_INITIALIZERS) set_property(TARGET gtest @@ -323,7 +318,7 @@ if(LEVELDB_BUILD_TESTS) "${test_file}" ) - target_link_libraries("${test_target_name}" leveldb gmock gtest benchmark) + target_link_libraries("${test_target_name}" leveldb gmock gtest) target_compile_definitions("${test_target_name}" PRIVATE ${LEVELDB_PLATFORM_NAME}=1 @@ -386,6 +381,11 @@ if(LEVELDB_BUILD_TESTS) endif(LEVELDB_BUILD_TESTS) if(LEVELDB_BUILD_BENCHMARKS) + # This project uses Google benchmark for benchmarking. + set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE) + set(BENCHMARK_ENABLE_EXCEPTIONS OFF CACHE BOOL "" FORCE) + add_subdirectory("third_party/benchmark") + function(leveldb_benchmark bench_file) get_filename_component(bench_target_name "${bench_file}" NAME_WE) @@ -400,7 +400,7 @@ if(LEVELDB_BUILD_BENCHMARKS) "${bench_file}" ) - target_link_libraries("${bench_target_name}" leveldb gmock gtest) + target_link_libraries("${bench_target_name}" leveldb gmock gtest benchmark) target_compile_definitions("${bench_target_name}" PRIVATE ${LEVELDB_PLATFORM_NAME}=1 diff --git a/benchmarks/db_bench_log.cc b/benchmarks/db_bench_log.cc new file mode 100644 index 0000000..a1845bf --- /dev/null +++ b/benchmarks/db_bench_log.cc @@ -0,0 +1,92 @@ +// Copyright (c) 2019 The LevelDB Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. See the AUTHORS file for names of contributors. + +#include +#include +#include + +#include "gtest/gtest.h" +#include "benchmark/benchmark.h" +#include "db/version_set.h" +#include "leveldb/comparator.h" +#include "leveldb/db.h" +#include "leveldb/env.h" +#include "leveldb/options.h" +#include "port/port.h" +#include "util/mutexlock.h" +#include "util/testutil.h" + +namespace leveldb { + +namespace { + +std::string MakeKey(unsigned int num) { + char buf[30]; + std::snprintf(buf, sizeof(buf), "%016u", num); + return std::string(buf); +} + +void BM_LogAndApply(benchmark::State& state) { + const int num_base_files = state.range(0); + + std::string dbname = testing::TempDir() + "leveldb_test_benchmark"; + DestroyDB(dbname, Options()); + + DB* db = nullptr; + Options opts; + opts.create_if_missing = true; + Status s = DB::Open(opts, dbname, &db); + ASSERT_LEVELDB_OK(s); + ASSERT_TRUE(db != nullptr); + + delete db; + db = nullptr; + + Env* env = Env::Default(); + + port::Mutex mu; + MutexLock l(&mu); + + InternalKeyComparator cmp(BytewiseComparator()); + Options options; + VersionSet vset(dbname, &options, nullptr, &cmp); + bool save_manifest; + ASSERT_LEVELDB_OK(vset.Recover(&save_manifest)); + VersionEdit vbase; + uint64_t fnum = 1; + for (int i = 0; i < num_base_files; i++) { + InternalKey start(MakeKey(2 * fnum), 1, kTypeValue); + InternalKey limit(MakeKey(2 * fnum + 1), 1, kTypeDeletion); + vbase.AddFile(2, fnum++, 1 /* file size */, start, limit); + } + ASSERT_LEVELDB_OK(vset.LogAndApply(&vbase, &mu)); + + uint64_t start_micros = env->NowMicros(); + + for (auto st : state) { + VersionEdit vedit; + vedit.RemoveFile(2, fnum); + InternalKey start(MakeKey(2 * fnum), 1, kTypeValue); + InternalKey limit(MakeKey(2 * fnum + 1), 1, kTypeDeletion); + vedit.AddFile(2, fnum++, 1 /* file size */, start, limit); + vset.LogAndApply(&vedit, &mu); + } + + uint64_t stop_micros = env->NowMicros(); + unsigned int us = stop_micros - start_micros; + char buf[16]; + std::snprintf(buf, sizeof(buf), "%d", num_base_files); + std::fprintf(stderr, + "BM_LogAndApply/%-6s %8" PRIu64 + " iters : %9u us (%7.0f us / iter)\n", + buf, state.iterations(), us, ((float)us) / state.iterations()); +} + +BENCHMARK(BM_LogAndApply)->Arg(1)->Arg(100)->Arg(10000)->Arg(100000); + +} // namespace + +} // namespace leveldb + +BENCHMARK_MAIN(); diff --git a/db/db_test.cc b/db/db_test.cc index 908b41d..7f22688 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -9,7 +9,6 @@ #include #include "gtest/gtest.h" -#include "benchmark/benchmark.h" #include "db/db_impl.h" #include "db/filename.h" #include "db/version_set.h" @@ -2295,72 +2294,9 @@ TEST_F(DBTest, Randomized) { } while (ChangeOptions()); } -std::string MakeKey(unsigned int num) { - char buf[30]; - std::snprintf(buf, sizeof(buf), "%016u", num); - return std::string(buf); -} - -static void BM_LogAndApply(benchmark::State& state) { - const int num_base_files = state.range(0); - - std::string dbname = testing::TempDir() + "leveldb_test_benchmark"; - DestroyDB(dbname, Options()); - - DB* db = nullptr; - Options opts; - opts.create_if_missing = true; - Status s = DB::Open(opts, dbname, &db); - ASSERT_LEVELDB_OK(s); - ASSERT_TRUE(db != nullptr); - - delete db; - db = nullptr; - - Env* env = Env::Default(); - - port::Mutex mu; - MutexLock l(&mu); - - InternalKeyComparator cmp(BytewiseComparator()); - Options options; - VersionSet vset(dbname, &options, nullptr, &cmp); - bool save_manifest; - ASSERT_LEVELDB_OK(vset.Recover(&save_manifest)); - VersionEdit vbase; - uint64_t fnum = 1; - for (int i = 0; i < num_base_files; i++) { - InternalKey start(MakeKey(2 * fnum), 1, kTypeValue); - InternalKey limit(MakeKey(2 * fnum + 1), 1, kTypeDeletion); - vbase.AddFile(2, fnum++, 1 /* file size */, start, limit); - } - ASSERT_LEVELDB_OK(vset.LogAndApply(&vbase, &mu)); - - uint64_t start_micros = env->NowMicros(); - - for (auto st : state) { - VersionEdit vedit; - vedit.RemoveFile(2, fnum); - InternalKey start(MakeKey(2 * fnum), 1, kTypeValue); - InternalKey limit(MakeKey(2 * fnum + 1), 1, kTypeDeletion); - vedit.AddFile(2, fnum++, 1 /* file size */, start, limit); - vset.LogAndApply(&vedit, &mu); - } - uint64_t stop_micros = env->NowMicros(); - unsigned int us = stop_micros - start_micros; - char buf[16]; - std::snprintf(buf, sizeof(buf), "%d", num_base_files); - std::fprintf(stderr, - "BM_LogAndApply/%-6s %8" PRIu64 - " iters : %9u us (%7.0f us / iter)\n", - buf, state.iterations(), us, ((float)us) / state.iterations()); -} - -BENCHMARK(BM_LogAndApply)->Arg(1)->Arg(100)->Arg(10000)->Arg(100000); } // namespace leveldb int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); - benchmark::RunSpecifiedBenchmarks(); return RUN_ALL_TESTS(); } -- cgit v1.2.1