leveldb
cache_test.cc
Go to the documentation of this file.
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 
5 #include "leveldb/cache.h"
6 
7 #include <vector>
8 #include "util/coding.h"
9 #include "util/testharness.h"
10 
11 namespace leveldb {
12 
13 // Conversions between numeric keys/values and the types expected by Cache.
14 static std::string EncodeKey(int k) {
15  std::string result;
16  PutFixed32(&result, k);
17  return result;
18 }
19 static int DecodeKey(const Slice& k) {
20  assert(k.size() == 4);
21  return DecodeFixed32(k.data());
22 }
23 static void* EncodeValue(uintptr_t v) { return reinterpret_cast<void*>(v); }
24 static int DecodeValue(void* v) { return reinterpret_cast<uintptr_t>(v); }
25 
26 class CacheTest {
27  public:
29 
30  static void Deleter(const Slice& key, void* v) {
31  current_->deleted_keys_.push_back(DecodeKey(key));
32  current_->deleted_values_.push_back(DecodeValue(v));
33  }
34 
35  static const int kCacheSize = 1000;
36  std::vector<int> deleted_keys_;
37  std::vector<int> deleted_values_;
39 
40  CacheTest() : cache_(NewLRUCache(kCacheSize)) {
41  current_ = this;
42  }
43 
45  delete cache_;
46  }
47 
48  int Lookup(int key) {
49  Cache::Handle* handle = cache_->Lookup(EncodeKey(key));
50  const int r = (handle == NULL) ? -1 : DecodeValue(cache_->Value(handle));
51  if (handle != NULL) {
52  cache_->Release(handle);
53  }
54  return r;
55  }
56 
57  void Insert(int key, int value, int charge = 1) {
58  cache_->Release(cache_->Insert(EncodeKey(key), EncodeValue(value), charge,
60  }
61 
62  Cache::Handle* InsertAndReturnHandle(int key, int value, int charge = 1) {
63  return cache_->Insert(EncodeKey(key), EncodeValue(value), charge,
65  }
66 
67  void Erase(int key) {
68  cache_->Erase(EncodeKey(key));
69  }
70 };
72 
73 TEST(CacheTest, HitAndMiss) {
74  ASSERT_EQ(-1, Lookup(100));
75 
76  Insert(100, 101);
77  ASSERT_EQ(101, Lookup(100));
78  ASSERT_EQ(-1, Lookup(200));
79  ASSERT_EQ(-1, Lookup(300));
80 
81  Insert(200, 201);
82  ASSERT_EQ(101, Lookup(100));
83  ASSERT_EQ(201, Lookup(200));
84  ASSERT_EQ(-1, Lookup(300));
85 
86  Insert(100, 102);
87  ASSERT_EQ(102, Lookup(100));
88  ASSERT_EQ(201, Lookup(200));
89  ASSERT_EQ(-1, Lookup(300));
90 
91  ASSERT_EQ(1, deleted_keys_.size());
92  ASSERT_EQ(100, deleted_keys_[0]);
93  ASSERT_EQ(101, deleted_values_[0]);
94 }
95 
97  Erase(200);
98  ASSERT_EQ(0, deleted_keys_.size());
99 
100  Insert(100, 101);
101  Insert(200, 201);
102  Erase(100);
103  ASSERT_EQ(-1, Lookup(100));
104  ASSERT_EQ(201, Lookup(200));
105  ASSERT_EQ(1, deleted_keys_.size());
106  ASSERT_EQ(100, deleted_keys_[0]);
107  ASSERT_EQ(101, deleted_values_[0]);
108 
109  Erase(100);
110  ASSERT_EQ(-1, Lookup(100));
111  ASSERT_EQ(201, Lookup(200));
112  ASSERT_EQ(1, deleted_keys_.size());
113 }
114 
115 TEST(CacheTest, EntriesArePinned) {
116  Insert(100, 101);
117  Cache::Handle* h1 = cache_->Lookup(EncodeKey(100));
118  ASSERT_EQ(101, DecodeValue(cache_->Value(h1)));
119 
120  Insert(100, 102);
121  Cache::Handle* h2 = cache_->Lookup(EncodeKey(100));
122  ASSERT_EQ(102, DecodeValue(cache_->Value(h2)));
123  ASSERT_EQ(0, deleted_keys_.size());
124 
125  cache_->Release(h1);
126  ASSERT_EQ(1, deleted_keys_.size());
127  ASSERT_EQ(100, deleted_keys_[0]);
128  ASSERT_EQ(101, deleted_values_[0]);
129 
130  Erase(100);
131  ASSERT_EQ(-1, Lookup(100));
132  ASSERT_EQ(1, deleted_keys_.size());
133 
134  cache_->Release(h2);
135  ASSERT_EQ(2, deleted_keys_.size());
136  ASSERT_EQ(100, deleted_keys_[1]);
137  ASSERT_EQ(102, deleted_values_[1]);
138 }
139 
140 TEST(CacheTest, EvictionPolicy) {
141  Insert(100, 101);
142  Insert(200, 201);
143  Insert(300, 301);
144  Cache::Handle* h = cache_->Lookup(EncodeKey(300));
145 
146  // Frequently used entry must be kept around,
147  // as must things that are still in use.
148  for (int i = 0; i < kCacheSize + 100; i++) {
149  Insert(1000+i, 2000+i);
150  ASSERT_EQ(2000+i, Lookup(1000+i));
151  ASSERT_EQ(101, Lookup(100));
152  }
153  ASSERT_EQ(101, Lookup(100));
154  ASSERT_EQ(-1, Lookup(200));
155  ASSERT_EQ(301, Lookup(300));
156  cache_->Release(h);
157 }
158 
159 TEST(CacheTest, UseExceedsCacheSize) {
160  // Overfill the cache, keeping handles on all inserted entries.
161  std::vector<Cache::Handle*> h;
162  for (int i = 0; i < kCacheSize + 100; i++) {
163  h.push_back(InsertAndReturnHandle(1000+i, 2000+i));
164  }
165 
166  // Check that all the entries can be found in the cache.
167  for (int i = 0; i < h.size(); i++) {
168  ASSERT_EQ(2000+i, Lookup(1000+i));
169  }
170 
171  for (int i = 0; i < h.size(); i++) {
172  cache_->Release(h[i]);
173  }
174 }
175 
176 TEST(CacheTest, HeavyEntries) {
177  // Add a bunch of light and heavy entries and then count the combined
178  // size of items still in the cache, which must be approximately the
179  // same as the total capacity.
180  const int kLight = 1;
181  const int kHeavy = 10;
182  int added = 0;
183  int index = 0;
184  while (added < 2*kCacheSize) {
185  const int weight = (index & 1) ? kLight : kHeavy;
186  Insert(index, 1000+index, weight);
187  added += weight;
188  index++;
189  }
190 
191  int cached_weight = 0;
192  for (int i = 0; i < index; i++) {
193  const int weight = (i & 1 ? kLight : kHeavy);
194  int r = Lookup(i);
195  if (r >= 0) {
196  cached_weight += weight;
197  ASSERT_EQ(1000+i, r);
198  }
199  }
200  ASSERT_LE(cached_weight, kCacheSize + kCacheSize/10);
201 }
202 
203 TEST(CacheTest, NewId) {
204  uint64_t a = cache_->NewId();
205  uint64_t b = cache_->NewId();
206  ASSERT_NE(a, b);
207 }
208 
209 TEST(CacheTest, Prune) {
210  Insert(1, 100);
211  Insert(2, 200);
212 
213  Cache::Handle* handle = cache_->Lookup(EncodeKey(1));
214  ASSERT_TRUE(handle);
215  cache_->Prune();
216  cache_->Release(handle);
217 
218  ASSERT_EQ(100, Lookup(1));
219  ASSERT_EQ(-1, Lookup(2));
220 }
221 
222 } // namespace leveldb
223 
224 int main(int argc, char** argv) {
226 }
void PutFixed32(std::string *dst, uint32_t value)
Definition: coding.cc:35
static int DecodeKey(const Slice &k)
Definition: cache_test.cc:19
virtual Handle * Insert(const Slice &key, void *value, size_t charge, void(*deleter)(const Slice &key, void *value))=0
virtual uint64_t NewId()=0
static void Deleter(const Slice &key, void *v)
Definition: cache_test.cc:30
#define ASSERT_LE(a, b)
Definition: testharness.h:111
virtual void Release(Handle *handle)=0
virtual void * Value(Handle *handle)=0
static const char * EncodeKey(std::string *scratch, const Slice &target)
Definition: memtable.cc:44
uint32_t DecodeFixed32(const char *ptr)
Definition: coding.h:58
int RunAllTests()
Definition: testharness.cc:36
static int DecodeValue(void *v)
Definition: cache_test.cc:24
virtual Handle * Lookup(const Slice &key)=0
static void * EncodeValue(uintptr_t v)
Definition: cache_test.cc:23
std::vector< int > deleted_values_
Definition: cache_test.cc:37
#define ASSERT_EQ(a, b)
Definition: testharness.h:107
static CacheTest * current_
Definition: cache_test.cc:28
std::vector< int > deleted_keys_
Definition: cache_test.cc:36
void Erase(int key)
Definition: cache_test.cc:67
#define ASSERT_TRUE(c)
Definition: testharness.h:105
TEST(AutoCompactTest, ReadAll)
#define ASSERT_NE(a, b)
Definition: testharness.h:108
Cache * NewLRUCache(size_t capacity)
Definition: cache.cc:401
int main(int argc, char **argv)
Definition: cache_test.cc:224
void Insert(int key, int value, int charge=1)
Definition: cache_test.cc:57
int Lookup(int key)
Definition: cache_test.cc:48
Cache::Handle * InsertAndReturnHandle(int key, int value, int charge=1)
Definition: cache_test.cc:62
static const int kCacheSize
Definition: cache_test.cc:35
virtual void Erase(const Slice &key)=0
const char * data() const
Definition: slice.h:40
size_t size() const
Definition: slice.h:43
virtual void Prune()
Definition: cache.h:89