优化计数器相关测试用例

This commit is contained in:
GoEdgeLab
2023-12-24 16:08:57 +08:00
parent f14d430754
commit 81fdc337c9
2 changed files with 9 additions and 8 deletions

View File

@@ -91,7 +91,7 @@ func TestCounterMemory(t *testing.T) {
var stat1 = &runtime.MemStats{} var stat1 = &runtime.MemStats{}
runtime.ReadMemStats(stat1) runtime.ReadMemStats(stat1)
t.Log((stat1.TotalAlloc-stat.TotalAlloc)/(1<<20), "MB") t.Log((stat1.HeapInuse-stat.HeapInuse)/(1<<20), "MB")
t.Log(counter.TotalItems()) t.Log(counter.TotalItems())

View File

@@ -6,6 +6,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/utils/counters" "github.com/TeaOSLab/EdgeNode/internal/utils/counters"
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
"github.com/iwind/TeaGo/assert" "github.com/iwind/TeaGo/assert"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time" timeutil "github.com/iwind/TeaGo/utils/time"
"runtime" "runtime"
"testing" "testing"
@@ -41,9 +42,9 @@ func TestItem_Increase2(t *testing.T) {
var a = assert.NewAssertion(t) var a = assert.NewAssertion(t)
var item = counters.NewItem[uint32](20) var item = counters.NewItem[uint32](23)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
t.Log(item.Increase(), item.Sum(), timeutil.Format("H:i:s")) t.Log("round "+types.String(i)+":", item.Increase(), item.Sum(), timeutil.Format("H:i:s"))
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
} }
@@ -56,14 +57,14 @@ func TestItem_IsExpired(t *testing.T) {
return return
} }
var currentTime = time.Now().Unix()
var item = counters.NewItem[uint32](10) var item = counters.NewItem[uint32](10)
t.Log(item.IsExpired(currentTime)) t.Log(item.IsExpired(time.Now().Unix()))
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
t.Log(item.IsExpired(currentTime)) t.Log(item.IsExpired(time.Now().Unix()))
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
t.Log(item.IsExpired(currentTime)) t.Log(item.IsExpired(time.Now().Unix()))
time.Sleep(2 * time.Second)
t.Log(item.IsExpired(time.Now().Unix()))
} }
func BenchmarkItem_Increase(b *testing.B) { func BenchmarkItem_Increase(b *testing.B) {