作者: 韩晨旭 10225101440 李畅 10225102463
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.8 KiB

  1. #include "gtest/gtest.h"
  2. #include "leveldb/env.h"
  3. #include "leveldb/db.h"
  4. using namespace leveldb;
  5. constexpr int value_size = 2048;
  6. constexpr int data_size = 128 << 20;
  7. Status OpenDB(std::string dbName, DB **db) {
  8. Options options;
  9. options.create_if_missing = true;
  10. return DB::Open(options, dbName, db);
  11. }
  12. void InsertData(DB *db, uint64_t ttl/* second */) {
  13. WriteOptions writeOptions;
  14. int key_num = data_size / value_size;
  15. srand(0);
  16. for (int i = 0; i < key_num; i++) {
  17. int key_ = rand() % key_num+1;
  18. std::string key = std::to_string(key_);
  19. std::string value(value_size, 'a');
  20. db->Put(writeOptions, key, value, ttl);
  21. }
  22. }
  23. void GetData(DB *db, int size = (1 << 30)) {
  24. ReadOptions readOptions;
  25. int key_num = data_size / value_size;
  26. // 点查
  27. srand(0);
  28. for (int i = 0; i < 100; i++) {
  29. int key_ = rand() % key_num+1;
  30. std::string key = std::to_string(key_);
  31. std::string value;
  32. db->Get(readOptions, key, &value);
  33. }
  34. }
  35. TEST(TestTTL, ReadTTL) {
  36. DB *db;
  37. if(OpenDB("testdb", &db).ok() == false) {
  38. std::cerr << "open db failed" << std::endl;
  39. abort();
  40. }
  41. uint64_t ttl = 20;
  42. InsertData(db, ttl);
  43. ReadOptions readOptions;
  44. Status status;
  45. int key_num = data_size / value_size;
  46. srand(0);
  47. for (int i = 0; i < 100; i++) {
  48. int key_ = rand() % key_num+1;
  49. std::string key = std::to_string(key_);
  50. std::string value;
  51. status = db->Get(readOptions, key, &value);
  52. ASSERT_TRUE(status.ok());
  53. }
  54. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  55. for (int i = 0; i < 100; i++) {
  56. int key_ = rand() % key_num+1;
  57. std::string key = std::to_string(key_);
  58. std::string value;
  59. status = db->Get(readOptions, key, &value);
  60. ASSERT_FALSE(status.ok());
  61. }
  62. delete db;
  63. }
  64. TEST(TestTTL, CompactionTTL) {
  65. DB *db;
  66. if(OpenDB("testdb", &db).ok() == false) {
  67. std::cerr << "open db failed" << std::endl;
  68. abort();
  69. }
  70. uint64_t ttl = 20;
  71. InsertData(db, ttl);
  72. leveldb::Range ranges[1];
  73. ranges[0] = leveldb::Range("-", "A");
  74. uint64_t sizes[1];
  75. db->GetApproximateSizes(ranges, 1, sizes);
  76. ASSERT_GT(sizes[0], 0);
  77. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  78. db->CompactRange(nullptr, nullptr);
  79. leveldb::Range ranges1[1];
  80. ranges1[0] = leveldb::Range("-", "A");
  81. uint64_t sizes1[1];
  82. db->GetApproximateSizes(ranges1, 1, sizes1);
  83. ASSERT_EQ(sizes1[0], 0);
  84. delete db;
  85. }
  86. // Time-consuming when kNumLevels is 7, so run this test when kNumLevels is set to 3,
  87. // the codes on branch light_ver is a version with kNumLevels set to 3.
  88. // In this case level 2 is the last level with files.
  89. TEST(TestTTL, LastLevelCompaction) {
  90. DB *db;
  91. if(OpenDB("testdb", &db).ok() == false) {
  92. std::cerr << "open db failed" << std::endl;
  93. abort();
  94. }
  95. uint64_t ttl = 20;
  96. InsertData(db, ttl);
  97. leveldb::Range ranges[1];
  98. ranges[0] = leveldb::Range("-", "A");
  99. uint64_t sizes[1];
  100. db->GetApproximateSizes(ranges, 1, sizes);
  101. ASSERT_GT(sizes[0], 0);
  102. std::string last_level_file_num;
  103. std::string last_level = "2";
  104. db->GetProperty("leveldb.num-files-at-level" + last_level, &last_level_file_num);
  105. std::cout << "File nums in last level: " << last_level_file_num << std::endl;
  106. ASSERT_GT(std::atoi(last_level_file_num.c_str()), 0);
  107. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  108. db->CompactRange(nullptr, nullptr);
  109. leveldb::Range ranges1[1];
  110. ranges1[0] = leveldb::Range("-", "A");
  111. uint64_t sizes1[1];
  112. db->GetApproximateSizes(ranges1, 1, sizes1);
  113. ASSERT_EQ(sizes1[0], 0);
  114. delete db;
  115. }
  116. int main(int argc, char** argv) {
  117. // All tests currently run with the same read-only file limits.
  118. testing::InitGoogleTest(&argc, argv);
  119. return RUN_ALL_TESTS();
  120. }