perf: 优化扫块成功率统计算法

This commit is contained in:
v03413
2026-01-14 14:40:27 +08:00
parent b6f60828bb
commit 4062bbaf2a
2 changed files with 22 additions and 16 deletions
+20 -16
View File
@@ -9,9 +9,10 @@ const maxRecords = 1000
type stat struct {
mu sync.RWMutex
records []bool // true表示成功,false表示失败
index int // 当前写入位置
count int // 已记录的总数(最多maxRecords)
records []bool
index int // 当前位置
total int // 已记录总数
succ int // 成功记录数
}
var (
@@ -30,10 +31,16 @@ func RecordSuccess(net string) {
s.mu.Lock()
defer s.mu.Unlock()
if s.total >= maxRecords && !s.records[s.index] {
s.succ++
} else if s.total < maxRecords {
s.succ++
}
s.records[s.index] = true
s.index = (s.index + 1) % maxRecords
if s.count < maxRecords {
s.count++
if s.total < maxRecords {
s.total++
}
}
@@ -42,10 +49,14 @@ func RecordFailure(net string) {
s.mu.Lock()
defer s.mu.Unlock()
if s.total >= maxRecords && s.records[s.index] {
s.succ--
}
s.records[s.index] = false
s.index = (s.index + 1) % maxRecords
if s.count < maxRecords {
s.count++
if s.total < maxRecords {
s.total++
}
}
@@ -54,16 +65,9 @@ func GetSuccessRate(net string) string {
s.mu.RLock()
defer s.mu.RUnlock()
if s.count == 0 {
if s.total == 0 {
return "100.00%"
}
successCount := 0
for i := 0; i < s.count; i++ {
if s.records[i] {
successCount++
}
}
return fmt.Sprintf("%.2f%%", float64(successCount)/float64(s.count)*100)
return fmt.Sprintf("%.2f%%", float64(s.succ)/float64(s.total)*100)
}
+2
View File
@@ -229,6 +229,8 @@ func (e *evm) getBlockByNumber(a any) {
return
}
conf.RecordSuccess(e.Network)
nativeTransfers := make([]transfer, 0)
blockTimestamp := make(map[string]time.Time)
for _, itm := range gjson.ParseBytes(body).Array() {