MCPcopy
hub / github.com/grafana/tempo / TestShardedBloom

Function TestShardedBloom

tempodb/encoding/common/bloom_test.go:15–68  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func TestShardedBloom(t *testing.T) {
16 // create a bunch of traceIDs
17 var err error
18 const numTraces = 10000
19 traceIDs := make([][]byte, 0)
20 for i := 0; i < numTraces; i++ {
21 id := make([]byte, 16)
22 _, err = crand.Read(id)
23 assert.NoError(t, err)
24 traceIDs = append(traceIDs, id)
25 }
26
27 // create sharded bloom filter
28 const bloomFP = .01
29 shardSize := uint(100)
30 estimatedObjects := uint(numTraces)
31 b := NewBloom(bloomFP, shardSize, estimatedObjects)
32
33 // add traceIDs to sharded bloom filter
34 for _, traceID := range traceIDs {
35 b.Add(traceID)
36 }
37
38 // get byte representation
39 bloomBytes, err := b.Marshal()
40 assert.NoError(t, err)
41 assert.Len(t, bloomBytes, b.GetShardCount())
42
43 // parse byte representation into bloom.Bloomfilter
44 var filters []*bloom.BloomFilter
45 for i := 0; i < b.GetShardCount(); i++ {
46 filters = append(filters, &bloom.BloomFilter{})
47 }
48 for i, singleBloom := range bloomBytes {
49 _, err = filters[i].ReadFrom(bytes.NewReader(singleBloom))
50 assert.NoError(t, err)
51
52 // assert that parsed form has the expected size
53 assert.Equal(t, shardSize*8, filters[i].Cap()) // * 8 because need bits from bytes
54 }
55
56 // confirm that the sharded bloom and parsed form give the same result
57 missingCount := 0
58 for _, traceID := range traceIDs {
59 found := b.Test(traceID)
60 if !found {
61 missingCount++
62 }
63 assert.Equal(t, found, filters[ShardKeyForTraceID(traceID, b.GetShardCount())].Test(traceID))
64 }
65
66 // check that missingCount is less than bloomFP
67 assert.LessOrEqual(t, float64(missingCount), bloomFP*numTraces)
68}
69
70func TestShardedBloomFalsePositive(t *testing.T) {
71 tests := []struct {

Callers

nothing calls this directly

Calls 9

NewBloomFunction · 0.85
ShardKeyForTraceIDFunction · 0.85
GetShardCountMethod · 0.80
TestMethod · 0.80
ReadMethod · 0.65
AddMethod · 0.65
MarshalMethod · 0.65
LenMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected