autocompact_test.cc raw
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
5 #include "db/db_impl.h"
6 #include "leveldb/cache.h"
7 #include "leveldb/db.h"
8 #include "util/testharness.h"
9 #include "util/testutil.h"
10
11 namespace leveldb {
12
13 class AutoCompactTest {
14 public:
15 AutoCompactTest() {
16 dbname_ = test::TmpDir() + "/autocompact_test";
17 tiny_cache_ = NewLRUCache(100);
18 options_.block_cache = tiny_cache_;
19 DestroyDB(dbname_, options_);
20 options_.create_if_missing = true;
21 options_.compression = kNoCompression;
22 ASSERT_OK(DB::Open(options_, dbname_, &db_));
23 }
24
25 ~AutoCompactTest() {
26 delete db_;
27 DestroyDB(dbname_, Options());
28 delete tiny_cache_;
29 }
30
31 std::string Key(int i) {
32 char buf[100];
33 snprintf(buf, sizeof(buf), "key%06d", i);
34 return std::string(buf);
35 }
36
37 uint64_t Size(const Slice& start, const Slice& limit) {
38 Range r(start, limit);
39 uint64_t size;
40 db_->GetApproximateSizes(&r, 1, &size);
41 return size;
42 }
43
44 void DoReads(int n);
45
46 private:
47 std::string dbname_;
48 Cache* tiny_cache_;
49 Options options_;
50 DB* db_;
51 };
52
53 static const int kValueSize = 200 * 1024;
54 static const int kTotalSize = 100 * 1024 * 1024;
55 static const int kCount = kTotalSize / kValueSize;
56
57 // Read through the first n keys repeatedly and check that reads do NOT
58 // trigger compaction (seek compaction is disabled in this fork).
59 void AutoCompactTest::DoReads(int n) {
60 std::string value(kValueSize, 'x');
61 DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
62
63 // Fill database
64 for (int i = 0; i < kCount; i++) {
65 ASSERT_OK(db_->Put(WriteOptions(), Key(i), value));
66 }
67 ASSERT_OK(dbi->TEST_CompactMemTable());
68
69 // Delete everything
70 for (int i = 0; i < kCount; i++) {
71 ASSERT_OK(db_->Delete(WriteOptions(), Key(i)));
72 }
73 ASSERT_OK(dbi->TEST_CompactMemTable());
74
75 // Get initial measurement of the space we will be reading.
76 const int64_t initial_size = Size(Key(0), Key(n));
77 const int64_t initial_other_size = Size(Key(n), Key(kCount));
78
79 // Read repeatedly. The size of the read range must NOT shrink: with
80 // seek compaction disabled, reads never schedule a compaction.
81 std::string limit_key = Key(n);
82 for (int read = 0; read < 100; read++) {
83 Iterator* iter = db_->NewIterator(ReadOptions());
84 for (iter->SeekToFirst();
85 iter->Valid() && iter->key().ToString() < limit_key; iter->Next()) {
86 // Drop data
87 }
88 delete iter;
89 uint64_t size = Size(Key(0), Key(n));
90 fprintf(stderr, "iter %3d => %7.3f MB [other %7.3f MB]\n", read + 1,
91 size / 1048576.0, Size(Key(n), Key(kCount)) / 1048576.0);
92 }
93 // Give any background work a chance to run, even though none should.
94 Env::Default()->SleepForMicroseconds(1000000);
95 ASSERT_EQ(Size(Key(0), Key(n)), static_cast<uint64_t>(initial_size));
96
97 // Verify that the size of the key space not touched by the reads
98 // is pretty much unchanged.
99 const int64_t final_other_size = Size(Key(n), Key(kCount));
100 ASSERT_LE(final_other_size, initial_other_size + 1048576);
101 ASSERT_GE(final_other_size, initial_other_size / 5 - 1048576);
102 }
103
104 TEST(AutoCompactTest, ReadAll) { DoReads(kCount); }
105
106 TEST(AutoCompactTest, ReadHalf) { DoReads(kCount / 2); }
107
108 } // namespace leveldb
109
110 int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }
111