From 776c2b8f422c24d8371f92a989a812e38cbff901 Mon Sep 17 00:00:00 2001 From: songjiayang Date: Tue, 23 Feb 2021 22:28:04 +0800 Subject: [PATCH] Speed delta value without loop to calculate resultValue Signed-off-by: songjiayang --- promql/functions.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/promql/functions.go b/promql/functions.go index d96d625752..e497be364b 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -70,17 +70,17 @@ func extrapolatedRate(vals []parser.Value, args parser.Expressions, enh *EvalNod if len(samples.Points) < 2 { return enh.Out } - var ( - counterCorrection float64 - lastValue float64 - ) - for _, sample := range samples.Points { - if isCounter && sample.V < lastValue { - counterCorrection += lastValue + + resultValue := samples.Points[len(samples.Points)-1].V - samples.Points[0].V + if isCounter { + var lastValue float64 + for _, sample := range samples.Points { + if sample.V < lastValue { + resultValue += lastValue + } + lastValue = sample.V } - lastValue = sample.V } - resultValue := lastValue - samples.Points[0].V + counterCorrection // Duration between first/last samples and boundary of range. durationToStart := float64(samples.Points[0].T-rangeStart) / 1000