2019-05-22 21:16:55 +00:00
|
|
|
package decimal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-01-26 22:53:16 +00:00
|
|
|
func BenchmarkCalbirateScale(b *testing.B) {
|
|
|
|
aSrc := []int64{1, 2, 3, 4, 5, 67, 8, 9, 12, 324, 29, -34, -94, -84, -34, 0, 2, 3}
|
|
|
|
bSrc := []int64{2, 3, 4, 5, 67, 8, 9, 12, 324, 29, -34, -94, -84, -34, 0, 2, 3, 1}
|
|
|
|
const aScale = 1
|
|
|
|
const bScale = 2
|
|
|
|
const scaleExpected = 1
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(len(aSrc)))
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
var a, b []int64
|
|
|
|
for pb.Next() {
|
|
|
|
a = append(a[:0], aSrc...)
|
|
|
|
b = append(b[:0], bSrc...)
|
|
|
|
scale := CalibrateScale(a, aScale, b, bScale)
|
|
|
|
if scale != scaleExpected {
|
|
|
|
panic(fmt.Errorf("unexpected scale; got %d; want %d", scale, scaleExpected))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func BenchmarkAppendDecimalToFloat(b *testing.B) {
|
2019-11-19 21:32:13 +00:00
|
|
|
b.Run("RealFloat", func(b *testing.B) {
|
2019-11-20 10:00:46 +00:00
|
|
|
benchmarkAppendDecimalToFloat(b, testVA, vaScale)
|
2019-11-01 14:31:51 +00:00
|
|
|
})
|
2019-11-19 21:32:13 +00:00
|
|
|
b.Run("Integers", func(b *testing.B) {
|
2019-11-20 10:00:46 +00:00
|
|
|
benchmarkAppendDecimalToFloat(b, testIntegers, integersScale)
|
2019-11-19 21:32:13 +00:00
|
|
|
})
|
2019-11-01 14:31:51 +00:00
|
|
|
b.Run("Zeros", func(b *testing.B) {
|
2019-11-20 10:00:46 +00:00
|
|
|
benchmarkAppendDecimalToFloat(b, testZeros, 0)
|
2019-11-01 14:31:51 +00:00
|
|
|
})
|
|
|
|
b.Run("Ones", func(b *testing.B) {
|
2019-11-20 10:00:46 +00:00
|
|
|
benchmarkAppendDecimalToFloat(b, testOnes, 0)
|
2019-11-01 14:31:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-20 10:00:46 +00:00
|
|
|
func benchmarkAppendDecimalToFloat(b *testing.B, a []int64, scale int16) {
|
2019-05-22 21:16:55 +00:00
|
|
|
b.ReportAllocs()
|
2019-11-01 14:31:51 +00:00
|
|
|
b.SetBytes(int64(len(a)))
|
2019-05-22 21:16:55 +00:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
var fa []float64
|
|
|
|
for pb.Next() {
|
2019-11-20 10:00:46 +00:00
|
|
|
fa = AppendDecimalToFloat(fa[:0], a, scale)
|
2024-02-24 00:07:51 +00:00
|
|
|
Sink.Add(uint64(len(fa)))
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-01 14:31:51 +00:00
|
|
|
var testZeros = make([]int64, 8*1024)
|
|
|
|
var testOnes = func() []int64 {
|
|
|
|
a := make([]int64, 8*1024)
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
a[i] = 1
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}()
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func BenchmarkAppendFloatToDecimal(b *testing.B) {
|
|
|
|
b.Run("RealFloat", func(b *testing.B) {
|
|
|
|
benchmarkAppendFloatToDecimal(b, testFAReal)
|
|
|
|
})
|
|
|
|
b.Run("Integers", func(b *testing.B) {
|
|
|
|
benchmarkAppendFloatToDecimal(b, testFAInteger)
|
|
|
|
})
|
2019-11-01 14:31:51 +00:00
|
|
|
b.Run("Zeros", func(b *testing.B) {
|
|
|
|
benchmarkAppendFloatToDecimal(b, testFZeros)
|
|
|
|
})
|
|
|
|
b.Run("Ones", func(b *testing.B) {
|
|
|
|
benchmarkAppendFloatToDecimal(b, testFOnes)
|
|
|
|
})
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 14:31:51 +00:00
|
|
|
var testFZeros = make([]float64, 8*1024)
|
|
|
|
var testFOnes = func() []float64 {
|
|
|
|
a := make([]float64, 8*1024)
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
a[i] = 1
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}()
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func benchmarkAppendFloatToDecimal(b *testing.B, fa []float64) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(len(fa)))
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
var da []int64
|
|
|
|
var e int16
|
|
|
|
var sink uint64
|
|
|
|
for pb.Next() {
|
|
|
|
da, e = AppendFloatToDecimal(da[:0], fa)
|
|
|
|
sink += uint64(len(da))
|
|
|
|
sink += uint64(e)
|
|
|
|
}
|
2024-02-24 00:07:51 +00:00
|
|
|
Sink.Add(sink)
|
2019-05-22 21:16:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var testFAReal = func() []float64 {
|
2023-01-24 03:23:37 +00:00
|
|
|
r := rand.New(rand.NewSource(1))
|
2019-05-22 21:16:55 +00:00
|
|
|
fa := make([]float64, 8*1024)
|
|
|
|
for i := 0; i < len(fa); i++ {
|
2023-01-24 03:23:37 +00:00
|
|
|
fa[i] = r.NormFloat64() * 1e-6
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
return fa
|
|
|
|
}()
|
|
|
|
|
|
|
|
var testFAInteger = func() []float64 {
|
2023-01-24 03:23:37 +00:00
|
|
|
r := rand.New(rand.NewSource(2))
|
2019-05-22 21:16:55 +00:00
|
|
|
fa := make([]float64, 8*1024)
|
|
|
|
for i := 0; i < len(fa); i++ {
|
2023-01-24 03:23:37 +00:00
|
|
|
fa[i] = float64(int(r.NormFloat64() * 1e6))
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
return fa
|
|
|
|
}()
|
|
|
|
|
2019-11-20 10:00:46 +00:00
|
|
|
var testVA, vaScale = AppendFloatToDecimal(nil, testFAReal)
|
|
|
|
var testIntegers, integersScale = AppendFloatToDecimal(nil, testFAInteger)
|
2019-11-19 21:32:13 +00:00
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func BenchmarkFromFloat(b *testing.B) {
|
|
|
|
for _, f := range []float64{0, 1234, 12334345, 12343.4344, 123.45678901e12, 12.3454435e30} {
|
|
|
|
b.Run(fmt.Sprintf("%g", f), func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(1)
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
var sink uint64
|
|
|
|
for pb.Next() {
|
|
|
|
v, e := FromFloat(f)
|
|
|
|
sink += uint64(v)
|
|
|
|
sink += uint64(e)
|
|
|
|
}
|
2024-02-24 00:07:51 +00:00
|
|
|
Sink.Add(sink)
|
2019-05-22 21:16:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 00:07:51 +00:00
|
|
|
var Sink atomic.Uint64
|