作者: 韩晨旭 10225101440 李畅 10225102463

162 行
3.7 KiB

  1. // Copyright (c) 2012 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/filter_policy.h"
  5. #include "util/coding.h"
  6. #include "util/logging.h"
  7. #include "util/testharness.h"
  8. #include "util/testutil.h"
  9. namespace leveldb {
  10. static const int kVerbose = 1;
  11. static Slice Key(int i, char* buffer) {
  12. EncodeFixed32(buffer, i);
  13. return Slice(buffer, sizeof(uint32_t));
  14. }
  15. class BloomTest {
  16. private:
  17. const FilterPolicy* policy_;
  18. std::string filter_;
  19. std::vector<std::string> keys_;
  20. public:
  21. BloomTest() : policy_(NewBloomFilterPolicy(10)) { }
  22. ~BloomTest() {
  23. delete policy_;
  24. }
  25. void Reset() {
  26. keys_.clear();
  27. filter_.clear();
  28. }
  29. void Add(const Slice& s) {
  30. keys_.push_back(s.ToString());
  31. }
  32. void Build() {
  33. std::vector<Slice> key_slices;
  34. for (size_t i = 0; i < keys_.size(); i++) {
  35. key_slices.push_back(Slice(keys_[i]));
  36. }
  37. filter_.clear();
  38. policy_->CreateFilter(&key_slices[0], static_cast<int>(key_slices.size()),
  39. &filter_);
  40. keys_.clear();
  41. if (kVerbose >= 2) DumpFilter();
  42. }
  43. size_t FilterSize() const {
  44. return filter_.size();
  45. }
  46. void DumpFilter() {
  47. fprintf(stderr, "F(");
  48. for (size_t i = 0; i+1 < filter_.size(); i++) {
  49. const unsigned int c = static_cast<unsigned int>(filter_[i]);
  50. for (int j = 0; j < 8; j++) {
  51. fprintf(stderr, "%c", (c & (1 <<j)) ? '1' : '.');
  52. }
  53. }
  54. fprintf(stderr, ")\n");
  55. }
  56. bool Matches(const Slice& s) {
  57. if (!keys_.empty()) {
  58. Build();
  59. }
  60. return policy_->KeyMayMatch(s, filter_);
  61. }
  62. double FalsePositiveRate() {
  63. char buffer[sizeof(int)];
  64. int result = 0;
  65. for (int i = 0; i < 10000; i++) {
  66. if (Matches(Key(i + 1000000000, buffer))) {
  67. result++;
  68. }
  69. }
  70. return result / 10000.0;
  71. }
  72. };
  73. TEST(BloomTest, EmptyFilter) {
  74. ASSERT_TRUE(! Matches("hello"));
  75. ASSERT_TRUE(! Matches("world"));
  76. }
  77. TEST(BloomTest, Small) {
  78. Add("hello");
  79. Add("world");
  80. ASSERT_TRUE(Matches("hello"));
  81. ASSERT_TRUE(Matches("world"));
  82. ASSERT_TRUE(! Matches("x"));
  83. ASSERT_TRUE(! Matches("foo"));
  84. }
  85. static int NextLength(int length) {
  86. if (length < 10) {
  87. length += 1;
  88. } else if (length < 100) {
  89. length += 10;
  90. } else if (length < 1000) {
  91. length += 100;
  92. } else {
  93. length += 1000;
  94. }
  95. return length;
  96. }
  97. TEST(BloomTest, VaryingLengths) {
  98. char buffer[sizeof(int)];
  99. // Count number of filters that significantly exceed the false positive rate
  100. int mediocre_filters = 0;
  101. int good_filters = 0;
  102. for (int length = 1; length <= 10000; length = NextLength(length)) {
  103. Reset();
  104. for (int i = 0; i < length; i++) {
  105. Add(Key(i, buffer));
  106. }
  107. Build();
  108. ASSERT_LE(FilterSize(), static_cast<size_t>((length * 10 / 8) + 40))
  109. << length;
  110. // All added keys must match
  111. for (int i = 0; i < length; i++) {
  112. ASSERT_TRUE(Matches(Key(i, buffer)))
  113. << "Length " << length << "; key " << i;
  114. }
  115. // Check false positive rate
  116. double rate = FalsePositiveRate();
  117. if (kVerbose >= 1) {
  118. fprintf(stderr, "False positives: %5.2f%% @ length = %6d ; bytes = %6d\n",
  119. rate*100.0, length, static_cast<int>(FilterSize()));
  120. }
  121. ASSERT_LE(rate, 0.02); // Must not be over 2%
  122. if (rate > 0.0125) mediocre_filters++; // Allowed, but not too often
  123. else good_filters++;
  124. }
  125. if (kVerbose >= 1) {
  126. fprintf(stderr, "Filters: %d good, %d mediocre\n",
  127. good_filters, mediocre_filters);
  128. }
  129. ASSERT_LE(mediocre_filters, good_filters/5);
  130. }
  131. // Different bits-per-byte
  132. } // namespace leveldb
  133. int main(int argc, char** argv) {
  134. return leveldb::test::RunAllTests();
  135. }