2019-05-22 23:16:55 +02:00
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkRollupAvg(b *testing.B) {
|
|
|
|
rfa := &rollupFuncArg{
|
|
|
|
values: benchValues,
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(len(benchValues)))
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
var vSum float64
|
|
|
|
for pb.Next() {
|
|
|
|
vSum += rollupAvg(rfa)
|
|
|
|
}
|
|
|
|
SinkLock.Lock()
|
|
|
|
Sink += vSum
|
|
|
|
SinkLock.Unlock()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Sink is a global sink for benchmarks.
|
|
|
|
// It guarantees the compiler doesn't remove the code in benchmarks,
|
|
|
|
// which writes data to the Sink.
|
|
|
|
Sink float64
|
|
|
|
|
|
|
|
// SinkLock locks Sink.
|
|
|
|
SinkLock sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
var benchValues = func() []float64 {
|
2023-01-24 04:27:23 +01:00
|
|
|
r := rand.New(rand.NewSource(1))
|
2019-05-22 23:16:55 +02:00
|
|
|
values := make([]float64, 1000)
|
|
|
|
for i := range values {
|
2023-01-24 04:27:23 +01:00
|
|
|
values[i] = r.Float64() * 100
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
return values
|
|
|
|
}()
|