YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/bloom_filter-test.cc
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#include <glog/logging.h>
33
#include <gtest/gtest.h>
34
35
#include "yb/util/bloom_filter.h"
36
37
namespace yb {
38
39
static const int kRandomSeed = 0xdeadbeef;
40
41
1
static void AddRandomKeys(int random_seed, int n_keys, BloomFilterBuilder *bf) {
42
1
  srandom(random_seed);
43
2.00k
  for (int i = 0; i < n_keys; i++) {
44
2.00k
    uint64_t key = random();
45
2.00k
    Slice key_slice(reinterpret_cast<const uint8_t *>(&key), sizeof(key));
46
2.00k
    BloomKeyProbe probe(key_slice);
47
2.00k
    bf->AddKey(probe);
48
2.00k
  }
49
1
}
50
51
1
static void CheckRandomKeys(int random_seed, int n_keys, const BloomFilter &bf) {
52
1
  srandom(random_seed);
53
2.00k
  for (int i = 0; i < n_keys; i++) {
54
2.00k
    uint64_t key = random();
55
2.00k
    Slice key_slice(reinterpret_cast<const uint8_t *>(&key), sizeof(key));
56
2.00k
    BloomKeyProbe probe(key_slice);
57
2.00k
    ASSERT_TRUE(bf.MayContainKey(probe));
58
2.00k
  }
59
1
}
60
61
1
TEST(TestBloomFilter, TestInsertAndProbe) {
62
1
  int n_keys = 2000;
63
1
  BloomFilterBuilder bfb(
64
1
    BloomFilterSizing::ByCountAndFPRate(n_keys, 0.01));
65
66
  // Check that the desired false positive rate is achieved.
67
1
  double expected_fp_rate = bfb.false_positive_rate();
68
1
  ASSERT_NEAR(expected_fp_rate, 0.01, 0.002);
69
70
  // 1% FP rate should need about 9 bits per key
71
1
  ASSERT_EQ(9, bfb.n_bits() / n_keys);
72
73
  // Enter n_keys random keys into the bloom filter
74
1
  AddRandomKeys(kRandomSeed, n_keys, &bfb);
75
76
  // Verify that the keys we inserted all return true when queried.
77
1
  BloomFilter bf(bfb.slice(), bfb.n_hashes());
78
1
  CheckRandomKeys(kRandomSeed, n_keys, bf);
79
80
  // Query a bunch of other keys, and verify the false positive rate
81
  // is within reasonable bounds.
82
1
  uint32_t num_queries = 100000;
83
1
  uint32_t num_positives = 0;
84
100k
  for (uint32_t i = 0; i < num_queries; i++) {
85
100k
    uint64_t key = random();
86
100k
    Slice key_slice(reinterpret_cast<const uint8_t *>(&key), sizeof(key));
87
100k
    BloomKeyProbe probe(key_slice);
88
100k
    if (bf.MayContainKey(probe)) {
89
994
      num_positives++;
90
994
    }
91
100k
  }
92
93
1
  double fp_rate = static_cast<double>(num_positives) / static_cast<double>(num_queries);
94
1
  LOG(INFO) << "FP rate: " << fp_rate << " (" << num_positives << "/" << num_queries << ")";
95
1
  LOG(INFO) << "Expected FP rate: " << expected_fp_rate;
96
97
  // Actual FP rate should be within 20% of the estimated FP rate
98
1
  ASSERT_NEAR(fp_rate, expected_fp_rate, 0.20*expected_fp_rate);
99
1
}
100
101
} // namespace yb