diff --git a/tools/blocktime/main.go b/tools/blocktime/main.go index 661c4dadd7..1cfdffe212 100644 --- a/tools/blocktime/main.go +++ b/tools/blocktime/main.go @@ -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 } +