github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/perf/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.Errorf("walbuf deduplicate fast path, want= %v, got= %v", want, got) 26 } 27 28 wb.reset() 29 copy(wb.b[:12], []uint32{3, 1, 3, 4, 9, 1, 10, 10, 6, 9, 5, 10}) 30 wb.p = 12 31 got = wb.deduplicate() 32 want = []uint32{3, 4, 1, 6, 9, 5, 10} 33 if equal := reflect.DeepEqual(want, got); !equal { 34 t.Errorf("walbuf deduplicate slow path, want= %v, got= %v", want, got) 35 } 36 } 37 38 func TestFastHashset(t *testing.T) { 39 values := make([]uint32, walBufSize) 40 for i := range values { 41 values[i] = uint32(rand.Int31n(1000)) 42 } 43 44 var fastSet fastHashset 45 for _, x := range values { 46 fastSet.add(x) 47 } 48 49 fmt.Println(values) 50 fmt.Println(fastSet) 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[int64, int64](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 }