github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/list_file_hash_map_sqlite_test.go (about) 1 // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 3 package caches_test 4 5 import ( 6 "github.com/TeaOSLab/EdgeNode/internal/caches" 7 "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" 8 "github.com/TeaOSLab/EdgeNode/internal/zero" 9 "github.com/iwind/TeaGo/Tea" 10 "github.com/iwind/TeaGo/assert" 11 "github.com/iwind/TeaGo/rands" 12 "github.com/iwind/TeaGo/types" 13 stringutil "github.com/iwind/TeaGo/utils/string" 14 "math/big" 15 "runtime" 16 "strconv" 17 "testing" 18 "time" 19 ) 20 21 func TestFileListHashMap_Memory(t *testing.T) { 22 var stat1 = &runtime.MemStats{} 23 runtime.ReadMemStats(stat1) 24 25 var m = caches.NewSQLiteFileListHashMap() 26 m.SetIsAvailable(true) 27 28 for i := 0; i < 1_000_000; i++ { 29 m.Add(stringutil.Md5(types.String(i))) 30 } 31 32 t.Log("added:", m.Len(), "hashes") 33 34 var stat2 = &runtime.MemStats{} 35 runtime.ReadMemStats(stat2) 36 37 t.Log("ready", (stat2.Alloc-stat1.Alloc)/1024/1024, "M") 38 t.Log("remains:", m.Len(), "hashes") 39 } 40 41 func TestFileListHashMap_Memory2(t *testing.T) { 42 var stat1 = &runtime.MemStats{} 43 runtime.ReadMemStats(stat1) 44 45 var m = map[uint64]zero.Zero{} 46 47 for i := 0; i < 1_000_000; i++ { 48 m[uint64(i)] = zero.New() 49 } 50 51 var stat2 = &runtime.MemStats{} 52 runtime.ReadMemStats(stat2) 53 54 t.Log("ready", (stat2.Alloc-stat1.Alloc)/1024/1024, "M") 55 } 56 57 func TestFileListHashMap_BigInt(t *testing.T) { 58 var bigInt = big.NewInt(0) 59 60 for _, s := range []string{"1", "2", "3", "123", "123456"} { 61 var hash = stringutil.Md5(s) 62 63 var bigInt1 = big.NewInt(0) 64 bigInt1.SetString(hash, 16) 65 66 bigInt.SetString(hash, 16) 67 68 t.Log(s, "=>", bigInt1.Uint64(), "hash:", hash, "format:", strconv.FormatUint(bigInt1.Uint64(), 16), strconv.FormatUint(bigInt.Uint64(), 16)) 69 70 if strconv.FormatUint(bigInt1.Uint64(), 16) != strconv.FormatUint(bigInt.Uint64(), 16) { 71 t.Fatal("not equal") 72 } 73 } 74 75 for i := 0; i < 1_000_000; i++ { 76 var hash = stringutil.Md5(types.String(i)) 77 78 var bigInt1 = big.NewInt(0) 79 bigInt1.SetString(hash, 16) 80 81 bigInt.SetString(hash, 16) 82 83 if bigInt1.Uint64() != bigInt.Uint64() { 84 t.Fatal(i, "not equal") 85 } 86 } 87 } 88 89 func TestFileListHashMap_Load(t *testing.T) { 90 if !testutils.IsSingleTesting() { 91 return 92 } 93 94 var list = caches.NewSQLiteFileList(Tea.Root + "/data/cache-index/p1").(*caches.SQLiteFileList) 95 96 defer func() { 97 _ = list.Close() 98 }() 99 100 err := list.Init() 101 if err != nil { 102 t.Fatal(err) 103 } 104 105 var m = caches.NewSQLiteFileListHashMap() 106 var before = time.Now() 107 var db = list.GetDB("abc") 108 err = m.Load(db) 109 if err != nil { 110 t.Fatal(err) 111 } 112 t.Log(time.Since(before).Seconds()*1000, "ms") 113 t.Log("count:", m.Len()) 114 m.Add("abc") 115 116 for _, hash := range []string{"33347bb4441265405347816cad36a0f8", "a", "abc", "123"} { 117 t.Log(hash, "=>", m.Exist(hash)) 118 } 119 } 120 121 func TestFileListHashMap_Delete(t *testing.T) { 122 var a = assert.NewAssertion(t) 123 124 var m = caches.NewSQLiteFileListHashMap() 125 m.SetIsReady(true) 126 m.SetIsAvailable(true) 127 m.Add("a") 128 a.IsTrue(m.Len() == 1) 129 m.Delete("a") 130 a.IsTrue(m.Len() == 0) 131 } 132 133 func TestFileListHashMap_Clean(t *testing.T) { 134 var m = caches.NewSQLiteFileListHashMap() 135 m.SetIsAvailable(true) 136 m.Clean() 137 m.Add("a") 138 } 139 140 func Benchmark_BigInt(b *testing.B) { 141 var hash = stringutil.Md5("123456") 142 b.ResetTimer() 143 144 for i := 0; i < b.N; i++ { 145 var bigInt = big.NewInt(0) 146 bigInt.SetString(hash, 16) 147 _ = bigInt.Uint64() 148 } 149 } 150 151 func BenchmarkFileListHashMap_Exist(b *testing.B) { 152 var m = caches.NewSQLiteFileListHashMap() 153 m.SetIsAvailable(true) 154 m.SetIsReady(true) 155 156 for i := 0; i < 1_000_000; i++ { 157 m.Add(types.String(i)) 158 } 159 160 b.ReportAllocs() 161 b.ResetTimer() 162 163 b.RunParallel(func(pb *testing.PB) { 164 for pb.Next() { 165 m.Add(types.String(rands.Int64())) 166 _ = m.Exist(types.String(rands.Int64())) 167 } 168 }) 169 }