Files
EdgeNode/internal/caches/list_file_hash_map_test.go

165 lines
3.5 KiB
Go
Raw Normal View History

2022-08-20 11:47:57 +08:00
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package caches_test
import (
"github.com/TeaOSLab/EdgeNode/internal/caches"
2022-08-22 09:44:09 +08:00
"github.com/TeaOSLab/EdgeNode/internal/zero"
2022-08-20 11:47:57 +08:00
"github.com/iwind/TeaGo/Tea"
2023-11-06 18:36:11 +08:00
"github.com/iwind/TeaGo/assert"
2023-10-05 17:40:27 +08:00
"github.com/iwind/TeaGo/rands"
2022-08-20 11:47:57 +08:00
"github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string"
2022-08-22 09:44:09 +08:00
"math/big"
2022-08-20 11:47:57 +08:00
"runtime"
2022-08-22 09:44:09 +08:00
"strconv"
2022-08-20 11:47:57 +08:00
"testing"
2022-09-07 11:34:26 +08:00
"time"
2022-08-20 11:47:57 +08:00
)
func TestFileListHashMap_Memory(t *testing.T) {
var stat1 = &runtime.MemStats{}
runtime.ReadMemStats(stat1)
var m = caches.NewFileListHashMap()
2023-10-01 19:48:35 +08:00
m.SetIsAvailable(true)
2022-08-20 11:47:57 +08:00
for i := 0; i < 1_000_000; i++ {
m.Add(stringutil.Md5(types.String(i)))
}
2023-10-01 19:48:35 +08:00
t.Log("added:", m.Len(), "hashes")
2022-08-20 11:47:57 +08:00
var stat2 = &runtime.MemStats{}
runtime.ReadMemStats(stat2)
t.Log("ready", (stat2.Alloc-stat1.Alloc)/1024/1024, "M")
2023-10-01 19:48:35 +08:00
t.Log("remains:", m.Len(), "hashes")
2022-08-20 11:47:57 +08:00
}
2022-08-22 09:44:09 +08:00
func TestFileListHashMap_Memory2(t *testing.T) {
var stat1 = &runtime.MemStats{}
runtime.ReadMemStats(stat1)
var m = map[uint64]zero.Zero{}
for i := 0; i < 1_000_000; i++ {
m[uint64(i)] = zero.New()
}
var stat2 = &runtime.MemStats{}
runtime.ReadMemStats(stat2)
t.Log("ready", (stat2.Alloc-stat1.Alloc)/1024/1024, "M")
}
func TestFileListHashMap_BigInt(t *testing.T) {
2023-10-05 17:40:27 +08:00
var bigInt = big.NewInt(0)
2022-08-22 09:44:09 +08:00
for _, s := range []string{"1", "2", "3", "123", "123456"} {
var hash = stringutil.Md5(s)
2023-10-05 17:40:27 +08:00
var bigInt1 = big.NewInt(0)
bigInt1.SetString(hash, 16)
bigInt.SetString(hash, 16)
t.Log(s, "=>", bigInt1.Uint64(), "hash:", hash, "format:", strconv.FormatUint(bigInt1.Uint64(), 16), strconv.FormatUint(bigInt.Uint64(), 16))
if strconv.FormatUint(bigInt1.Uint64(), 16) != strconv.FormatUint(bigInt.Uint64(), 16) {
t.Fatal("not equal")
}
}
for i := 0; i < 1_000_000; i++ {
var hash = stringutil.Md5(types.String(i))
var bigInt1 = big.NewInt(0)
bigInt1.SetString(hash, 16)
2022-08-22 09:44:09 +08:00
bigInt.SetString(hash, 16)
2023-10-05 17:40:27 +08:00
if bigInt1.Uint64() != bigInt.Uint64() {
t.Fatal(i, "not equal")
}
2022-08-22 09:44:09 +08:00
}
}
2022-08-20 11:47:57 +08:00
func TestFileListHashMap_Load(t *testing.T) {
var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1").(*caches.FileList)
defer func() {
_ = list.Close()
}()
2023-06-07 21:49:42 +08:00
err := list.Init()
if err != nil {
t.Fatal(err)
}
2022-08-20 11:47:57 +08:00
var m = caches.NewFileListHashMap()
2022-09-07 11:34:26 +08:00
var before = time.Now()
var db = list.GetDB("abc")
err = m.Load(db)
2022-08-20 11:47:57 +08:00
if err != nil {
t.Fatal(err)
}
2022-09-07 11:34:26 +08:00
t.Log(time.Since(before).Seconds()*1000, "ms")
t.Log("count:", m.Len())
2022-08-20 11:47:57 +08:00
m.Add("abc")
for _, hash := range []string{"33347bb4441265405347816cad36a0f8", "a", "abc", "123"} {
t.Log(hash, "=>", m.Exist(hash))
}
}
2022-08-22 09:44:09 +08:00
2023-11-06 18:36:11 +08:00
func TestFileListHashMap_Delete(t *testing.T) {
var a = assert.NewAssertion(t)
var m = caches.NewFileListHashMap()
m.SetIsReady(true)
m.SetIsAvailable(true)
m.Add("a")
a.IsTrue(m.Len() == 1)
m.Delete("a")
a.IsTrue(m.Len() == 0)
}
func TestFileListHashMap_Clean(t *testing.T) {
var m = caches.NewFileListHashMap()
m.SetIsAvailable(true)
m.Clean()
m.Add("a")
}
2022-08-22 09:44:09 +08:00
func Benchmark_BigInt(b *testing.B) {
var hash = stringutil.Md5("123456")
b.ResetTimer()
for i := 0; i < b.N; i++ {
var bigInt = big.NewInt(0)
bigInt.SetString(hash, 16)
_ = bigInt.Uint64()
}
}
2023-10-05 17:40:27 +08:00
func BenchmarkFileListHashMap_Exist(b *testing.B) {
var m = caches.NewFileListHashMap()
m.SetIsAvailable(true)
m.SetIsReady(true)
for i := 0; i < 1_000_000; i++ {
m.Add(types.String(i))
}
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
m.Add(types.String(rands.Int64()))
_ = m.Exist(types.String(rands.Int64()))
}
})
}