Files
EdgeNode/internal/caches/list_memory_test.go

269 lines
6.1 KiB
Go
Raw Normal View History

2020-10-04 14:30:42 +08:00
package caches
import (
2020-12-23 21:28:50 +08:00
"fmt"
2023-06-07 21:49:42 +08:00
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
2020-12-23 21:28:50 +08:00
"github.com/cespare/xxhash"
2021-06-11 14:53:51 +08:00
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/rands"
2020-10-04 14:30:42 +08:00
"math/rand"
2020-12-23 21:28:50 +08:00
"strconv"
2020-10-04 14:30:42 +08:00
"testing"
"time"
)
func TestMemoryList_Add(t *testing.T) {
list := NewMemoryList().(*MemoryList)
2021-06-11 14:53:51 +08:00
_ = list.Init()
2021-05-19 12:07:35 +08:00
_ = list.Add("a", &Item{
2021-01-13 12:02:50 +08:00
Key: "a1",
ExpiredAt: time.Now().Unix() + 3600,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-05-19 12:07:35 +08:00
_ = list.Add("b", &Item{
2021-01-13 12:02:50 +08:00
Key: "b1",
ExpiredAt: time.Now().Unix() + 3600,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-06-11 14:53:51 +08:00
_ = list.Add("123456", &Item{
Key: "c1",
ExpiredAt: time.Now().Unix() + 3600,
HeaderSize: 1024,
})
t.Log(list.prefixes)
logs.PrintAsJSON(list.itemMaps, t)
logs.PrintAsJSON(list.weekItemMaps, t)
t.Log(list.Count())
2020-10-04 14:30:42 +08:00
}
func TestMemoryList_Remove(t *testing.T) {
list := NewMemoryList().(*MemoryList)
2021-06-11 14:53:51 +08:00
_ = list.Init()
2021-05-19 12:07:35 +08:00
_ = list.Add("a", &Item{
2021-01-13 12:02:50 +08:00
Key: "a1",
ExpiredAt: time.Now().Unix() + 3600,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-05-19 12:07:35 +08:00
_ = list.Add("b", &Item{
2021-01-13 12:02:50 +08:00
Key: "b1",
ExpiredAt: time.Now().Unix() + 3600,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-05-19 12:07:35 +08:00
_ = list.Remove("b")
2021-06-11 14:53:51 +08:00
list.print(t)
logs.PrintAsJSON(list.weekItemMaps, t)
t.Log(list.Count())
2020-10-04 14:30:42 +08:00
}
func TestMemoryList_Purge(t *testing.T) {
list := NewMemoryList().(*MemoryList)
2021-06-11 14:53:51 +08:00
_ = list.Init()
2021-05-19 12:07:35 +08:00
_ = list.Add("a", &Item{
2021-01-13 12:02:50 +08:00
Key: "a1",
ExpiredAt: time.Now().Unix() + 3600,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-05-19 12:07:35 +08:00
_ = list.Add("b", &Item{
2021-01-13 12:02:50 +08:00
Key: "b1",
ExpiredAt: time.Now().Unix() + 3600,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-05-19 12:07:35 +08:00
_ = list.Add("c", &Item{
2021-01-13 12:02:50 +08:00
Key: "c1",
ExpiredAt: time.Now().Unix() - 3600,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-05-19 12:07:35 +08:00
_ = list.Add("d", &Item{
2021-01-13 12:02:50 +08:00
Key: "d1",
ExpiredAt: time.Now().Unix() - 2,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
_, _ = list.Purge(100, func(hash string) error {
2020-10-04 14:30:42 +08:00
t.Log("delete:", hash)
2021-05-19 12:07:35 +08:00
return nil
2020-10-04 14:30:42 +08:00
})
2021-06-11 14:53:51 +08:00
list.print(t)
logs.PrintAsJSON(list.weekItemMaps, t)
2021-06-11 14:53:51 +08:00
for i := 0; i < 1000; i++ {
_, _ = list.Purge(100, func(hash string) error {
2021-06-11 14:53:51 +08:00
t.Log("delete:", hash)
return nil
})
t.Log(list.purgeIndex)
}
t.Log(list.Count())
2021-06-11 14:53:51 +08:00
}
func TestMemoryList_Purge_Large_List(t *testing.T) {
list := NewMemoryList().(*MemoryList)
_ = list.Init()
for i := 0; i < 1_000_000; i++ {
_ = list.Add("a"+strconv.Itoa(i), &Item{
Key: "a" + strconv.Itoa(i),
ExpiredAt: time.Now().Unix() + int64(rands.Int(0, 24*3600)),
HeaderSize: 1024,
})
}
2023-06-07 21:49:42 +08:00
if testutils.IsSingleTesting() {
time.Sleep(1 * time.Hour)
}
2020-10-04 14:30:42 +08:00
}
func TestMemoryList_Stat(t *testing.T) {
list := NewMemoryList()
2021-06-11 14:53:51 +08:00
_ = list.Init()
2021-05-19 12:07:35 +08:00
_ = list.Add("a", &Item{
2021-01-13 12:02:50 +08:00
Key: "a1",
ExpiredAt: time.Now().Unix() + 3600,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-05-19 12:07:35 +08:00
_ = list.Add("b", &Item{
2021-01-13 12:02:50 +08:00
Key: "b1",
ExpiredAt: time.Now().Unix() + 3600,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-05-19 12:07:35 +08:00
_ = list.Add("c", &Item{
2021-01-13 12:02:50 +08:00
Key: "c1",
ExpiredAt: time.Now().Unix(),
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-05-19 12:07:35 +08:00
_ = list.Add("d", &Item{
2021-01-13 12:02:50 +08:00
Key: "d1",
ExpiredAt: time.Now().Unix() - 2,
HeaderSize: 1024,
2020-10-04 14:30:42 +08:00
})
2021-05-19 12:07:35 +08:00
result, _ := list.Stat(func(hash string) bool {
2020-10-04 14:30:42 +08:00
// 随机测试
rand.Seed(time.Now().UnixNano())
return rand.Int()%2 == 0
})
t.Log(result)
}
2020-12-23 21:28:50 +08:00
2021-06-13 17:37:57 +08:00
func TestMemoryList_CleanPrefix(t *testing.T) {
list := NewMemoryList()
2021-06-11 14:53:51 +08:00
_ = list.Init()
2020-12-23 21:28:50 +08:00
before := time.Now()
for i := 0; i < 1_000_000; i++ {
key := "https://www.teaos.cn/hello/" + strconv.Itoa(i/10000) + "/" + strconv.Itoa(i) + ".html"
2021-05-19 12:07:35 +08:00
_ = list.Add(fmt.Sprintf("%d", xxhash.Sum64String(key)), &Item{
2021-01-13 12:02:50 +08:00
Key: key,
2021-06-13 17:37:57 +08:00
ExpiredAt: time.Now().Unix() + 3600,
2021-01-13 12:02:50 +08:00
BodySize: 0,
HeaderSize: 0,
2020-12-23 21:28:50 +08:00
})
}
t.Log(time.Since(before).Seconds()*1000, "ms")
before = time.Now()
err := list.CleanPrefix("https://www.teaos.cn/hello/10")
2021-05-19 12:07:35 +08:00
if err != nil {
t.Fatal(err)
}
2021-06-13 17:37:57 +08:00
logs.Println(list.Stat(func(hash string) bool {
return true
}))
2020-12-23 21:28:50 +08:00
t.Log(time.Since(before).Seconds()*1000, "ms")
}
func TestMemoryList_PurgeLFU(t *testing.T) {
var list = NewMemoryList().(*MemoryList)
list.minWeek = 2704
var before = time.Now()
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
t.Log("current week:", currentWeek())
_ = list.Add("1", &Item{})
_ = list.Add("2", &Item{})
_ = list.Add("3", &Item{})
_ = list.Add("4", &Item{})
_ = list.Add("5", &Item{})
_ = list.Add("6", &Item{Week: 2704})
_ = list.Add("7", &Item{Week: 2704})
_ = list.Add("8", &Item{Week: 2705})
err := list.PurgeLFU(2, func(hash string) error {
t.Log("purge lfu:", hash)
return nil
})
if err != nil {
t.Fatal(err)
}
t.Log("ok")
logs.PrintAsJSON(list.weekItemMaps, t)
t.Log(list.Count())
}
func TestMemoryList_IncreaseHit(t *testing.T) {
var list = NewMemoryList().(*MemoryList)
var item = &Item{}
item.Week = 2705
item.Week2Hits = 100
_ = list.Add("a", &Item{})
_ = list.Add("a", item)
t.Log("hits1:", item.Week1Hits, "hits2:", item.Week2Hits, "week:", item.Week)
logs.PrintAsJSON(list.weekItemMaps, t)
_ = list.IncreaseHit("a")
t.Log("hits1:", item.Week1Hits, "hits2:", item.Week2Hits, "week:", item.Week)
logs.PrintAsJSON(list.weekItemMaps, t)
_ = list.IncreaseHit("a")
t.Log("hits1:", item.Week1Hits, "hits2:", item.Week2Hits, "week:", item.Week)
logs.PrintAsJSON(list.weekItemMaps, t)
}
func TestMemoryList_CleanAll(t *testing.T) {
var list = NewMemoryList().(*MemoryList)
var item = &Item{}
item.Week = 2705
item.Week2Hits = 100
_ = list.Add("a", &Item{})
_ = list.CleanAll()
logs.PrintAsJSON(list.itemMaps, t)
logs.PrintAsJSON(list.weekItemMaps, t)
t.Log(list.Count())
}
func TestMemoryList_GC(t *testing.T) {
list := NewMemoryList().(*MemoryList)
2021-06-11 14:53:51 +08:00
_ = list.Init()
for i := 0; i < 1_000_000; i++ {
key := "https://www.teaos.cn/hello" + strconv.Itoa(i/100000) + "/" + strconv.Itoa(i) + ".html"
_ = list.Add(fmt.Sprintf("%d", xxhash.Sum64String(key)), &Item{
Key: key,
ExpiredAt: 0,
BodySize: 0,
HeaderSize: 0,
})
}
time.Sleep(10 * time.Second)
2021-06-11 14:53:51 +08:00
t.Log("clean...", len(list.itemMaps))
_ = list.CleanAll()
2021-06-11 14:53:51 +08:00
t.Log("cleanAll...", len(list.itemMaps))
before := time.Now()
//runtime.GC()
t.Log("gc cost:", time.Since(before).Seconds()*1000, "ms")
2023-06-07 21:49:42 +08:00
if testutils.IsSingleTesting() {
timeout := time.NewTimer(2 * time.Minute)
<-timeout.C
t.Log("2 minutes passed")
2023-06-07 21:49:42 +08:00
time.Sleep(30 * time.Minute)
}
}