小组成员:10215300402-朱维清 & 10222140408 谷杰
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.

130 lines
3.7 KiB

4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
  1. #include <chrono>
  2. #include "gtest/gtest.h"
  3. #include "leveldb/env.h"
  4. #include "leveldb/db.h"
  5. using namespace leveldb;
  6. constexpr int value_size = 2048;
  7. constexpr int data_size = 128 << 20;
  8. Status OpenDB(std::string dbName, DB **db) {
  9. std::string rm_command = "rm -rf " + dbName;
  10. system(rm_command.c_str());
  11. Options options;
  12. options.create_if_missing = true;
  13. return DB::Open(options, dbName, db);
  14. }
  15. void InsertData(DB *db, uint64_t ttl/* second */) {
  16. WriteOptions writeOptions;
  17. int key_num = data_size / value_size;
  18. srand(42);
  19. for (int i = 0; i < key_num; i++) {
  20. int key_ = rand() % key_num+1;
  21. std::string key = std::to_string(key_);
  22. std::string value(value_size, 'a');
  23. db->ttl = ttl;
  24. db->Put(writeOptions, key, value, ttl);
  25. }
  26. // for (int i = 0; i < key_num; i++) {
  27. // int key_ = rand() % key_num+1;
  28. // std::string key = std::to_string(key_ );
  29. // std::string value = "aaaaaa_ts_5678aaaaaa";
  30. // db->Put(writeOptions, key, value);
  31. // }
  32. }
  33. void GetData(DB *db, int size = (1 << 30)) {
  34. ReadOptions readOptions;
  35. int key_num = data_size / value_size;
  36. // 点查
  37. srand(42);
  38. for (int i = 0; i < 100; i++) {
  39. int key_ = rand() % key_num+1;
  40. std::string key = std::to_string(key_);
  41. std::string value;
  42. db->Get(readOptions, key, &value);
  43. }
  44. }
  45. TEST(TestTTL, ReadTTL) {
  46. DB *db;
  47. if(OpenDB("testdb_ReadTTL", &db).ok() == false) {
  48. std::cerr << "open db failed" << std::endl;
  49. abort();
  50. }
  51. uint64_t ttl = 20;
  52. InsertData(db, ttl);
  53. ReadOptions readOptions;
  54. Status status;
  55. int key_num = data_size / value_size;
  56. srand(42);
  57. for (int i = 0; i < 100; i++) {
  58. int key_ = rand() % key_num+1;
  59. std::string key = std::to_string(key_);
  60. std::string value;
  61. status = db->Get(readOptions, key, &value);
  62. uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
  63. std::cout << status.ToString() << " key: " << key << " value: ******" << value.substr(value.find("_ts_")) << " now: " << now << std::endl;
  64. ASSERT_TRUE(status.ok());
  65. }
  66. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  67. for (int i = 0; i < 100; i++) {
  68. int key_ = rand() % key_num+1;
  69. std::string key = std::to_string(key_);
  70. std::string value;
  71. status = db->Get(readOptions, key, &value);
  72. uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
  73. std::cout << status.ToString() << " key: " << key << " value: ******" << value.substr(value.find("_ts_")) << " now: " << now << std::endl;
  74. ASSERT_FALSE(status.ok());
  75. }
  76. }
  77. TEST(TestTTL, CompactionTTL) {
  78. DB *db;
  79. if(OpenDB("testdb_CompactionTTL", &db).ok() == false) {
  80. std::cerr << "open db failed" << OpenDB("testdb_CompactionTTL", &db).ToString() << std::endl;
  81. abort();
  82. }
  83. uint64_t ttl = 20;
  84. InsertData(db, ttl);
  85. leveldb::Range ranges[1];
  86. ranges[0] = leveldb::Range("-", "A");
  87. uint64_t sizes[1];
  88. db->GetApproximateSizes(ranges, 1, sizes);
  89. std::cout << "ApproximateSizes before TTL: " << sizes[0] << std::endl;
  90. ASSERT_GT(sizes[0], 0);
  91. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  92. db->CompactRange(nullptr, nullptr);
  93. // leveldb::Range ranges[1];
  94. ranges[0] = leveldb::Range("-", "A");
  95. // uint64_t sizes[1];
  96. db->GetApproximateSizes(ranges, 1, sizes);
  97. std::cout << "ApproximateSizes after TTL: " << sizes[0] << std::endl;
  98. ASSERT_EQ(sizes[0], 0);
  99. }
  100. int main(int argc, char** argv) {
  101. // All tests currently run with the same read-only file limits.
  102. testing::InitGoogleTest(&argc, argv);
  103. return RUN_ALL_TESTS();
  104. }