github.com/MetalBlockchain/metalgo@v1.11.9/utils/math/continuous_averager_benchmark_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package math 5 6 import ( 7 "fmt" 8 "testing" 9 "time" 10 ) 11 12 func BenchmarkAveragers(b *testing.B) { 13 periods := []time.Duration{ 14 time.Millisecond, 15 time.Duration(0), 16 -time.Millisecond, 17 } 18 for _, period := range periods { 19 name := fmt.Sprintf("period=%s", period) 20 b.Run(name, func(b *testing.B) { 21 a := NewAverager(0, time.Second, time.Now()) 22 AveragerBenchmark(b, a, period) 23 }) 24 } 25 } 26 27 func AveragerBenchmark(b *testing.B, a Averager, period time.Duration) { 28 currentTime := time.Now() 29 30 b.ResetTimer() 31 for i := 0; i < b.N; i++ { 32 currentTime = currentTime.Add(period) 33 a.Observe(float64(i), currentTime) 34 } 35 }