github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitpage/bitrie_test.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package bitpage 16 17 import ( 18 "bytes" 19 "fmt" 20 "math/rand" 21 "os" 22 "runtime" 23 "testing" 24 "time" 25 ) 26 27 const ( 28 letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 29 ) 30 31 func pMem() { 32 var memStats runtime.MemStats 33 34 runtime.ReadMemStats(&memStats) 35 36 fmt.Printf("Allocated memory: %dMB\n", memStats.Alloc/1024/1024) 37 fmt.Printf("Total memory allocated: %dMB\n", memStats.TotalAlloc/1024/1024) 38 fmt.Printf("Memory obtained from OS: %dMB\n", memStats.Sys/1024/1024) 39 } 40 41 func TestBitrie_String(t *testing.T) { 42 os.Remove("bitrie.db") 43 44 trie := NewBitrie() 45 trie.InitWriter() 46 47 totalNum := 1<<20 + 128 48 keylist := make([][]byte, totalNum) 49 valuelist := make([][]byte, totalNum) 50 gomap := make(map[string][]byte, totalNum) 51 52 for i := 0; i < totalNum; i++ { 53 key := fmt.Sprintf("sorted_key_prefix_%s_bitalosdb_%s_%d", randBytes(1), randBytes(16), i) 54 value := []byte(fmt.Sprintf("value_%d", i)) 55 keylist[i] = []byte(key) 56 valuelist[i] = value 57 } 58 59 pMem() 60 61 for i := 0; i < totalNum; i++ { 62 gomap[string(keylist[i])] = valuelist[i] 63 } 64 65 pMem() 66 67 bt := time.Now() 68 for i := 0; i < totalNum; i++ { 69 if v, ok := gomap[string(keylist[i])]; !ok || !bytes.Equal(v, valuelist[i]) { 70 fmt.Printf("get map i=%d not exist or v=%s error\n", i, v) 71 } 72 } 73 et := time.Since(bt) 74 fmt.Printf("gomap get time cost = %v\n", et) 75 76 bt = time.Now() 77 for i := 0; i < totalNum; i++ { 78 trie.Add(keylist[i], valuelist[i]) 79 } 80 et = time.Since(bt) 81 fmt.Printf("build trie-index time cost = %v; item-count = %d\n", et, trie.length) 82 83 tbl, _ := openTable("bitrie.db", defaultTableOptions) 84 defer func() { 85 os.Remove("bitrie.db") 86 }() 87 88 tblalloc := func(size uint32) uint32 { 89 offset, _ := tbl.alloc(size) 90 return offset 91 } 92 93 tblbytes := func(offset uint32, size uint32) []byte { 94 return tbl.getBytes(offset, size) 95 } 96 97 tblsize := func() uint32 { 98 return tbl.Size() 99 } 100 101 bt = time.Now() 102 trie.Serialize(tblalloc, tblbytes, tblsize) 103 et = time.Since(bt) 104 fmt.Printf("flush trie-index time cost = %v, tbl-size=%d\n", et, tbl.Size()) 105 106 trie.SetReader(tblbytes(0, tbl.Size()), tableDataOffset) 107 108 bt = time.Now() 109 for i := 0; i < totalNum; i++ { 110 if v, ok := trie.Get(keylist[i]); !ok || !bytes.Equal(v, valuelist[i]) { 111 fmt.Printf("get trie-index i=%d not exist or v=%s != %s\n", i, v, valuelist[i]) 112 } 113 } 114 et = time.Since(bt) 115 fmt.Printf("trie-index get time cost = %v\n", et) 116 trie.Finish() 117 } 118 119 func randBytes(n int) []byte { 120 b := make([]byte, n) 121 for i := range b { 122 b[i] = letters[rand.Intn(len(letters))] 123 } 124 return b 125 }