您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

92 行
2.5 KiB

3 周前
  1. // Copyright (c) 2019 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include <cinttypes>
  5. #include <cstdio>
  6. #include <string>
  7. #include "gtest/gtest.h"
  8. #include "benchmark/benchmark.h"
  9. #include "db/version_set.h"
  10. #include "leveldb/comparator.h"
  11. #include "leveldb/db.h"
  12. #include "leveldb/env.h"
  13. #include "leveldb/options.h"
  14. #include "port/port.h"
  15. #include "util/mutexlock.h"
  16. #include "util/testutil.h"
  17. namespace leveldb {
  18. namespace {
  19. std::string MakeKey(unsigned int num) {
  20. char buf[30];
  21. std::snprintf(buf, sizeof(buf), "%016u", num);
  22. return std::string(buf);
  23. }
  24. void BM_LogAndApply(benchmark::State& state) {
  25. const int num_base_files = state.range(0);
  26. std::string dbname = testing::TempDir() + "leveldb_test_benchmark";
  27. DestroyDB(dbname, Options());
  28. DB* db = nullptr;
  29. Options opts;
  30. opts.create_if_missing = true;
  31. Status s = DB::Open(opts, dbname, &db);
  32. ASSERT_LEVELDB_OK(s);
  33. ASSERT_TRUE(db != nullptr);
  34. delete db;
  35. db = nullptr;
  36. Env* env = Env::Default();
  37. port::Mutex mu;
  38. MutexLock l(&mu);
  39. InternalKeyComparator cmp(BytewiseComparator());
  40. Options options;
  41. VersionSet vset(dbname, &options, nullptr, &cmp);
  42. bool save_manifest;
  43. ASSERT_LEVELDB_OK(vset.Recover(&save_manifest));
  44. VersionEdit vbase;
  45. uint64_t fnum = 1;
  46. for (int i = 0; i < num_base_files; i++) {
  47. InternalKey start(MakeKey(2 * fnum), 1, kTypeValue);
  48. InternalKey limit(MakeKey(2 * fnum + 1), 1, kTypeDeletion);
  49. vbase.AddFile(2, fnum++, 1 /* file size */, start, limit);
  50. }
  51. ASSERT_LEVELDB_OK(vset.LogAndApply(&vbase, &mu));
  52. uint64_t start_micros = env->NowMicros();
  53. for (auto st : state) {
  54. VersionEdit vedit;
  55. vedit.RemoveFile(2, fnum);
  56. InternalKey start(MakeKey(2 * fnum), 1, kTypeValue);
  57. InternalKey limit(MakeKey(2 * fnum + 1), 1, kTypeDeletion);
  58. vedit.AddFile(2, fnum++, 1 /* file size */, start, limit);
  59. vset.LogAndApply(&vedit, &mu);
  60. }
  61. uint64_t stop_micros = env->NowMicros();
  62. unsigned int us = stop_micros - start_micros;
  63. char buf[16];
  64. std::snprintf(buf, sizeof(buf), "%d", num_base_files);
  65. std::fprintf(stderr,
  66. "BM_LogAndApply/%-6s %8" PRIu64
  67. " iters : %9u us (%7.0f us / iter)\n",
  68. buf, state.iterations(), us, ((float)us) / state.iterations());
  69. }
  70. BENCHMARK(BM_LogAndApply)->Arg(1)->Arg(100)->Arg(10000)->Arg(100000);
  71. } // namespace
  72. } // namespace leveldb
  73. BENCHMARK_MAIN();