mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-10 03:40:24 +08:00
优化计数器相关代码
This commit is contained in:
@@ -33,14 +33,17 @@ func NewItem(lifeSeconds int) *Item {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Item) Increase() uint64 {
|
func (this *Item) Increase() (result uint64) {
|
||||||
var currentTime = fasttime.Now().Unix()
|
var currentTime = fasttime.Now().Unix()
|
||||||
var currentSpanIndex = this.calculateSpanIndex(currentTime)
|
var currentSpanIndex = this.calculateSpanIndex(currentTime)
|
||||||
|
|
||||||
// return quickly
|
// return quickly
|
||||||
if this.lastUpdateTime == currentTime {
|
if this.lastUpdateTime == currentTime {
|
||||||
this.spans[currentSpanIndex]++
|
this.spans[currentSpanIndex]++
|
||||||
return this.Sum()
|
for _, count := range this.spans {
|
||||||
|
result += count
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.lastUpdateTime > 0 {
|
if this.lastUpdateTime > 0 {
|
||||||
@@ -50,16 +53,17 @@ func (this *Item) Increase() uint64 {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var lastSpanIndex = this.calculateSpanIndex(this.lastUpdateTime)
|
var lastSpanIndex = this.calculateSpanIndex(this.lastUpdateTime)
|
||||||
var countSpans = len(this.spans)
|
|
||||||
|
|
||||||
// reset values between LAST and CURRENT
|
if lastSpanIndex != currentSpanIndex {
|
||||||
for index := lastSpanIndex + 1; ; index++ {
|
var countSpans = len(this.spans)
|
||||||
var realIndex = index % countSpans
|
|
||||||
if realIndex <= currentSpanIndex {
|
// reset values between LAST and CURRENT
|
||||||
|
for index := lastSpanIndex + 1; ; index++ {
|
||||||
|
var realIndex = index % countSpans
|
||||||
this.spans[realIndex] = 0
|
this.spans[realIndex] = 0
|
||||||
}
|
if realIndex == currentSpanIndex {
|
||||||
if realIndex == currentSpanIndex {
|
break
|
||||||
break
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,7 +72,11 @@ func (this *Item) Increase() uint64 {
|
|||||||
this.spans[currentSpanIndex]++
|
this.spans[currentSpanIndex]++
|
||||||
this.lastUpdateTime = currentTime
|
this.lastUpdateTime = currentTime
|
||||||
|
|
||||||
return this.Sum()
|
for _, count := range this.spans {
|
||||||
|
result += count
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Item) Sum() (result uint64) {
|
func (this *Item) Sum() (result uint64) {
|
||||||
@@ -84,10 +92,11 @@ func (this *Item) Sum() (result uint64) {
|
|||||||
} else {
|
} else {
|
||||||
var lastSpanIndex = this.calculateSpanIndex(this.lastUpdateTime)
|
var lastSpanIndex = this.calculateSpanIndex(this.lastUpdateTime)
|
||||||
var countSpans = len(this.spans)
|
var countSpans = len(this.spans)
|
||||||
for index := 0; index < countSpans; index++ {
|
for index := currentSpanIndex + 1; ; index++ {
|
||||||
if (currentSpanIndex >= lastSpanIndex && (index <= lastSpanIndex || index >= currentSpanIndex /** a >=b **/)) ||
|
var realIndex = index % countSpans
|
||||||
(currentSpanIndex < lastSpanIndex && index >= currentSpanIndex && index <= lastSpanIndex /** a < b **/) {
|
result += this.spans[realIndex]
|
||||||
result += this.spans[index]
|
if realIndex == lastSpanIndex {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,19 +18,19 @@ func TestItem_Increase(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var item = counters.NewItem(10)
|
var item = counters.NewItem(10)
|
||||||
t.Log(item.Increase())
|
t.Log(item.Increase(), item.Sum())
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
t.Log(item.Increase())
|
t.Log(item.Increase(), item.Sum())
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
t.Log(item.Increase())
|
t.Log(item.Increase(), item.Sum())
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
t.Log(item.Increase())
|
t.Log(item.Increase(), item.Sum())
|
||||||
time.Sleep(6 * time.Second)
|
time.Sleep(6 * time.Second)
|
||||||
t.Log(item.Increase())
|
t.Log(item.Increase(), item.Sum())
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
t.Log(item.Increase())
|
t.Log(item.Increase(), item.Sum())
|
||||||
time.Sleep(11 * time.Second)
|
time.Sleep(11 * time.Second)
|
||||||
t.Log(item.Increase())
|
t.Log(item.Increase(), item.Sum())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestItem_Increase2(t *testing.T) {
|
func TestItem_Increase2(t *testing.T) {
|
||||||
@@ -43,7 +43,7 @@ func TestItem_Increase2(t *testing.T) {
|
|||||||
|
|
||||||
var item = counters.NewItem(20)
|
var item = counters.NewItem(20)
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
t.Log(item.Increase(), timeutil.Format("H:i:s"))
|
t.Log(item.Increase(), item.Sum(), timeutil.Format("H:i:s"))
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,10 +69,11 @@ func TestItem_IsExpired(t *testing.T) {
|
|||||||
func BenchmarkItem_Increase(b *testing.B) {
|
func BenchmarkItem_Increase(b *testing.B) {
|
||||||
runtime.GOMAXPROCS(1)
|
runtime.GOMAXPROCS(1)
|
||||||
|
|
||||||
var item = counters.NewItem(60)
|
b.ReportAllocs()
|
||||||
|
|
||||||
b.RunParallel(func(pb *testing.PB) {
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
for pb.Next() {
|
for pb.Next() {
|
||||||
|
var item = counters.NewItem(60)
|
||||||
item.Increase()
|
item.Increase()
|
||||||
item.Sum()
|
item.Sum()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user