Skip to content

Commit

Permalink
refactor: fix initialization of minTime and maxTime variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mdqst authored Feb 11, 2025
1 parent 2cd5023 commit 93f8614
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions tools/blocktime/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,31 @@ Block Time (from %d to %d):
// analyzeBlockTimes returns the average, min, max, and standard deviation of the block times.
// Units are in milliseconds.
func analyzeBlockTimes(times []time.Time) (float64, float64, float64, float64) {
numberOfObservations := len(times) - 1
totalTime := times[numberOfObservations].Sub(times[0])
averageTime := float64(totalTime.Milliseconds()) / float64(numberOfObservations)
variance, minTime, maxTime := float64(0), float64(0), float64(0)
for i := 0; i < numberOfObservations; i++ {
diff := float64(times[i+1].Sub(times[i]).Milliseconds())
if minTime == 0 || diff < minTime {
minTime = diff
}
if maxTime == 0 || diff > maxTime {
maxTime = diff
}
variance += math.Pow(averageTime-diff, 2)
}
stddev := math.Sqrt(variance / float64(numberOfObservations))
return averageTime, minTime, maxTime, stddev
numberOfObservations := len(times) - 1
totalTime := times[numberOfObservations].Sub(times[0])
averageTime := float64(totalTime.Milliseconds()) / float64(numberOfObservations)

// Initialize minTime and maxTime correctly
minTime := math.MaxFloat64
maxTime := -math.MaxFloat64

variance := float64(0)

for i := 0; i < numberOfObservations; i++ {
diff := float64(times[i+1].Sub(times[i]).Milliseconds())

// Calculate the minimum and maximum block times
if diff < minTime {
minTime = diff
}
if diff > maxTime {
maxTime = diff
}

variance += math.Pow(averageTime-diff, 2)
}

stddev := math.Sqrt(variance / float64(numberOfObservations))
return averageTime, minTime, maxTime, stddev
}

0 comments on commit 93f8614

Please sign in to comment.