10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

186 rivejä
4.6 KiB

  1. // Copyright (c) 2011 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 "leveldb/cache.h"
  5. #include <vector>
  6. #include "util/coding.h"
  7. #include "util/testharness.h"
  8. namespace leveldb {
  9. // Conversions between numeric keys/values and the types expected by Cache.
  10. static std::string EncodeKey(int k) {
  11. std::string result;
  12. PutFixed32(&result, k);
  13. return result;
  14. }
  15. static int DecodeKey(const Slice& k) {
  16. assert(k.size() == 4);
  17. return DecodeFixed32(k.data());
  18. }
  19. static void* EncodeValue(uintptr_t v) { return reinterpret_cast<void*>(v); }
  20. static int DecodeValue(void* v) { return reinterpret_cast<uintptr_t>(v); }
  21. class CacheTest {
  22. public:
  23. static CacheTest* current_;
  24. static void Deleter(const Slice& key, void* v) {
  25. current_->deleted_keys_.push_back(DecodeKey(key));
  26. current_->deleted_values_.push_back(DecodeValue(v));
  27. }
  28. static const int kCacheSize = 1000;
  29. std::vector<int> deleted_keys_;
  30. std::vector<int> deleted_values_;
  31. Cache* cache_;
  32. CacheTest() : cache_(NewLRUCache(kCacheSize)) {
  33. current_ = this;
  34. }
  35. ~CacheTest() {
  36. delete cache_;
  37. }
  38. int Lookup(int key) {
  39. Cache::Handle* handle = cache_->Lookup(EncodeKey(key));
  40. const int r = (handle == NULL) ? -1 : DecodeValue(cache_->Value(handle));
  41. if (handle != NULL) {
  42. cache_->Release(handle);
  43. }
  44. return r;
  45. }
  46. void Insert(int key, int value, int charge = 1) {
  47. cache_->Release(cache_->Insert(EncodeKey(key), EncodeValue(value), charge,
  48. &CacheTest::Deleter));
  49. }
  50. void Erase(int key) {
  51. cache_->Erase(EncodeKey(key));
  52. }
  53. };
  54. CacheTest* CacheTest::current_;
  55. TEST(CacheTest, HitAndMiss) {
  56. ASSERT_EQ(-1, Lookup(100));
  57. Insert(100, 101);
  58. ASSERT_EQ(101, Lookup(100));
  59. ASSERT_EQ(-1, Lookup(200));
  60. ASSERT_EQ(-1, Lookup(300));
  61. Insert(200, 201);
  62. ASSERT_EQ(101, Lookup(100));
  63. ASSERT_EQ(201, Lookup(200));
  64. ASSERT_EQ(-1, Lookup(300));
  65. Insert(100, 102);
  66. ASSERT_EQ(102, Lookup(100));
  67. ASSERT_EQ(201, Lookup(200));
  68. ASSERT_EQ(-1, Lookup(300));
  69. ASSERT_EQ(1, deleted_keys_.size());
  70. ASSERT_EQ(100, deleted_keys_[0]);
  71. ASSERT_EQ(101, deleted_values_[0]);
  72. }
  73. TEST(CacheTest, Erase) {
  74. Erase(200);
  75. ASSERT_EQ(0, deleted_keys_.size());
  76. Insert(100, 101);
  77. Insert(200, 201);
  78. Erase(100);
  79. ASSERT_EQ(-1, Lookup(100));
  80. ASSERT_EQ(201, Lookup(200));
  81. ASSERT_EQ(1, deleted_keys_.size());
  82. ASSERT_EQ(100, deleted_keys_[0]);
  83. ASSERT_EQ(101, deleted_values_[0]);
  84. Erase(100);
  85. ASSERT_EQ(-1, Lookup(100));
  86. ASSERT_EQ(201, Lookup(200));
  87. ASSERT_EQ(1, deleted_keys_.size());
  88. }
  89. TEST(CacheTest, EntriesArePinned) {
  90. Insert(100, 101);
  91. Cache::Handle* h1 = cache_->Lookup(EncodeKey(100));
  92. ASSERT_EQ(101, DecodeValue(cache_->Value(h1)));
  93. Insert(100, 102);
  94. Cache::Handle* h2 = cache_->Lookup(EncodeKey(100));
  95. ASSERT_EQ(102, DecodeValue(cache_->Value(h2)));
  96. ASSERT_EQ(0, deleted_keys_.size());
  97. cache_->Release(h1);
  98. ASSERT_EQ(1, deleted_keys_.size());
  99. ASSERT_EQ(100, deleted_keys_[0]);
  100. ASSERT_EQ(101, deleted_values_[0]);
  101. Erase(100);
  102. ASSERT_EQ(-1, Lookup(100));
  103. ASSERT_EQ(1, deleted_keys_.size());
  104. cache_->Release(h2);
  105. ASSERT_EQ(2, deleted_keys_.size());
  106. ASSERT_EQ(100, deleted_keys_[1]);
  107. ASSERT_EQ(102, deleted_values_[1]);
  108. }
  109. TEST(CacheTest, EvictionPolicy) {
  110. Insert(100, 101);
  111. Insert(200, 201);
  112. // Frequently used entry must be kept around
  113. for (int i = 0; i < kCacheSize + 100; i++) {
  114. Insert(1000+i, 2000+i);
  115. ASSERT_EQ(2000+i, Lookup(1000+i));
  116. ASSERT_EQ(101, Lookup(100));
  117. }
  118. ASSERT_EQ(101, Lookup(100));
  119. ASSERT_EQ(-1, Lookup(200));
  120. }
  121. TEST(CacheTest, HeavyEntries) {
  122. // Add a bunch of light and heavy entries and then count the combined
  123. // size of items still in the cache, which must be approximately the
  124. // same as the total capacity.
  125. const int kLight = 1;
  126. const int kHeavy = 10;
  127. int added = 0;
  128. int index = 0;
  129. while (added < 2*kCacheSize) {
  130. const int weight = (index & 1) ? kLight : kHeavy;
  131. Insert(index, 1000+index, weight);
  132. added += weight;
  133. index++;
  134. }
  135. int cached_weight = 0;
  136. for (int i = 0; i < index; i++) {
  137. const int weight = (i & 1 ? kLight : kHeavy);
  138. int r = Lookup(i);
  139. if (r >= 0) {
  140. cached_weight += weight;
  141. ASSERT_EQ(1000+i, r);
  142. }
  143. }
  144. ASSERT_LE(cached_weight, kCacheSize + kCacheSize/10);
  145. }
  146. TEST(CacheTest, NewId) {
  147. uint64_t a = cache_->NewId();
  148. uint64_t b = cache_->NewId();
  149. ASSERT_NE(a, b);
  150. }
  151. } // namespace leveldb
  152. int main(int argc, char** argv) {
  153. return leveldb::test::RunAllTests();
  154. }