github.com/coocood/badger@v1.5.1-0.20200528065104-c02ac3616d04/cache/sketch_test.go (about) 1 package cache 2 3 import ( 4 "testing" 5 ) 6 7 func TestSketch(t *testing.T) { 8 defer func() { 9 if r := recover(); r == nil { 10 t.Fatal("no panic with bad param numCounters") 11 } 12 }() 13 s := newCmSketch(5) 14 if s.mask != 7 { 15 t.Fatal("not rounding up to next power of 2") 16 } 17 newCmSketch(0) 18 } 19 20 func TestSketchIncrement(t *testing.T) { 21 s := newCmSketch(16) 22 s.Increment(1) 23 s.Increment(5) 24 s.Increment(9) 25 for i := 0; i < cmDepth; i++ { 26 if s.rows[i].string() != s.rows[0].string() { 27 break 28 } 29 if i == cmDepth-1 { 30 t.Fatal("identical rows, bad seeding") 31 } 32 } 33 } 34 35 func TestSketchEstimate(t *testing.T) { 36 s := newCmSketch(16) 37 s.Increment(1) 38 s.Increment(1) 39 if s.Estimate(1) != 2 { 40 t.Fatal("estimate should be 2") 41 } 42 if s.Estimate(0) != 0 { 43 t.Fatal("estimate should be 0") 44 } 45 } 46 47 func TestSketchReset(t *testing.T) { 48 s := newCmSketch(16) 49 s.Increment(1) 50 s.Increment(1) 51 s.Increment(1) 52 s.Increment(1) 53 s.Reset() 54 if s.Estimate(1) != 2 { 55 t.Fatal("reset failed, estimate should be 2") 56 } 57 } 58 59 func TestSketchClear(t *testing.T) { 60 s := newCmSketch(16) 61 for i := 0; i < 16; i++ { 62 s.Increment(uint64(i)) 63 } 64 s.Clear() 65 for i := 0; i < 16; i++ { 66 if s.Estimate(uint64(i)) != 0 { 67 t.Fatal("clear failed") 68 } 69 } 70 } 71 72 func BenchmarkSketchIncrement(b *testing.B) { 73 s := newCmSketch(16) 74 b.SetBytes(1) 75 for n := 0; n < b.N; n++ { 76 s.Increment(1) 77 } 78 } 79 80 func BenchmarkSketchEstimate(b *testing.B) { 81 s := newCmSketch(16) 82 s.Increment(1) 83 b.SetBytes(1) 84 for n := 0; n < b.N; n++ { 85 s.Estimate(1) 86 } 87 }