app/vmselect/promql: increase floating point calculations accuracy by dividing by 1e3 instead of multiplying by 1e-3

This commit is contained in:
Aliaksandr Valialkin 2020-09-08 14:00:47 +03:00
parent 13dd915302
commit db91045348
5 changed files with 26 additions and 26 deletions

View File

@ -851,7 +851,7 @@ func evalTime(ec *EvalConfig) []*timeseries {
timestamps := rv[0].Timestamps timestamps := rv[0].Timestamps
values := rv[0].Values values := rv[0].Values
for i, ts := range timestamps { for i, ts := range timestamps {
values[i] = float64(ts) * 1e-3 values[i] = float64(ts) / 1e3
} }
return rv return rv
} }

View File

@ -4833,10 +4833,10 @@ func TestExecSuccess(t *testing.T) {
}) })
t.Run(`integrate(time())`, func(t *testing.T) { t.Run(`integrate(time())`, func(t *testing.T) {
t.Parallel() t.Parallel()
q := `integrate(time()*1e-3)` q := `integrate(time()/1e3)`
r := netstorage.Result{ r := netstorage.Result{
MetricName: metricNameExpected, MetricName: metricNameExpected,
Values: []float64{180, 220.00000000000003, 260, 300, 340.00000000000006, 380}, Values: []float64{180, 220.00000000000003, 259.99999999999994, 300, 340.00000000000006, 380},
Timestamps: timestampsExpected, Timestamps: timestampsExpected,
} }
resultExpected := []netstorage.Result{r} resultExpected := []netstorage.Result{r}

View File

@ -670,7 +670,7 @@ func derivValues(values []float64, timestamps []int64) {
values[i] = prevDeriv values[i] = prevDeriv
continue continue
} }
dt := float64(ts-prevTs) * 1e-3 dt := float64(ts-prevTs) / 1e3
prevDeriv = (v - prevValue) / dt prevDeriv = (v - prevValue) / dt
values[i] = prevDeriv values[i] = prevDeriv
prevValue = v prevValue = v
@ -790,7 +790,7 @@ func linearRegression(rfa *rollupFuncArg) (float64, float64) {
n = 0 n = 0
} }
for i, v := range values { for i, v := range values {
dt := float64(timestamps[i]-tFirst) * 1e-3 dt := float64(timestamps[i]-tFirst) / 1e3
vSum += v vSum += v
tSum += dt tSum += dt
tvSum += dt * v tvSum += dt * v
@ -803,7 +803,7 @@ func linearRegression(rfa *rollupFuncArg) (float64, float64) {
k := (n*tvSum - tSum*vSum) / (n*ttSum - tSum*tSum) k := (n*tvSum - tSum*vSum) / (n*ttSum - tSum*tSum)
v := (vSum - k*tSum) / n v := (vSum - k*tSum) / n
// Adjust v to the last timestamp on the given time range. // Adjust v to the last timestamp on the given time range.
v += k * (float64(timestamps[len(timestamps)-1]-tFirst) * 1e-3) v += k * (float64(timestamps[len(timestamps)-1]-tFirst) / 1e3)
return v, k return v, k
} }
@ -1055,7 +1055,7 @@ func rollupTmin(rfa *rollupFuncArg) float64 {
minTimestamp = timestamps[i] minTimestamp = timestamps[i]
} }
} }
return float64(minTimestamp) * 1e-3 return float64(minTimestamp) / 1e3
} }
func rollupTmax(rfa *rollupFuncArg) float64 { func rollupTmax(rfa *rollupFuncArg) float64 {
@ -1074,7 +1074,7 @@ func rollupTmax(rfa *rollupFuncArg) float64 {
maxTimestamp = timestamps[i] maxTimestamp = timestamps[i]
} }
} }
return float64(maxTimestamp) * 1e-3 return float64(maxTimestamp) / 1e3
} }
func rollupSum(rfa *rollupFuncArg) float64 { func rollupSum(rfa *rollupFuncArg) float64 {
@ -1303,7 +1303,7 @@ func rollupDerivFast(rfa *rollupFuncArg) float64 {
vEnd := values[len(values)-1] vEnd := values[len(values)-1]
tEnd := timestamps[len(timestamps)-1] tEnd := timestamps[len(timestamps)-1]
dv := vEnd - prevValue dv := vEnd - prevValue
dt := float64(tEnd-prevTimestamp) * 1e-3 dt := float64(tEnd-prevTimestamp) / 1e3
return dv / dt return dv / dt
} }
@ -1329,7 +1329,7 @@ func rollupIderiv(rfa *rollupFuncArg) float64 {
// So just return nan // So just return nan
return nan return nan
} }
return (values[0] - rfa.prevValue) / (float64(timestamps[0]-rfa.prevTimestamp) * 1e-3) return (values[0] - rfa.prevValue) / (float64(timestamps[0]-rfa.prevTimestamp) / 1e3)
} }
vEnd := values[len(values)-1] vEnd := values[len(values)-1]
tEnd := timestamps[len(timestamps)-1] tEnd := timestamps[len(timestamps)-1]
@ -1353,7 +1353,7 @@ func rollupIderiv(rfa *rollupFuncArg) float64 {
} }
dv := vEnd - vStart dv := vEnd - vStart
dt := tEnd - tStart dt := tEnd - tStart
return dv / (float64(dt) * 1e-3) return dv / (float64(dt) / 1e3)
} }
func rollupLifetime(rfa *rollupFuncArg) float64 { func rollupLifetime(rfa *rollupFuncArg) float64 {
@ -1363,12 +1363,12 @@ func rollupLifetime(rfa *rollupFuncArg) float64 {
if len(timestamps) < 2 { if len(timestamps) < 2 {
return nan return nan
} }
return float64(timestamps[len(timestamps)-1]-timestamps[0]) * 1e-3 return float64(timestamps[len(timestamps)-1]-timestamps[0]) / 1e3
} }
if len(timestamps) == 0 { if len(timestamps) == 0 {
return nan return nan
} }
return float64(timestamps[len(timestamps)-1]-rfa.prevTimestamp) * 1e-3 return float64(timestamps[len(timestamps)-1]-rfa.prevTimestamp) / 1e3
} }
func rollupLag(rfa *rollupFuncArg) float64 { func rollupLag(rfa *rollupFuncArg) float64 {
@ -1378,9 +1378,9 @@ func rollupLag(rfa *rollupFuncArg) float64 {
if math.IsNaN(rfa.prevValue) { if math.IsNaN(rfa.prevValue) {
return nan return nan
} }
return float64(rfa.currTimestamp-rfa.prevTimestamp) * 1e-3 return float64(rfa.currTimestamp-rfa.prevTimestamp) / 1e3
} }
return float64(rfa.currTimestamp-timestamps[len(timestamps)-1]) * 1e-3 return float64(rfa.currTimestamp-timestamps[len(timestamps)-1]) / 1e3
} }
func rollupScrapeInterval(rfa *rollupFuncArg) float64 { func rollupScrapeInterval(rfa *rollupFuncArg) float64 {
@ -1390,12 +1390,12 @@ func rollupScrapeInterval(rfa *rollupFuncArg) float64 {
if len(timestamps) < 2 { if len(timestamps) < 2 {
return nan return nan
} }
return float64(timestamps[len(timestamps)-1]-timestamps[0]) * 1e-3 / float64(len(timestamps)-1) return (float64(timestamps[len(timestamps)-1]-timestamps[0]) / 1e3) / float64(len(timestamps)-1)
} }
if len(timestamps) == 0 { if len(timestamps) == 0 {
return nan return nan
} }
return (float64(timestamps[len(timestamps)-1]-rfa.prevTimestamp) * 1e-3) / float64(len(timestamps)) return (float64(timestamps[len(timestamps)-1]-rfa.prevTimestamp) / 1e3) / float64(len(timestamps))
} }
func rollupChanges(rfa *rollupFuncArg) float64 { func rollupChanges(rfa *rollupFuncArg) float64 {
@ -1709,7 +1709,7 @@ func rollupIntegrate(rfa *rollupFuncArg) float64 {
var sum float64 var sum float64
for i, v := range values { for i, v := range values {
timestamp := timestamps[i] timestamp := timestamps[i]
dt := float64(timestamp-prevTimestamp) * 1e-3 dt := float64(timestamp-prevTimestamp) / 1e3
sum += 0.5 * (v + prevValue) * dt sum += 0.5 * (v + prevValue) * dt
prevTimestamp = timestamp prevTimestamp = timestamp
prevValue = v prevValue = v

View File

@ -139,16 +139,16 @@ func TestDerivValues(t *testing.T) {
values = append([]float64{}, testValues...) values = append([]float64{}, testValues...)
derivValues(values, testTimestamps) derivValues(values, testTimestamps)
valuesExpected = []float64{-8900, 1111.111111111111, -1916.6666666666665, 2538.461538461538, -1818.1818181818182, 3611.111111111111, valuesExpected = []float64{-8900, 1111.111111111111, -1916.6666666666665, 2538.4615384615386, -1818.1818181818182, 3611.1111111111113,
-43500, 1882.3529411764705, -666.6666666666666, 400, 0, 0} -43500, 1882.3529411764705, -666.6666666666667, 400, 0, 0}
testRowsEqual(t, values, testTimestamps, valuesExpected, testTimestamps) testRowsEqual(t, values, testTimestamps, valuesExpected, testTimestamps)
// remove counter resets // remove counter resets
values = append([]float64{}, testValues...) values = append([]float64{}, testValues...)
removeCounterResets(values) removeCounterResets(values)
derivValues(values, testTimestamps) derivValues(values, testTimestamps)
valuesExpected = []float64{3400, 1111.111111111111, 1750, 2538.461538461538, 3090.909090909091, 3611.111111111111, valuesExpected = []float64{3400, 1111.111111111111, 1750, 2538.4615384615386, 3090.909090909091, 3611.1111111111113,
6000, 1882.3529411764705, 1777.7777777777776, 400, 0, 0} 6000, 1882.3529411764705, 1777.7777777777778, 400, 0, 0}
testRowsEqual(t, values, testTimestamps, valuesExpected, testTimestamps) testRowsEqual(t, values, testTimestamps, valuesExpected, testTimestamps)
// duplicate timestamps // duplicate timestamps
@ -914,7 +914,7 @@ func TestRollupFuncsNoWindow(t *testing.T) {
} }
rc.Timestamps = getTimestamps(rc.Start, rc.End, rc.Step) rc.Timestamps = getTimestamps(rc.Start, rc.End, rc.Step)
values := rc.Do(nil, testValues, testTimestamps) values := rc.Do(nil, testValues, testTimestamps)
valuesExpected := []float64{0, -2879.310344827587, 558.0608793686592, 422.84569138276544, 0} valuesExpected := []float64{0, -2879.310344827587, 558.0608793686595, 422.84569138276544, 0}
timestampsExpected := []int64{0, 40, 80, 120, 160} timestampsExpected := []int64{0, 40, 80, 120, 160}
testRowsEqual(t, values, rc.Timestamps, valuesExpected, timestampsExpected) testRowsEqual(t, values, rc.Timestamps, valuesExpected, timestampsExpected)
}) })

View File

@ -1674,15 +1674,15 @@ func newTransformFuncZeroArgs(f func(tfa *transformFuncArg) float64) transformFu
} }
func transformStep(tfa *transformFuncArg) float64 { func transformStep(tfa *transformFuncArg) float64 {
return float64(tfa.ec.Step) * 1e-3 return float64(tfa.ec.Step) / 1e3
} }
func transformStart(tfa *transformFuncArg) float64 { func transformStart(tfa *transformFuncArg) float64 {
return float64(tfa.ec.Start) * 1e-3 return float64(tfa.ec.Start) / 1e3
} }
func transformEnd(tfa *transformFuncArg) float64 { func transformEnd(tfa *transformFuncArg) float64 {
return float64(tfa.ec.End) * 1e-3 return float64(tfa.ec.End) / 1e3
} }
// copyTimeseriesMetricNames returns a copy of tss with real copy of MetricNames, // copyTimeseriesMetricNames returns a copy of tss with real copy of MetricNames,