mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-28 00:06:35 +08:00
实现新的计数器算法(将时间分片, 统计更加精准)
This commit is contained in:
54
internal/utils/counters/item_test.go
Normal file
54
internal/utils/counters/item_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package counters_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/counters"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestItem_Increase(t *testing.T) {
|
||||
// run only under single testing
|
||||
if !testutils.IsSingleTesting() {
|
||||
return
|
||||
}
|
||||
|
||||
var a = assert.NewAssertion(t)
|
||||
|
||||
var item = counters.NewItem(20)
|
||||
for i := 0; i < 100; i++ {
|
||||
t.Log(item.Increase(), timeutil.Format("i:s"))
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
|
||||
item.Reset()
|
||||
a.IsTrue(item.Sum() == 0)
|
||||
}
|
||||
|
||||
func TestItem_IsExpired(t *testing.T) {
|
||||
if !testutils.IsSingleTesting() {
|
||||
return
|
||||
}
|
||||
|
||||
var item = counters.NewItem(10)
|
||||
t.Log(item.IsExpired())
|
||||
time.Sleep(10 * time.Second)
|
||||
t.Log(item.IsExpired())
|
||||
time.Sleep(2 * time.Second)
|
||||
t.Log(item.IsExpired())
|
||||
}
|
||||
|
||||
func BenchmarkItem_Increase(b *testing.B) {
|
||||
runtime.GOMAXPROCS(1)
|
||||
|
||||
var item = counters.NewItem(60)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
item.Increase()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user