2024-01-19 09:27:42 +08:00
|
|
|
package caches_test
|
2020-10-04 14:30:42 +08:00
|
|
|
|
|
|
|
|
import (
|
2020-12-23 21:28:50 +08:00
|
|
|
"fmt"
|
2024-07-27 15:42:50 +08:00
|
|
|
"math/rand"
|
|
|
|
|
"sort"
|
|
|
|
|
"strconv"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2024-01-19 09:27:42 +08:00
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/caches"
|
2023-06-07 21:49:42 +08:00
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
2024-04-18 18:25:33 +08:00
|
|
|
"github.com/cespare/xxhash/v2"
|
2021-06-11 14:53:51 +08:00
|
|
|
"github.com/iwind/TeaGo/logs"
|
|
|
|
|
"github.com/iwind/TeaGo/rands"
|
2023-09-14 18:30:11 +08:00
|
|
|
"github.com/iwind/TeaGo/types"
|
2024-01-19 09:27:42 +08:00
|
|
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
2020-10-04 14:30:42 +08:00
|
|
|
)
|
|
|
|
|
|
2021-06-06 23:42:11 +08:00
|
|
|
func TestMemoryList_Add(t *testing.T) {
|
2024-01-19 09:27:42 +08:00
|
|
|
list := caches.NewMemoryList().(*caches.MemoryList)
|
2021-06-11 14:53:51 +08:00
|
|
|
_ = list.Init()
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("a", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "a1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + 3600,
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("b", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "b1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + 3600,
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("123456", &caches.Item{
|
2021-06-11 14:53:51 +08:00
|
|
|
Key: "c1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + 3600,
|
2021-06-11 14:53:51 +08:00
|
|
|
HeaderSize: 1024,
|
|
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
t.Log(list.Prefixes())
|
|
|
|
|
logs.PrintAsJSON(list.ItemMaps(), t)
|
2021-11-13 21:30:24 +08:00
|
|
|
t.Log(list.Count())
|
2020-10-04 14:30:42 +08:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 23:42:11 +08:00
|
|
|
func TestMemoryList_Remove(t *testing.T) {
|
2024-01-19 09:27:42 +08:00
|
|
|
list := caches.NewMemoryList().(*caches.MemoryList)
|
2021-06-11 14:53:51 +08:00
|
|
|
_ = list.Init()
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("a", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "a1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + 3600,
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("b", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "b1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + 3600,
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2021-05-19 12:07:35 +08:00
|
|
|
_ = list.Remove("b")
|
2024-01-19 09:27:42 +08:00
|
|
|
list.Print(t)
|
2021-11-13 21:30:24 +08:00
|
|
|
t.Log(list.Count())
|
2020-10-04 14:30:42 +08:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 23:42:11 +08:00
|
|
|
func TestMemoryList_Purge(t *testing.T) {
|
2024-01-19 09:27:42 +08:00
|
|
|
list := caches.NewMemoryList().(*caches.MemoryList)
|
2021-06-11 14:53:51 +08:00
|
|
|
_ = list.Init()
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("a", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "a1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + 3600,
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("b", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "b1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + 3600,
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("c", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "c1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() - 3600,
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("d", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "d1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() - 2,
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2021-11-13 21:30:24 +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
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
list.Print(t)
|
2021-06-11 14:53:51 +08:00
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
2021-11-13 21:30:24 +08:00
|
|
|
_, _ = list.Purge(100, func(hash string) error {
|
2021-06-11 14:53:51 +08:00
|
|
|
t.Log("delete:", hash)
|
|
|
|
|
return nil
|
|
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
t.Log(list.PurgeIndex())
|
2021-06-11 14:53:51 +08:00
|
|
|
}
|
2021-11-13 21:30:24 +08:00
|
|
|
|
|
|
|
|
t.Log(list.Count())
|
2021-06-11 14:53:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMemoryList_Purge_Large_List(t *testing.T) {
|
2024-01-21 11:13:30 +08:00
|
|
|
var list = caches.NewMemoryList().(*caches.MemoryList)
|
2021-06-11 14:53:51 +08:00
|
|
|
_ = list.Init()
|
|
|
|
|
|
2024-01-21 11:13:30 +08:00
|
|
|
var count = 100
|
|
|
|
|
if testutils.IsSingleTesting() {
|
|
|
|
|
count = 1_000_000
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("a"+strconv.Itoa(i), &caches.Item{
|
2021-06-11 14:53:51 +08:00
|
|
|
Key: "a" + strconv.Itoa(i),
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + int64(rands.Int(0, 24*3600)),
|
2021-06-11 14:53:51 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-06-06 23:42:11 +08:00
|
|
|
func TestMemoryList_Stat(t *testing.T) {
|
2024-01-19 09:27:42 +08:00
|
|
|
list := caches.NewMemoryList()
|
2021-06-11 14:53:51 +08:00
|
|
|
_ = list.Init()
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("a", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "a1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + 3600,
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("b", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "b1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + 3600,
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("c", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "c1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix(),
|
2021-01-13 12:02:50 +08:00
|
|
|
HeaderSize: 1024,
|
2020-10-04 14:30:42 +08:00
|
|
|
})
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("d", &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: "d1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() - 2,
|
2021-01-13 12:02:50 +08:00
|
|
|
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
|
|
|
// 随机测试
|
|
|
|
|
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) {
|
2024-01-19 09:27:42 +08:00
|
|
|
list := caches.NewMemoryList()
|
2021-06-11 14:53:51 +08:00
|
|
|
_ = list.Init()
|
2020-12-23 21:28:50 +08:00
|
|
|
before := time.Now()
|
2024-01-21 11:13:30 +08:00
|
|
|
var count = 100
|
|
|
|
|
if testutils.IsSingleTesting() {
|
|
|
|
|
count = 1_000_000
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < count; i++ {
|
2021-11-13 21:30:24 +08:00
|
|
|
key := "https://www.teaos.cn/hello/" + strconv.Itoa(i/10000) + "/" + strconv.Itoa(i) + ".html"
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add(fmt.Sprintf("%d", xxhash.Sum64String(key)), &caches.Item{
|
2021-01-13 12:02:50 +08:00
|
|
|
Key: key,
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: 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()
|
2021-11-13 21:30:24 +08:00
|
|
|
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")
|
|
|
|
|
}
|
2021-06-06 23:42:11 +08:00
|
|
|
|
2023-09-14 18:30:11 +08:00
|
|
|
func TestMapRandomDelete(t *testing.T) {
|
|
|
|
|
var countMap = map[int]int{} // k => count
|
|
|
|
|
|
2024-01-21 11:13:30 +08:00
|
|
|
var count = 1000
|
|
|
|
|
if testutils.IsSingleTesting() {
|
|
|
|
|
count = 1_000_000
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for j := 0; j < count; j++ {
|
2023-09-14 18:30:11 +08:00
|
|
|
var m = map[int]bool{}
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
|
m[i] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var count = 0
|
|
|
|
|
for k := range m {
|
|
|
|
|
delete(m, k)
|
|
|
|
|
count++
|
|
|
|
|
if count >= 10 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for k := range m {
|
|
|
|
|
countMap[k]++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var counts = []int{}
|
|
|
|
|
for _, count := range countMap {
|
|
|
|
|
counts = append(counts, count)
|
|
|
|
|
}
|
|
|
|
|
sort.Ints(counts)
|
|
|
|
|
t.Log("["+types.String(len(counts))+"]", counts)
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-13 21:30:24 +08:00
|
|
|
func TestMemoryList_PurgeLFU(t *testing.T) {
|
2024-01-19 09:27:42 +08:00
|
|
|
var list = caches.NewMemoryList().(*caches.MemoryList)
|
2021-11-13 21:30:24 +08:00
|
|
|
|
|
|
|
|
var before = time.Now()
|
|
|
|
|
defer func() {
|
|
|
|
|
t.Log(time.Since(before).Seconds()*1000, "ms")
|
|
|
|
|
}()
|
|
|
|
|
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("1", &caches.Item{})
|
|
|
|
|
_ = list.Add("2", &caches.Item{})
|
|
|
|
|
_ = list.Add("3", &caches.Item{})
|
|
|
|
|
_ = list.Add("4", &caches.Item{})
|
|
|
|
|
_ = list.Add("5", &caches.Item{})
|
2021-11-13 21:30:24 +08:00
|
|
|
|
2023-09-14 18:30:11 +08:00
|
|
|
//_ = list.IncreaseHit("1")
|
|
|
|
|
//_ = list.IncreaseHit("2")
|
|
|
|
|
//_ = list.IncreaseHit("3")
|
|
|
|
|
//_ = list.IncreaseHit("4")
|
|
|
|
|
//_ = list.IncreaseHit("5")
|
|
|
|
|
|
|
|
|
|
count, err := list.Count()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
t.Log("count items before purge:", count)
|
|
|
|
|
|
|
|
|
|
err = list.PurgeLFU(5, func(hash string) error {
|
2021-11-13 21:30:24 +08:00
|
|
|
t.Log("purge lfu:", hash)
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
t.Log("ok")
|
|
|
|
|
|
2023-09-14 18:30:11 +08:00
|
|
|
count, err = list.Count()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
t.Log("count items left:", count)
|
2021-11-13 21:30:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMemoryList_CleanAll(t *testing.T) {
|
2024-01-19 09:27:42 +08:00
|
|
|
var list = caches.NewMemoryList().(*caches.MemoryList)
|
|
|
|
|
_ = list.Add("a", &caches.Item{})
|
2021-11-13 21:30:24 +08:00
|
|
|
_ = list.CleanAll()
|
2024-01-19 09:27:42 +08:00
|
|
|
logs.PrintAsJSON(list.ItemMaps(), t)
|
2021-11-13 21:30:24 +08:00
|
|
|
t.Log(list.Count())
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 23:42:11 +08:00
|
|
|
func TestMemoryList_GC(t *testing.T) {
|
2024-01-19 09:27:42 +08:00
|
|
|
if !testutils.IsSingleTesting() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list := caches.NewMemoryList().(*caches.MemoryList)
|
2021-06-11 14:53:51 +08:00
|
|
|
_ = list.Init()
|
2021-06-06 23:42:11 +08:00
|
|
|
for i := 0; i < 1_000_000; i++ {
|
2021-11-13 21:30:24 +08:00
|
|
|
key := "https://www.teaos.cn/hello" + strconv.Itoa(i/100000) + "/" + strconv.Itoa(i) + ".html"
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add(fmt.Sprintf("%d", xxhash.Sum64String(key)), &caches.Item{
|
2021-06-06 23:42:11 +08:00
|
|
|
Key: key,
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: 0,
|
2021-06-06 23:42:11 +08:00
|
|
|
BodySize: 0,
|
|
|
|
|
HeaderSize: 0,
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-01-19 09:27:42 +08:00
|
|
|
t.Log("clean...", len(list.ItemMaps()))
|
2021-06-06 23:42:11 +08:00
|
|
|
_ = list.CleanAll()
|
2024-01-19 09:27:42 +08:00
|
|
|
t.Log("cleanAll...", len(list.ItemMaps()))
|
2021-06-06 23:42:11 +08:00
|
|
|
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")
|
2021-06-06 23:42:11 +08:00
|
|
|
|
2023-06-07 21:49:42 +08:00
|
|
|
time.Sleep(30 * time.Minute)
|
|
|
|
|
}
|
2021-06-06 23:42:11 +08:00
|
|
|
}
|
2024-01-19 09:27:42 +08:00
|
|
|
|
|
|
|
|
func BenchmarkMemoryList(b *testing.B) {
|
|
|
|
|
var list = caches.NewMemoryList()
|
|
|
|
|
err := list.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < 1_000_000; i++ {
|
|
|
|
|
_ = list.Add(stringutil.Md5(types.String(i)), &caches.Item{
|
|
|
|
|
Key: "a1",
|
2024-03-22 08:23:22 +08:00
|
|
|
ExpiresAt: time.Now().Unix() + 3600,
|
2024-01-19 09:27:42 +08:00
|
|
|
HeaderSize: 1024,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
for pb.Next() {
|
2024-04-04 08:28:14 +08:00
|
|
|
_, _, _ = list.Exist(types.String("a" + types.String(rands.Int(1, 10000))))
|
2024-01-19 09:27:42 +08:00
|
|
|
_ = list.Add("a"+types.String(rands.Int(1, 100000)), &caches.Item{})
|
|
|
|
|
_, _ = list.Purge(1000, func(hash string) error {
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|