Skip to content

Commit

Permalink
avg time
Browse files Browse the repository at this point in the history
Signed-off-by: bufferflies <1045931706@qq.com>
  • Loading branch information
bufferflies committed Apr 11, 2023
1 parent ce053eb commit fb22d1e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/movingaverage/avg_over_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewAvgOverTime(interval time.Duration) *AvgOverTime {
que: queue.New(),
margin: deltaWithInterval{
delta: 0,
interval: time.Microsecond,
interval: 0,
},
deltaSum: 0,
intervalSum: 0,
Expand All @@ -57,6 +57,7 @@ func (aot *AvgOverTime) Get() float64 {
if aot.intervalSum < aot.avgInterval {
return 0
}

marginDelta := aot.margin.delta * (aot.intervalSum.Seconds() - aot.avgInterval.Seconds()) / aot.margin.interval.Seconds()
return (aot.deltaSum - marginDelta) / aot.avgInterval.Seconds()
}
Expand All @@ -68,7 +69,7 @@ func (aot *AvgOverTime) Clear() {
}
aot.margin = deltaWithInterval{
delta: 0,
interval: time.Microsecond,
interval: 0,
}
aot.intervalSum = 0
aot.deltaSum = 0
Expand Down Expand Up @@ -135,7 +136,7 @@ func (aot *AvgOverTime) GetIntervalSum() time.Duration {
// GetInstantaneous returns the value just added.
func (aot *AvgOverTime) GetInstantaneous() float64 {
if aot.que.Len() == 0 || aot.que.Back() == nil {
return 0
return aot.margin.delta
}
return aot.que.Back().(deltaWithInterval).delta
}
1 change: 1 addition & 0 deletions pkg/movingaverage/avg_over_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestPulse2(t *testing.T) {
aot := NewAvgOverTime(dur)
aot.Add(1000, dur)
re.Equal(float64(1000), aot.GetInstantaneous())
re.True(aot.IsFull())
aot.Clear()
aot.Add(1000, dur)
re.Equal(float64(1000), aot.GetInstantaneous())
Expand Down

0 comments on commit fb22d1e

Please sign in to comment.