LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

110 行
3.3 KiB

1ヶ月前
  1. // Copyright (c) 2013 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 "gtest/gtest.h"
  5. #include "db/db_impl.h"
  6. #include "leveldb/cache.h"
  7. #include "leveldb/db.h"
  8. #include "util/testutil.h"
  9. namespace leveldb {
  10. class AutoCompactTest : public testing::Test {
  11. public:
  12. AutoCompactTest() {
  13. dbname_ = testing::TempDir() + "autocompact_test";
  14. tiny_cache_ = NewLRUCache(100);
  15. options_.block_cache = tiny_cache_;
  16. DestroyDB(dbname_, options_);
  17. options_.create_if_missing = true;
  18. options_.compression = kNoCompression;
  19. EXPECT_LEVELDB_OK(DB::Open(options_, dbname_, &db_));
  20. }
  21. ~AutoCompactTest() {
  22. delete db_;
  23. DestroyDB(dbname_, Options());
  24. delete tiny_cache_;
  25. }
  26. std::string Key(int i) {
  27. char buf[100];
  28. std::snprintf(buf, sizeof(buf), "key%06d", i);
  29. return std::string(buf);
  30. }
  31. uint64_t Size(const Slice& start, const Slice& limit) {
  32. Range r(start, limit);
  33. uint64_t size;
  34. db_->GetApproximateSizes(&r, 1, &size);
  35. return size;
  36. }
  37. void DoReads(int n);
  38. private:
  39. std::string dbname_;
  40. Cache* tiny_cache_;
  41. Options options_;
  42. DB* db_;
  43. };
  44. static const int kValueSize = 200 * 1024;
  45. static const int kTotalSize = 100 * 1024 * 1024;
  46. static const int kCount = kTotalSize / kValueSize;
  47. // Read through the first n keys repeatedly and check that they get
  48. // compacted (verified by checking the size of the key space).
  49. void AutoCompactTest::DoReads(int n) {
  50. std::string value(kValueSize, 'x');
  51. DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
  52. // Fill database
  53. for (int i = 0; i < kCount; i++) {
  54. ASSERT_LEVELDB_OK(db_->Put(WriteOptions(), Key(i), value));
  55. }
  56. ASSERT_LEVELDB_OK(dbi->TEST_CompactMemTable());
  57. // Delete everything
  58. for (int i = 0; i < kCount; i++) {
  59. ASSERT_LEVELDB_OK(db_->Delete(WriteOptions(), Key(i)));
  60. }
  61. ASSERT_LEVELDB_OK(dbi->TEST_CompactMemTable());
  62. // Get initial measurement of the space we will be reading.
  63. const int64_t initial_size = Size(Key(0), Key(n));
  64. const int64_t initial_other_size = Size(Key(n), Key(kCount));
  65. // Read until size drops significantly.
  66. std::string limit_key = Key(n);
  67. for (int read = 0; true; read++) {
  68. ASSERT_LT(read, 100) << "Taking too long to compact";
  69. Iterator* iter = db_->NewIterator(ReadOptions());
  70. for (iter->SeekToFirst();
  71. iter->Valid() && iter->key().ToString() < limit_key; iter->Next()) {
  72. // Drop data
  73. }
  74. delete iter;
  75. // Wait a little bit to allow any triggered compactions to complete.
  76. Env::Default()->SleepForMicroseconds(1000000);
  77. uint64_t size = Size(Key(0), Key(n));
  78. std::fprintf(stderr, "iter %3d => %7.3f MB [other %7.3f MB]\n", read + 1,
  79. size / 1048576.0, Size(Key(n), Key(kCount)) / 1048576.0);
  80. if (size <= initial_size / 10) {
  81. break;
  82. }
  83. }
  84. // Verify that the size of the key space not touched by the reads
  85. // is pretty much unchanged.
  86. const int64_t final_other_size = Size(Key(n), Key(kCount));
  87. ASSERT_LE(final_other_size, initial_other_size + 1048576);
  88. ASSERT_GE(final_other_size, initial_other_size / 5 - 1048576);
  89. }
  90. TEST_F(AutoCompactTest, ReadAll) { DoReads(kCount); }
  91. TEST_F(AutoCompactTest, ReadHalf) { DoReads(kCount / 2); }
  92. } // namespace leveldb