作者: 谢瑞阳 10225101483 徐翔宇 10225101535
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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