Files
EdgeNode/internal/caches/list_memory_test.go

124 lines
2.5 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"
"github.com/cespare/xxhash"
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 TestList_Add(t *testing.T) {
2021-05-19 12:07:35 +08:00
list := &MemoryList{}
_ = 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
})
t.Log(list.m)
}
func TestList_Remove(t *testing.T) {
2021-05-19 12:07:35 +08:00
list := &MemoryList{}
_ = 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")
2020-10-04 14:30:42 +08:00
t.Log(list.m)
}
func TestList_Purge(t *testing.T) {
2021-05-19 12:07:35 +08:00
list := &MemoryList{}
_ = 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
})
2021-05-19 12:07:35 +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
})
t.Log(list.m)
}
func TestList_Stat(t *testing.T) {
2021-05-19 12:07:35 +08:00
list := &MemoryList{}
_ = 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
func TestList_FindKeysWithPrefix(t *testing.T) {
2021-05-19 12:07:35 +08:00
list := &MemoryList{}
2020-12-23 21:28:50 +08:00
before := time.Now()
for i := 0; i < 1_000_000; i++ {
key := "http://www.teaos.cn/hello" + strconv.Itoa(i/100000) + "/" + 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,
ExpiredAt: 0,
BodySize: 0,
HeaderSize: 0,
2020-12-23 21:28:50 +08:00
})
}
t.Log(time.Since(before).Seconds()*1000, "ms")
before = time.Now()
2021-05-19 12:07:35 +08:00
keys, err := list.FindKeysWithPrefix("http://www.teaos.cn/hello/5000")
if err != nil {
t.Fatal(err)
}
2020-12-23 21:28:50 +08:00
t.Log(len(keys))
t.Log(time.Since(before).Seconds()*1000, "ms")
}