From f5c911150d830dd2f04be7c92855d53dbfa97fac Mon Sep 17 00:00:00 2001 From: p4u Date: Thu, 30 Nov 2023 13:17:16 +0100 Subject: [PATCH] fix blocks per minute estimation Signed-off-by: p4u --- service/vochain.go | 2 +- vochain/vochaininfo/vochaininfo.go | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/service/vochain.go b/service/vochain.go index 4c295e768..90b387247 100644 --- a/service/vochain.go +++ b/service/vochain.go @@ -174,7 +174,7 @@ func VochainPrintInfo(interval time.Duration, vi *vochaininfo.VochainInfo) { b.Reset() a := vi.BlockTimes() if a[1] > 0 { - fmt.Fprintf(&b, " 10m:%.2f", float32(a[1])/1000) + fmt.Fprintf(&b, "10m:%.2f", float32(a[1])/1000) } if a[2] > 0 { fmt.Fprintf(&b, " 1h:%.2f", float32(a[2])/1000) diff --git a/vochain/vochaininfo/vochaininfo.go b/vochain/vochaininfo/vochaininfo.go index 5758081cb..a7ef785aa 100644 --- a/vochain/vochaininfo/vochaininfo.go +++ b/vochain/vochaininfo/vochaininfo.go @@ -287,8 +287,8 @@ func (vi *VochainInfo) Start(sleepSecs uint64) { var duration time.Duration var prevHeight, currentHeight uint64 - var intervalCount, heightDiffSum, voteMetricsCount uint64 - var avgBlocksPerMinute float64 + var accumulatedTimeSecs, heightDiffSum, voteMetricsCount uint64 + var blocksPerMinute float64 var oldVoteTreeSize uint64 duration = time.Second * time.Duration(sleepSecs) for { @@ -297,18 +297,22 @@ func (vi *VochainInfo) Start(sleepSecs uint64) { vi.updateCounters() currentHeight = uint64(vi.vnode.Height()) voteMetricsCount++ - intervalCount++ + accumulatedTimeSecs += sleepSecs heightDiffSum += currentHeight - prevHeight - if sleepSecs*intervalCount >= 60 && heightDiffSum > 0 { - avgBlocksPerMinute = float64(heightDiffSum) / float64((intervalCount * sleepSecs)) - intervalCount = 0 + + if accumulatedTimeSecs >= 60 { + if accumulatedTimeSecs > 0 { + blocksPerMinute = float64(heightDiffSum) * 60.0 / float64(accumulatedTimeSecs) + } + accumulatedTimeSecs = 0 heightDiffSum = 0 } + prevHeight = currentHeight // update values vi.lock.Lock() - vi.blocksMinute = avgBlocksPerMinute + vi.blocksMinute = blocksPerMinute vi.updateBlockTimes() if sleepSecs*voteMetricsCount >= 60 { vi.votesPerMinute = voteCount.Get() - oldVoteTreeSize