优化测试用例

This commit is contained in:
GoEdgeLab
2023-12-25 16:57:25 +08:00
parent 5b6f572a47
commit 0f76de73ee

View File

@@ -1,6 +1,7 @@
package ttlcache package ttlcache
import ( import (
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime" "github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
"github.com/iwind/TeaGo/assert" "github.com/iwind/TeaGo/assert"
@@ -8,6 +9,7 @@ import (
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time" timeutil "github.com/iwind/TeaGo/utils/time"
"runtime" "runtime"
"runtime/debug"
"strconv" "strconv"
"sync/atomic" "sync/atomic"
"testing" "testing"
@@ -48,16 +50,34 @@ func TestCache_Memory(t *testing.T) {
} }
var cache = NewCache[int]() var cache = NewCache[int]()
var isReady bool
testutils.StartMemoryStats(t, func() { testutils.StartMemoryStats(t, func() {
if !isReady {
return
}
t.Log(cache.Count(), "items") t.Log(cache.Count(), "items")
}) })
var count = 20_000_000 var count = 1_000_000
if utils.SystemMemoryGB() > 4 {
count = 20_000_000
}
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
cache.Write("a"+strconv.Itoa(i), 1, time.Now().Unix()+int64(rands.Int(0, 300))) cache.Write("a"+strconv.Itoa(i), 1, time.Now().Unix()+int64(rands.Int(0, 300)))
} }
func() {
var before = time.Now()
runtime.GC()
var costSeconds = time.Since(before).Seconds()
var stats = &debug.GCStats{}
debug.ReadGCStats(stats)
t.Log("GC pause:", stats.Pause[0].Seconds()*1000, "ms", "cost:", costSeconds*1000, "ms")
}()
isReady = true
t.Log(cache.Count()) t.Log(cache.Count())
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)