优化计数器

This commit is contained in:
刘祥超
2023-07-15 11:08:25 +08:00
parent 2079b0ebee
commit 1fdce3ef7e
4 changed files with 38 additions and 14 deletions

View File

@@ -6,7 +6,9 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/utils/counters"
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
"github.com/iwind/TeaGo/assert"
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"runtime"
"sync/atomic"
"testing"
@@ -54,9 +56,21 @@ func TestCounter_GC(t *testing.T) {
}
func TestCounter_GC2(t *testing.T) {
var counter = counters.NewCounter()
for i := 0; i < runtime.NumCPU()*32; i++ {
counter.GC()
if !testutils.IsSingleTesting() {
return
}
var counter = counters.NewCounter().WithGC()
for i := 0; i < 1e5; i++ {
counter.Increase(uint64(i), rands.Int(10, 300))
}
var ticker = time.NewTicker(1 * time.Second)
for range ticker.C {
t.Log(timeutil.Format("H:i:s"), counter.TotalItems())
if counter.TotalItems() == 0 {
break
}
}
}
@@ -140,4 +154,6 @@ func BenchmarkCounter_GC(b *testing.B) {
counter.GC()
}
})
//b.Log(counter.TotalItems())
}