github.com/jxskiss/gopkg@v0.17.3/lru/walbuf_test.go (about) 1 package lru 2 3 import ( 4 "fmt" 5 "math/rand" 6 "reflect" 7 "testing" 8 "time" 9 ) 10 11 func init() { 12 rand.Seed(time.Now().UnixNano()) 13 } 14 15 func TestWalbuf(t *testing.T) { 16 wb := &walbuf{} 17 18 copy(wb.b[:8], []uint32{3, 1, 3, 4, 9, 1, 10, 10}) 19 wb.p = 8 20 got := wb.deduplicate() 21 want := []uint32{3, 4, 9, 1, 10} 22 if equal := reflect.DeepEqual(want, got); !equal { 23 t.Log(got) 24 t.Log(want) 25 t.Error("walbuf deduplicate fast path") 26 } 27 28 copy(wb.b[:12], []uint32{3, 1, 3, 4, 9, 1, 10, 10, 6, 9, 5, 10}) 29 wb.p = 12 30 got = wb.deduplicate() 31 want = []uint32{3, 4, 1, 6, 9, 5, 10} 32 if equal := reflect.DeepEqual(want, got); !equal { 33 t.Error("walbuf deduplicate slow path") 34 } 35 } 36 37 func TestFastHashset(t *testing.T) { 38 values := make([]uint32, walBufSize) 39 for i := range values { 40 values[i] = uint32(rand.Int31n(1000)) 41 } 42 43 var setBuf [walSetSize]uint32 44 fastSet := fastHashset(setBuf) 45 for _, x := range values { 46 fastSet.add(x) 47 } 48 49 fmt.Println(values) 50 fmt.Println(setBuf) 51 52 mapSet := make(map[uint32]bool) 53 for _, x := range values { 54 mapSet[x] = true 55 } 56 for _, x := range values { 57 if fastSet.has(x) != mapSet[x] { 58 t.Errorf("got incorrect value from fastHashset") 59 } 60 } 61 } 62 63 func BenchmarkWalbuf(b *testing.B) { 64 cache := NewCache(2000) 65 _ = cache 66 67 values := make([]int64, walBufSize) 68 for i := range values { 69 values[i] = rand.Int63() % walBufSize 70 } 71 for _, v := range values { 72 cache.Set(v, v, 0) 73 } 74 75 b.ResetTimer() 76 b.ReportAllocs() 77 for i := 0; i < b.N; i++ { 78 for _, v := range values { 79 _, _, _ = cache.Get(v) 80 } 81 } 82 }