metrics: fix issues with benchmarks (#30667)

This commit is contained in:
Daniel Liu 2024-12-13 14:00:14 +08:00
parent ea355e824b
commit 7d82f27894

View file

@ -3,7 +3,6 @@ package metrics
import ( import (
"math" "math"
"math/rand" "math/rand"
"runtime"
"testing" "testing"
"time" "time"
) )
@ -27,6 +26,7 @@ func BenchmarkCompute1000(b *testing.B) {
SampleVariance(mean, s) SampleVariance(mean, s)
} }
} }
func BenchmarkCompute1000000(b *testing.B) { func BenchmarkCompute1000000(b *testing.B) {
s := make([]int64, 1000000) s := make([]int64, 1000000)
var sum int64 var sum int64
@ -40,28 +40,6 @@ func BenchmarkCompute1000000(b *testing.B) {
SampleVariance(mean, s) SampleVariance(mean, s)
} }
} }
func BenchmarkCopy1000(b *testing.B) {
s := make([]int64, 1000)
for i := 0; i < len(s); i++ {
s[i] = int64(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
sCopy := make([]int64, len(s))
copy(sCopy, s)
}
}
func BenchmarkCopy1000000(b *testing.B) {
s := make([]int64, 1000000)
for i := 0; i < len(s); i++ {
s[i] = int64(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
sCopy := make([]int64, len(s))
copy(sCopy, s)
}
}
func BenchmarkExpDecaySample257(b *testing.B) { func BenchmarkExpDecaySample257(b *testing.B) {
benchmarkSample(b, NewExpDecaySample(257, 0.015)) benchmarkSample(b, NewExpDecaySample(257, 0.015))
@ -237,17 +215,9 @@ func TestUniformSampleStatistics(t *testing.T) {
} }
func benchmarkSample(b *testing.B, s Sample) { func benchmarkSample(b *testing.B, s Sample) {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
pauseTotalNs := memStats.PauseTotalNs
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
s.Update(1) s.Update(1)
} }
b.StopTimer()
runtime.GC()
runtime.ReadMemStats(&memStats)
b.Logf("GC cost: %d ns/op", int(memStats.PauseTotalNs-pauseTotalNs)/b.N)
} }
func testExpDecaySampleStatistics(t *testing.T, s SampleSnapshot) { func testExpDecaySampleStatistics(t *testing.T, s SampleSnapshot) {